Skip to content

Commit

Permalink
General logic adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugi-Games committed Oct 24, 2024
1 parent fb57c2d commit ddbaeb7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/MeetMate/company/CompanyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -83,31 +84,28 @@ 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);
}

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<GetResponse> getAllMembers(String token) {
Company company = getCompanyWithToken(token);
System.out.println(company.toString());

ArrayList<GetResponse> members = new ArrayList<>();
for (Long memberId : company.getMemberIds()) {
GetResponse member = getMemberById(memberId);
members.add(member);
}
System.out.println(members.toString());
return members;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ddbaeb7

Please sign in to comment.