Skip to content

Commit

Permalink
Fixed various issues related to updating, deleting and creating new c…
Browse files Browse the repository at this point in the history
…ompanies
  • Loading branch information
Gugi-Games committed Jun 12, 2024
1 parent 85085dd commit e5cce02
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 93 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/MeetMate/company/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.MeetMate.enums.BusinessType;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.IndexDirection;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "companies")
@Data
@NoArgsConstructor
public class Company {
@Id
@Indexed(direction = IndexDirection.ASCENDING)
private long id;
private String name;
private String description;
private BusinessType businessType;
Expand All @@ -23,5 +20,7 @@ public class Company {
public Company(String name, String ownerEmail) {
this.name = name;
this.ownerEmail = ownerEmail;
this.description = "";
this.businessType = null;
}
}
105 changes: 70 additions & 35 deletions src/main/java/com/MeetMate/company/CompanyController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.MeetMate.company;

import com.MeetMate._experiments.Experimentational;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.graphql.ConditionalOnGraphQlSchema;
import org.springframework.graphql.data.method.annotation.*;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.ContextValue;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -19,50 +18,86 @@
@RequiredArgsConstructor
public class CompanyController {

private final CompanyService companyService;
private final CompanyService companyService;

@Experimentational
@GetMapping(path = "test")
public Company test(@RequestHeader(name = "Authorization") String token) {
token = token.substring(7);
return companyService.getCompany(token);
}
@QueryMapping
public Company getCompany(@ContextValue(name = "token") String token) {
token = token.substring(7);
try {
return companyService.getCompany(token);

@QueryMapping
public Company getCompany(@ContextValue(name = "token") String token) {
token = token.substring(7);
try {
var c = companyService.getCompany(token);
System.out.println(c);
return c;

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return null;
} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return null;
// if (tc == EntityNotFoundException.class)
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());
//
// if (tc == IllegalArgumentException.class)
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage());
//
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}
}

@MutationMapping
public ResponseEntity<?> createCompany(
@Argument String companyName,
@Argument String ownerEmail,
@Argument String ownerName,
@Argument String ownerPassword) {
try {
companyService.createCompany(companyName, ownerEmail, ownerName, ownerPassword);
return ResponseEntity.ok().build();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
if (tc == InaccessibleObjectException.class)
return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}

@MutationMapping
public ResponseEntity<?> createCompany(@Argument String companyName, @Argument String ownerEmail, @Argument String ownerName, @Argument String ownerPassword) {
try {
companyService.createCompany(companyName, ownerEmail, ownerName, ownerPassword);
return ResponseEntity.ok().build();
@MutationMapping
public ResponseEntity<?> editCompany(
@ContextValue String token,
@Argument String companyName,
@Argument String description,
@Argument String businessType) {
token = token.substring(7);
try {
companyService.editCompany(token, companyName, description, businessType);
return ResponseEntity.ok().build();
} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
if (tc == InaccessibleObjectException.class)
return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body("message: " + t.getMessage());
if (tc == EntityNotFoundException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}

@MutationMapping
public ResponseEntity<?> deleteCompany(@ContextValue String token) {
token = token.substring(7);
try {
companyService.deleteCompany(token);
return ResponseEntity.ok().build();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
if (tc == EntityNotFoundException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/MeetMate/company/CompanyRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.MeetMate.company;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -9,4 +12,8 @@
public interface CompanyRepository extends MongoRepository<Company, Long> {

Optional<Company> findCompanyByOwnerEmail(String ownerEmail);

// @Modifying
// @Query("{ 'ownerEmail': :#{#ownerEmail} }")
// Company updateByOwnerEmail(String ownerEmail);
}
84 changes: 59 additions & 25 deletions src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.MeetMate.company;

import com.MeetMate.enums.BusinessType;
import com.MeetMate.enums.UserRole;
import com.MeetMate.security.JwtService;
import com.MeetMate.user.UserController;
import com.MeetMate.user.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand All @@ -14,36 +20,64 @@
@RequiredArgsConstructor
public class CompanyService {

private final UserController userController;
private final UserRepository userRepository;
private final CompanyRepository companyRepository;
private final JwtService jwtService;
private final UserController userController;
private final UserRepository userRepository;
private final CompanyRepository companyRepository;
private final JwtService jwtService;
private final MongoTemplate mongoTemplate;

public Company getCompany(String token) throws IllegalArgumentException {
String ownerEmail = jwtService.extractUserEmail(token);
public Company getCompany(String token) throws IllegalArgumentException {
String ownerEmail = jwtService.extractUserEmail(token);

//Test if user is a company owner
if (userRepository.findUserByEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
throw new IllegalArgumentException("User is not a company owner");
//Test if user is a company owner
if (userRepository.findUserByEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
throw new IllegalArgumentException("User is not a company owner");

return companyRepository.findCompanyByOwnerEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}
return companyRepository.findCompanyByOwnerEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}

public void createCompany(String companyName, String ownerEmail, String ownerName, String ownerPassword) {
//Create the company owner
MultiValueMap<String, String> ownerData = new LinkedMultiValueMap<>();
ownerData.add("email", ownerEmail);
ownerData.add("name", ownerName);
ownerData.add("password", ownerPassword);
ownerData.add("role", UserRole.COMPANY_OWNER.toString());
@Transactional
public void createCompany(String companyName, String ownerEmail, String ownerName, String ownerPassword) {
//Create the company owner
MultiValueMap<String, String> ownerData = new LinkedMultiValueMap<>();
ownerData.add("email", ownerEmail);
ownerData.add("name", ownerName);
ownerData.add("password", ownerPassword);
ownerData.add("role", UserRole.COMPANY_OWNER.toString());

//Get id of company Owner
userController.registerNewUser(ownerData);
userController.registerNewUser(ownerData);

companyRepository.save(new Company(companyName, ownerEmail));
}
companyRepository.save(new Company(companyName, ownerEmail));
}

@Transactional
public void editCompany(String token, String companyName, String description, String businessType) {
Company company = getCompany(token);
String ownerEmail = jwtService.extractUserEmail(token);
// if(companyName != null) company.setName(companyName);
// if(description != null) company.setDescription(description);
// if(businessType != null) company.setBusinessType(BusinessType.valueOf(businessType));
// companyRepository.save(company);

Query query = new Query(Criteria.where("ownerEmail").is(ownerEmail));
Update update = new Update();
if (companyName != null) update.set("name", companyName);
if (description != null) update.set("description", description);
if (businessType != null) update.set("businessType", BusinessType.valueOf(businessType));
mongoTemplate.updateFirst(query, update, Company.class);
}

@Transactional
public void deleteCompany(String token) {
Company company = getCompany(token);
try {
userController.deleteUser(token);
} catch (Throwable t) {
return;
}
companyRepository.delete(company);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.MeetMate.security;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.WebGraphQlResponse;
Expand All @@ -13,10 +14,12 @@
public class RequestHeaderInterceptor implements WebGraphQlInterceptor {

@Override
public @NotNull Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
String value = request.getHeaders().getFirst("Authorization");
request.configureExecutionInput((executionInput, builder) ->
builder.graphQLContext(Collections.singletonMap("token", value)).build());
if (value != null) {
request.configureExecutionInput((executionInput, builder) ->
builder.graphQLContext(Collections.singletonMap("token", value)).build());
}
return chain.next(request);
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/MeetMate/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public ResponseEntity<?> authenticateUser(@RequestParam MultiValueMap<String, St

@PostMapping(path = "refresh")
@ResponseBody
public ResponseEntity<?> refreshAccessToken(
@RequestHeader(name = "Authorization") String refreshToken) {
public ResponseEntity<?> refreshAccessToken(@RequestHeader(name = "Authorization") String refreshToken) {
refreshToken = refreshToken.substring(7);
try {
return ResponseEntity.ok(userService.refreshAccessToken(refreshToken));
Expand Down Expand Up @@ -147,7 +146,7 @@ public ResponseEntity<?> deleteUser(@RequestHeader(name = "Authorization") Strin
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("message: " + t.getMessage());
.body("type: " + tc + "\nmessage: " + t.getMessage());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/MeetMate/user/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(String email);

Optional<User> findUserById(Long id);

void deleteByEmail(String email);
}
Loading

0 comments on commit e5cce02

Please sign in to comment.