From ddbaeb7c2520bc9d30c2d31b8ccbf035879dbe66 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 24 Oct 2024 20:17:47 +0200 Subject: [PATCH] General logic adjustments --- .../MeetMate/company/CompanyController.java | 4 +++ .../com/MeetMate/company/CompanyService.java | 26 +++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index 93d915f..67e0eb4 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -6,6 +6,7 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.hibernate.mapping.Array; +import org.springframework.data.mongodb.MongoTransactionException; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.ContextValue; import org.springframework.graphql.data.method.annotation.MutationMapping; @@ -116,6 +117,9 @@ public ResponseEntity deleteCompany(@ContextValue String token) { if (tc == IllegalArgumentException.class) return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); + if (tc == MongoTransactionException.class) + return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body("message: " + t.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); } } diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index 08936ec..ec425cc 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -11,6 +11,7 @@ import jakarta.persistence.EntityNotFoundException; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.springframework.data.mongodb.MongoTransactionException; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; @@ -71,9 +72,9 @@ public void editCompany(String token, String companyName, String description, St 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)); + if (companyName != null && !companyName.isEmpty()) update.set("name", companyName); + if (description != null && !description.isEmpty()) update.set("description", description); + if (businessType != null && !businessType.isEmpty()) update.set("businessType", BusinessType.valueOf(businessType)); mongoTemplate.updateFirst(query, update, Company.class); } @@ -83,7 +84,7 @@ public void deleteCompany(String token) { try { userController.deleteUser("Bearer:" + token); } catch (Throwable t) { - return; + throw new MongoTransactionException("Could not delete company owner"); } companyRepository.delete(company); } @@ -91,23 +92,20 @@ public void deleteCompany(String token) { public GetResponse getMember(String token, long memberId) { Company company = getCompanyWithToken(token); - GetResponse member = getMemberById(memberId); + if (isNotCompanyMember(company, memberId)) + throw new EntityNotFoundException("Not a company member!"); - if (!company.getMemberIds().contains(member.getId())) - throw new EntityNotFoundException("Member not found"); - - return member; + return getMemberById(memberId); } public ArrayList getAllMembers(String token) { Company company = getCompanyWithToken(token); - System.out.println(company.toString()); + ArrayList members = new ArrayList<>(); for (Long memberId : company.getMemberIds()) { GetResponse member = getMemberById(memberId); members.add(member); } - System.out.println(members.toString()); return members; } @@ -145,7 +143,7 @@ public void addMember(String token, String memberEmail, String memberName, Strin public void deleteMember(String token, long memberId) throws IllegalAccessException { Company company = getCompanyWithToken(token); - if (!isCompanyMember(company, memberId)) + if (isNotCompanyMember(company, memberId)) throw new IllegalAccessException("Not a member of the company"); User member = userRepository.findUserById(memberId) @@ -173,8 +171,8 @@ private Company getCompanyWithToken(String token) throws IllegalArgumentExceptio .orElseThrow(() -> new EntityNotFoundException("Company not found")); } - private boolean isCompanyMember(Company company, long memberId) { - return company.getMemberIds().contains(memberId); + private boolean isNotCompanyMember(Company company, long memberId) { + return !company.getMemberIds().contains(memberId); } private GetResponse getMemberById(long id) {