From 87b81a9280b99c1678e9b25a0bcbba380331059b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 18:44:47 +0200 Subject: [PATCH 1/8] Added associated companies to get user response and added method to subscribe to a company --- .../com/MeetMate/response/GetResponse.java | 5 +++ src/main/java/com/MeetMate/user/User.java | 18 +++++------ .../com/MeetMate/user/UserController.java | 23 ++++++++++++++ .../java/com/MeetMate/user/UserService.java | 31 ++++++++++++------- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/MeetMate/response/GetResponse.java b/src/main/java/com/MeetMate/response/GetResponse.java index bb295eb..77cde4f 100644 --- a/src/main/java/com/MeetMate/response/GetResponse.java +++ b/src/main/java/com/MeetMate/response/GetResponse.java @@ -1,7 +1,10 @@ package com.MeetMate.response; import com.MeetMate.enums.UserRole; + import java.time.LocalDate; +import java.util.ArrayList; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -16,4 +19,6 @@ public class GetResponse { LocalDate created_at; String email; UserRole role; + long associatedCompany; + ArrayList subscribedCompanies; } diff --git a/src/main/java/com/MeetMate/user/User.java b/src/main/java/com/MeetMate/user/User.java index 0cb66cb..c8f7850 100644 --- a/src/main/java/com/MeetMate/user/User.java +++ b/src/main/java/com/MeetMate/user/User.java @@ -9,10 +9,7 @@ import org.springframework.security.core.userdetails.UserDetails; import java.time.LocalDate; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Entity @Table(name = "users") @@ -32,7 +29,7 @@ public class User implements UserDetails { private String refreshToken; // bool verified private long associatedCompany; - private long[] subscribedCompanies; + private ArrayList subscribedCompanies; public User() {} @@ -45,6 +42,12 @@ public User(Long id, String name, String email, String password) { this.createdAt = LocalDate.now(); } + @Experimentational + public User(String email, String password) { + this.email = email; + this.password = password; + } + public User(String name, String email, String password, UserRole role) { this.name = name; this.email = email; @@ -52,11 +55,6 @@ public User(String name, String email, String password, UserRole role) { this.createdAt = LocalDate.now(); this.role = role; } - @Experimentational - public User(String email, String password) { - this.email = email; - this.password = password; - } public Map generateMap() { HashMap map = new HashMap<>(); diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index 33ce42c..abb974d 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -152,4 +152,27 @@ public ResponseEntity deleteUser(@RequestHeader(name = "Authorization") Strin .body("type: " + tc + "\nmessage: " + t.getMessage()); } } + + public ResponseEntity subscribeToCompany( + @RequestHeader(name = "Authorization") String token, + @RequestParam long companyId + ) { + token = token.substring(7); + try { + userService.subscribeToCompany(token, companyId); + return ResponseEntity.ok().build(); + + } catch (Throwable t) { + Class 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_ACCEPTABLE).body("message: " + t.getMessage()); + + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("message: " + t.getMessage()); + } + } } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index a273e13..5690852 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -17,7 +17,6 @@ import javax.naming.NameAlreadyBoundException; import java.util.List; -import java.util.Optional; @Service @RequiredArgsConstructor @@ -31,12 +30,9 @@ public class UserService { public GetResponse getUserByEmail(String token) { String email = jwtService.extractUserEmail(token); - Optional userOptional = userRepository.findUserByEmail(email); - - User user = - userRepository - .findUserByEmail(email) - .orElseThrow(() -> new EntityNotFoundException("User does not exist")); + User user = userRepository.findUserByEmail(email) + .orElseThrow(() -> new EntityNotFoundException("User does not exist")); + ; return GetResponse.builder() .id(user.getId()) @@ -44,6 +40,8 @@ public GetResponse getUserByEmail(String token) { .created_at(user.getCreatedAt()) .email(user.getEmail()) .role(user.getRole()) + .associatedCompany(user.getAssociatedCompany()) + .subscribedCompanies(user.getSubscribedCompanies()) .build(); } @@ -158,11 +156,22 @@ public RefreshResponse refreshAccessToken(String refreshToken) { @Transactional public void deleteUser(String token) { String email = jwtService.extractUserEmail(token); - User user = - userRepository - .findUserByEmail(email) - .orElseThrow(() -> new EntityNotFoundException("User does not exist.")); + User user = userRepository + .findUserByEmail(email) + .orElseThrow(() -> new EntityNotFoundException("User does not exist.")); userRepository.deleteByEmail(email); } + + @Transactional + public void subscribeToCompany(String token, long companyId) { + String email = jwtService.extractUserEmail(token); + User user = userRepository.findUserByEmail(email) + .orElseThrow(() -> new EntityNotFoundException("User does not exist.")); + + if (user.getRole() != UserRole.CLIENT) + throw new IllegalArgumentException("Only CLIENT users can subscribe to companies"); + + user.getSubscribedCompanies().add(companyId); + } } From 93ca984194650438e181bbc75a699cc74f488536 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 18:49:24 +0200 Subject: [PATCH 2/8] Shortend get user response for specific roles --- src/main/java/com/MeetMate/user/UserService.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 5690852..188a70a 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -32,17 +32,22 @@ public GetResponse getUserByEmail(String token) { User user = userRepository.findUserByEmail(email) .orElseThrow(() -> new EntityNotFoundException("User does not exist")); - ; - return GetResponse.builder() + GetResponse response = GetResponse.builder() .id(user.getId()) .name(user.getName()) .created_at(user.getCreatedAt()) .email(user.getEmail()) .role(user.getRole()) - .associatedCompany(user.getAssociatedCompany()) - .subscribedCompanies(user.getSubscribedCompanies()) .build(); + + switch (user.getRole()) { + case COMPANY_OWNER, COMPANY_MEMBER -> response.setAssociatedCompany(user.getAssociatedCompany()); + case CLIENT -> response.setSubscribedCompanies(user.getSubscribedCompanies()); + default -> throw new IllegalStateException(user.getRole() + " is invalid!"); + } + + return response; } public List getAllUsers() { From a3c46bc1d087884e0fffcb5309170097217a95e4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 19:10:42 +0200 Subject: [PATCH 3/8] Added add company member method --- .../java/com/MeetMate/company/Company.java | 4 +++- .../MeetMate/company/CompanyController.java | 24 +++++++++++++++++++ .../com/MeetMate/company/CompanyService.java | 19 +++++++++++++++ .../com/MeetMate/user/UserController.java | 4 +++- src/main/resources/graphql/schema.graphqls | 6 +++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/MeetMate/company/Company.java b/src/main/java/com/MeetMate/company/Company.java index 52e7a24..3a2040f 100644 --- a/src/main/java/com/MeetMate/company/Company.java +++ b/src/main/java/com/MeetMate/company/Company.java @@ -6,6 +6,8 @@ import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.ArrayList; + @Document(collection = "companies") @Data @NoArgsConstructor @@ -14,7 +16,7 @@ public class Company { private String name; private String description; private BusinessType businessType; - private String[] memberEmails; + private ArrayList memberEmails; @Indexed(unique = true) private String ownerEmail; diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index 72fb132..fee53b1 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -100,4 +100,28 @@ public ResponseEntity deleteCompany(@ContextValue String token) { } } + @MutationMapping + public ResponseEntity addMember( + @ContextValue String token, + @Argument String memberEmail, + @Argument String memberName, + @Argument String memberPassword) { + token = token.substring(7); + try { + companyService.addMember(token, memberEmail, memberName, memberPassword); + return ResponseEntity.ok().build(); + + } catch (Throwable t) { + Class 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()); + } + } + } diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index df1d702..0dfd806 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -73,6 +73,25 @@ public void deleteCompany(String token) { companyRepository.delete(company); } + @Transactional + public void addMember(String token, String memberEmail, String memberName, String memberPassword) { + Company company = getCompanyWithToken(token); + + MultiValueMap memberData = new LinkedMultiValueMap<>(); + memberData.add("email", memberEmail); + memberData.add("name", memberName); + memberData.add("password", memberPassword); + memberData.add("role", UserRole.COMPANY_MEMBER.toString()); + memberData.add("associatedCompany", String.valueOf(company.getId())); + + userController.registerNewUser(memberData); + + Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail())); + Update update = new Update(); + update.set("memberEmails", company.getMemberEmails().add(memberEmail)); + mongoTemplate.updateFirst(query, update, Company.class); + } + private Company getCompanyWithToken(String token) throws IllegalArgumentException { String ownerEmail = jwtService.extractUserEmail(token); if (userRepository.findUserByEmail(ownerEmail) diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index abb974d..e6ca2cb 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -153,9 +153,11 @@ public ResponseEntity deleteUser(@RequestHeader(name = "Authorization") Strin } } + @PutMapping(path = "subscribe") + @ResponseBody public ResponseEntity subscribeToCompany( @RequestHeader(name = "Authorization") String token, - @RequestParam long companyId + @RequestParam long companyId ) { token = token.substring(7); try { diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index 8f32ba6..f5137ea 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -27,6 +27,12 @@ type Mutation { deleteCompany: String + addMember( + memberEmail: String! + memberName: String! + memberPassword: String! + ): String + #Appointment Mutations createAppointment( from: String, From a29230e1783eecea8ce83e706ee8a1f9d62ba45e Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 19:29:03 +0200 Subject: [PATCH 4/8] Added get Member Method --- .../MeetMate/company/CompanyController.java | 50 +++++++++++++------ .../com/MeetMate/company/CompanyService.java | 27 ++++++++++ .../com/MeetMate/user/UserController.java | 2 +- .../java/com/MeetMate/user/UserService.java | 3 +- src/main/resources/graphql/schema.graphqls | 2 + 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index fee53b1..e8301f5 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -1,5 +1,8 @@ package com.MeetMate.company; +import com.MeetMate.enums.UserRole; +import com.MeetMate.response.GetResponse; +import com.MeetMate.user.User; import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.graphql.data.method.annotation.Argument; @@ -75,7 +78,7 @@ public ResponseEntity editCompany( 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.BAD_REQUEST).body("message: " + t.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); } @@ -100,28 +103,45 @@ public ResponseEntity deleteCompany(@ContextValue String token) { } } - @MutationMapping - public ResponseEntity addMember( + ////////////////MEMBER MANAGEMENT//////////////// + + @QueryMapping + public GetResponse getMember( @ContextValue String token, - @Argument String memberEmail, - @Argument String memberName, - @Argument String memberPassword) { + @Argument long memberId + ) { token = token.substring(7); try { - companyService.addMember(token, memberEmail, memberName, memberPassword); - return ResponseEntity.ok().build(); + return companyService.getMember(token, memberId); } catch (Throwable t) { Class tc = t.getClass(); + return null; + } + } - if (tc == EntityNotFoundException.class) - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); + @MutationMapping + public ResponseEntity addMember ( + @ContextValue String token, + @Argument String memberEmail, + @Argument String memberName, + @Argument String memberPassword){ + token = token.substring(7); + try { + companyService.addMember(token, memberEmail, memberName, memberPassword); + return ResponseEntity.ok().build(); - if (tc == IllegalArgumentException.class) - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); + } catch (Throwable t) { + Class tc = t.getClass(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); + 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()); + } } - } -} + } diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index 0dfd806..ed38b27 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -3,7 +3,9 @@ import com.MeetMate.company.sequence.CompanySequenceService; import com.MeetMate.enums.BusinessType; import com.MeetMate.enums.UserRole; +import com.MeetMate.response.GetResponse; import com.MeetMate.security.JwtService; +import com.MeetMate.user.User; import com.MeetMate.user.UserController; import com.MeetMate.user.UserRepository; import jakarta.persistence.EntityNotFoundException; @@ -73,6 +75,17 @@ public void deleteCompany(String token) { companyRepository.delete(company); } + public GetResponse getMember(String token, long memberId) { + Company company = getCompanyWithToken(token); + + GetResponse member = getMemberById(memberId); + + if (!company.getMemberEmails().contains(member.getEmail())) + throw new EntityNotFoundException("Member not found"); + + return member; + } + @Transactional public void addMember(String token, String memberEmail, String memberName, String memberPassword) { Company company = getCompanyWithToken(token); @@ -102,4 +115,18 @@ private Company getCompanyWithToken(String token) throws IllegalArgumentExceptio return companyRepository.findCompanyByOwnerEmail(ownerEmail) .orElseThrow(() -> new EntityNotFoundException("Company not found")); } + + public GetResponse getMemberById(long id) { + User user = userRepository.findUserById(id) + .orElseThrow(() -> new EntityNotFoundException("User does not exist")); + + return GetResponse.builder() + .id(user.getId()) + .name(user.getName()) + .created_at(user.getCreatedAt()) + .email(user.getEmail()) + .role(user.getRole()) + .associatedCompany(user.getAssociatedCompany()) + .build(); + } } diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index e6ca2cb..465939d 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -25,7 +25,7 @@ public class UserController { public ResponseEntity getUser(@RequestHeader(name = "Authorization") String token) { token = token.substring(7); try { - return ResponseEntity.ok(userService.getUserByEmail(token)); + return ResponseEntity.ok(userService.getUser(token)); } catch (Throwable t) { Class tc = t.getClass(); diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 188a70a..3eb1d84 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -27,9 +27,8 @@ public class UserService { private final PasswordEncoder passwordEncoder; private final AuthenticationManager authenticationManager; - public GetResponse getUserByEmail(String token) { + public GetResponse getUser(String token) { String email = jwtService.extractUserEmail(token); - User user = userRepository.findUserByEmail(email) .orElseThrow(() -> new EntityNotFoundException("User does not exist")); diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index f5137ea..58c9451 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -5,6 +5,8 @@ type Query { #Company Queries getCompany(id: ID!): Company + getMember(memberId: ID!): String + #Appointment Queries getAppointment(id: ID!): Appointment } From b710f617bfde7d3aa94a1006f33bce7e9ca20b42 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 19:45:32 +0200 Subject: [PATCH 5/8] Added get all members method --- .../java/com/MeetMate/company/Company.java | 2 +- .../MeetMate/company/CompanyController.java | 17 ++++++++++++++ .../com/MeetMate/company/CompanyService.java | 22 ++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/MeetMate/company/Company.java b/src/main/java/com/MeetMate/company/Company.java index 3a2040f..15df562 100644 --- a/src/main/java/com/MeetMate/company/Company.java +++ b/src/main/java/com/MeetMate/company/Company.java @@ -16,7 +16,7 @@ public class Company { private String name; private String description; private BusinessType businessType; - private ArrayList memberEmails; + private ArrayList memberIds; @Indexed(unique = true) private String ownerEmail; diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index e8301f5..5c2a728 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import java.lang.reflect.InaccessibleObjectException; +import java.util.ArrayList; @Controller @RequestMapping(path = "api/company") @@ -120,6 +121,19 @@ public GetResponse getMember( } } + public ArrayList getAllMembers( + @ContextValue String token + ) { + token = token.substring(7); + try { + return companyService.getAllMembers(token); + + } catch (Throwable t) { + Class tc = t.getClass(); + return null; + } + } + @MutationMapping public ResponseEntity addMember ( @ContextValue String token, @@ -140,6 +154,9 @@ public ResponseEntity addMember ( if (tc == IllegalArgumentException.class) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage()); + if (tc == IllegalStateException.class) + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).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 ed38b27..9381971 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -19,6 +19,8 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import java.util.ArrayList; + @Service @RequiredArgsConstructor public class CompanyService { @@ -80,12 +82,23 @@ public GetResponse getMember(String token, long memberId) { GetResponse member = getMemberById(memberId); - if (!company.getMemberEmails().contains(member.getEmail())) + if (!company.getMemberIds().contains(member.getId())) throw new EntityNotFoundException("Member not found"); return member; } + public ArrayList getAllMembers(String token) { + Company company = getCompanyWithToken(token); + + ArrayList members = new ArrayList<>(); + for (Long memberId : company.getMemberIds()) { + GetResponse member = getMemberById(memberId); + members.add(member); + } + return members; + } + @Transactional public void addMember(String token, String memberEmail, String memberName, String memberPassword) { Company company = getCompanyWithToken(token); @@ -101,7 +114,10 @@ public void addMember(String token, String memberEmail, String memberName, Strin Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail())); Update update = new Update(); - update.set("memberEmails", company.getMemberEmails().add(memberEmail)); + update.set("memberEmails", company.getMemberIds().add( + userRepository.findUserByEmail(memberEmail) + .orElseThrow(() -> new IllegalStateException("Member could not be created correctly!")) + .getId())); mongoTemplate.updateFirst(query, update, Company.class); } @@ -116,7 +132,7 @@ private Company getCompanyWithToken(String token) throws IllegalArgumentExceptio .orElseThrow(() -> new EntityNotFoundException("Company not found")); } - public GetResponse getMemberById(long id) { + private GetResponse getMemberById(long id) { User user = userRepository.findUserById(id) .orElseThrow(() -> new EntityNotFoundException("User does not exist")); From 61e9e46824506beaf78ddc707c58b277d39b37de Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 14 Oct 2024 13:48:17 +0200 Subject: [PATCH 6/8] added delete member method --- .../MeetMate/company/CompanyController.java | 64 +++++++++++++------ .../com/MeetMate/company/CompanyService.java | 14 ++++ .../com/MeetMate/user/UserRepository.java | 3 + src/main/resources/graphql/schema.graphqls | 4 ++ 4 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index 5c2a728..e71c4fb 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -134,31 +134,55 @@ public ArrayList getAllMembers( } } - @MutationMapping - public ResponseEntity addMember ( - @ContextValue String token, - @Argument String memberEmail, - @Argument String memberName, - @Argument String memberPassword){ - token = token.substring(7); - try { - companyService.addMember(token, memberEmail, memberName, memberPassword); - return ResponseEntity.ok().build(); - - } catch (Throwable t) { - Class tc = t.getClass(); + @MutationMapping + public ResponseEntity addMember( + @ContextValue String token, + @Argument String memberEmail, + @Argument String memberName, + @Argument String memberPassword) { + token = token.substring(7); + try { + companyService.addMember(token, memberEmail, memberName, memberPassword); + return ResponseEntity.ok().build(); - if (tc == EntityNotFoundException.class) - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); + } catch (Throwable t) { + Class tc = t.getClass(); - if (tc == IllegalArgumentException.class) - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage()); + if (tc == EntityNotFoundException.class) + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); - if (tc == IllegalStateException.class) - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); + if (tc == IllegalArgumentException.class) + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage()); + if (tc == IllegalStateException.class) return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); - } + + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); } + } + + @MutationMapping + public ResponseEntity deleteMember( + @ContextValue String token, + @Argument long memberId + ) { + token = token.substring(7); + try { + companyService.deleteMember(token, memberId); + return ResponseEntity.ok().build(); + } catch (Throwable t) { + Class 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.BAD_REQUEST).body("message: " + t.getMessage()); + + if (tc == IllegalAccessException.class) + return ResponseEntity.status(HttpStatus.FORBIDDEN).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 9381971..1b158bb 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -121,6 +121,16 @@ public void addMember(String token, String memberEmail, String memberName, Strin mongoTemplate.updateFirst(query, update, Company.class); } + @Transactional + public void deleteMember(String token, long memberId) throws IllegalAccessException { + Company company = getCompanyWithToken(token); + + if (!isCompanyMember(company, memberId)) + throw new IllegalAccessException("Not a member of the company"); + + userRepository.deleteById(memberId); + } + private Company getCompanyWithToken(String token) throws IllegalArgumentException { String ownerEmail = jwtService.extractUserEmail(token); if (userRepository.findUserByEmail(ownerEmail) @@ -132,6 +142,10 @@ 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 GetResponse getMemberById(long id) { User user = userRepository.findUserById(id) .orElseThrow(() -> new EntityNotFoundException("User does not exist")); diff --git a/src/main/java/com/MeetMate/user/UserRepository.java b/src/main/java/com/MeetMate/user/UserRepository.java index 8991ccb..b0d0aa2 100644 --- a/src/main/java/com/MeetMate/user/UserRepository.java +++ b/src/main/java/com/MeetMate/user/UserRepository.java @@ -1,5 +1,6 @@ package com.MeetMate.user; +import org.jetbrains.annotations.NotNull; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -15,4 +16,6 @@ public interface UserRepository extends JpaRepository { Optional findUserById(Long id); void deleteByEmail(String email); + + void deleteById(@NotNull Long id); } diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index 58c9451..08ad5d1 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -35,6 +35,10 @@ type Mutation { memberPassword: String! ): String + deleteMember( + memberId: ID! + ): String + #Appointment Mutations createAppointment( from: String, From 630a97a9bc4d797cb80e2c1ae866ee01337ead44 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 15 Oct 2024 21:27:52 +0200 Subject: [PATCH 7/8] Fixed members not being added correctly and in return the getMember method not working --- src/main/java/com/MeetMate/company/Company.java | 4 +++- .../com/MeetMate/company/CompanyController.java | 8 +++++--- .../com/MeetMate/company/CompanyService.java | 15 ++++++++++----- src/main/java/com/MeetMate/user/User.java | 1 + src/main/resources/graphql/schema.graphqls | 16 ++++++++++++++-- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/MeetMate/company/Company.java b/src/main/java/com/MeetMate/company/Company.java index 15df562..ce529b1 100644 --- a/src/main/java/com/MeetMate/company/Company.java +++ b/src/main/java/com/MeetMate/company/Company.java @@ -20,11 +20,13 @@ public class Company { @Indexed(unique = true) private String ownerEmail; - public Company(long id,String name, String ownerEmail) { + public Company(long id,String name, String ownerEmail, long ownerId) { this.id = id; this.name = name; this.ownerEmail = ownerEmail; this.description = ""; this.businessType = null; + this.memberIds = new ArrayList<>(); + this.memberIds.add(ownerId); } } diff --git a/src/main/java/com/MeetMate/company/CompanyController.java b/src/main/java/com/MeetMate/company/CompanyController.java index e71c4fb..588f159 100644 --- a/src/main/java/com/MeetMate/company/CompanyController.java +++ b/src/main/java/com/MeetMate/company/CompanyController.java @@ -31,7 +31,7 @@ public Company getCompany(@Argument long id) { } catch (Throwable t) { Class tc = t.getClass(); - return new Company(-1, "error", "error"); + return new Company(-1, "error", "error", -1); // if (tc == EntityNotFoundException.class) // return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); @@ -113,14 +113,16 @@ public GetResponse getMember( ) { token = token.substring(7); try { - return companyService.getMember(token, memberId); - + GetResponse g = companyService.getMember(token, memberId); + System.out.println(g.toString()); + return g; } catch (Throwable t) { Class tc = t.getClass(); return null; } } + @QueryMapping public ArrayList getAllMembers( @ContextValue String token ) { diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index 1b158bb..951b386 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -49,8 +49,11 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam ownerData.add("associatedCompany", String.valueOf(companyId)); userController.registerNewUser(ownerData); + long ownerId = userRepository.findUserByEmail(ownerEmail) + .orElseThrow(() -> new IllegalStateException("Owner could not be created correctly!")) + .getId(); - companyRepository.save(new Company(companyId, companyName, ownerEmail)); + companyRepository.save(new Company(companyId, companyName, ownerEmail, ownerId)); companySequenceService.incrementId(); } @@ -114,10 +117,12 @@ public void addMember(String token, String memberEmail, String memberName, Strin Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail())); Update update = new Update(); - update.set("memberEmails", company.getMemberIds().add( - userRepository.findUserByEmail(memberEmail) - .orElseThrow(() -> new IllegalStateException("Member could not be created correctly!")) - .getId())); + ArrayList a = company.getMemberIds(); + a.add(userRepository.findUserByEmail(memberEmail) + .orElseThrow(() -> new IllegalStateException("Member could not be created correctly!")) + .getId()); + update.set("memberIds", a); + mongoTemplate.updateFirst(query, update, Company.class); } diff --git a/src/main/java/com/MeetMate/user/User.java b/src/main/java/com/MeetMate/user/User.java index c8f7850..f583b4d 100644 --- a/src/main/java/com/MeetMate/user/User.java +++ b/src/main/java/com/MeetMate/user/User.java @@ -40,6 +40,7 @@ public User(Long id, String name, String email, String password) { this.email = email; this.password = password; this.createdAt = LocalDate.now(); + subscribedCompanies = new ArrayList<>(); } @Experimentational diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index 08ad5d1..bc38821 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -5,7 +5,9 @@ type Query { #Company Queries getCompany(id: ID!): Company - getMember(memberId: ID!): String + getMember(memberId: ID!): GetResponse + + getAllMembers: GetResponse #Appointment Queries getAppointment(id: ID!): Appointment @@ -74,7 +76,7 @@ type Company { name: String description: String businessType: String - memberEmails: [String] + memberIds: [ID] ownerEmail: String } @@ -89,4 +91,14 @@ type Appointment { description: String location: String Status: String +} + +type GetResponse { + id: ID! + name: String + created_at: String + email: String + role: String + associatedCompany: ID + subscribedCompanies: [ID] } \ No newline at end of file From 8fcf9caa7e0fb13b9f3842a43d7b8513cb0148fe Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Oct 2024 00:38:54 +0200 Subject: [PATCH 8/8] Fixed getAllMembers mathos not returning as array --- src/main/java/com/MeetMate/company/CompanyService.java | 3 ++- src/main/resources/graphql/schema.graphqls | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index 951b386..45dad12 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -93,12 +93,13 @@ public GetResponse getMember(String token, long 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; } diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index bc38821..1be71d8 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -7,7 +7,7 @@ type Query { getMember(memberId: ID!): GetResponse - getAllMembers: GetResponse + getAllMembers: [GetResponse] #Appointment Queries getAppointment(id: ID!): Appointment @@ -94,7 +94,7 @@ type Appointment { } type GetResponse { - id: ID! + id: ID name: String created_at: String email: String