From 43de94e521aedd4879cfca53a43ff17c93adb6c3 Mon Sep 17 00:00:00 2001 From: furknbulbul Date: Sat, 25 Nov 2023 14:34:03 +0300 Subject: [PATCH 1/2] profile dto added --- .../resq/controller/UserController.java | 19 ++++++++++ .../resq/converter/ProfileConverter.java | 25 +++++++++++++ .../java/com/groupa1/resq/dto/ProfileDto.java | 36 +++++++++++++++++++ .../com/groupa1/resq/entity/UserProfile.java | 12 +++++++ .../com/groupa1/resq/service/UserService.java | 6 ++++ 5 files changed, 98 insertions(+) create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/converter/ProfileConverter.java create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/dto/ProfileDto.java diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java index 707447b7..be70dbca 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java @@ -1,7 +1,9 @@ package com.groupa1.resq.controller; import com.groupa1.resq.config.ResqAppProperties; +import com.groupa1.resq.converter.ProfileConverter; import com.groupa1.resq.converter.UserConverter; +import com.groupa1.resq.dto.ProfileDto; import com.groupa1.resq.dto.UserDto; import com.groupa1.resq.entity.User; import com.groupa1.resq.service.UserService; @@ -26,6 +28,8 @@ public class UserController { @Autowired private UserConverter userConverter; + @Autowired + private ProfileConverter profileConverter; @PostMapping("/requestRole") public String requestRole(@RequestParam Long userId, @RequestParam String role) { log.info("Requested role: {} requested for user: {}", role, userId); @@ -81,4 +85,19 @@ public String victimAccess() { public String coordinatorAccess() { return "Coordinator Board."; } + + @GetMapping("getProfileInfo") + @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") + public ProfileDto getProfileInfo(@RequestParam Long userId) { + log.info("Get profile info requested for userId : {}", userId); + return profileConverter.convertToDto(userService.findById(userId).getUserProfile()); + } + +// @PostMapping("/updateProfile") +// @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") +// public String updateProfile(@RequestParam Long userId, @RequestBody ProfileDto profileDto) { +// log.info("Updating profile for user: {}", userId); +// userService.updateProfile(userId, profileDto); +// return "Profile successfully updated."; +// } } diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/converter/ProfileConverter.java b/resq/backend/resq/src/main/java/com/groupa1/resq/converter/ProfileConverter.java new file mode 100644 index 00000000..339672ad --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/converter/ProfileConverter.java @@ -0,0 +1,25 @@ +package com.groupa1.resq.converter; + +import com.groupa1.resq.dto.ProfileDto; +import com.groupa1.resq.entity.UserProfile; +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ProfileConverter { + + @Autowired + ModelMapper modelMapper; + + public ProfileDto convertToDto(UserProfile userProfile) { + ProfileDto profileDto = modelMapper.map(userProfile, ProfileDto.class); + return profileDto; + } + + public UserProfile convertToEntity(ProfileDto profileDto) { + UserProfile userProfile = modelMapper.map(profileDto, UserProfile.class); + return userProfile; + } + +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/dto/ProfileDto.java b/resq/backend/resq/src/main/java/com/groupa1/resq/dto/ProfileDto.java new file mode 100644 index 00000000..6867a3e7 --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/dto/ProfileDto.java @@ -0,0 +1,36 @@ +package com.groupa1.resq.dto; + + +import com.groupa1.resq.entity.enums.EGender; +import lombok.Data; + +import java.time.LocalDate; + +@Data +public class ProfileDto { + + private String name; + private String surname; + + private LocalDate birth_date; + + private EGender gender; + + private boolean isEmailConfirmed; + + private String phoneNumber; + + private boolean isPrivacyPolicyAccepted; + + private String Country; + + private String City; + + private String State; + + private String bloodType; + + private Integer weight; + + private Integer height; +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java b/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java index 6b11b992..22769a19 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java @@ -32,5 +32,17 @@ public class UserProfile extends BaseEntity{ private boolean isPrivacyPolicyAccepted; + private String Country; + + private String City; + + private String State; + + private String bloodType; + + private Integer weight; + + private Integer height; + } diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java index eca984d7..301adbbe 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java @@ -1,5 +1,6 @@ package com.groupa1.resq.service; +import com.groupa1.resq.dto.ProfileDto; import com.groupa1.resq.entity.User; import com.groupa1.resq.entity.enums.EUserRole; import com.groupa1.resq.exception.EntityNotFoundException; @@ -7,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.PostMapping; import java.util.Optional; import java.util.Set; @@ -42,4 +44,8 @@ public User requestRole(Long userId, String role) { public User findById(Long userId) { return userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException("User not found")); } + + + + } From 8b3fb0593e0dd68775c5182ecd070ab61f9c171c Mon Sep 17 00:00:00 2001 From: furknbulbul Date: Sat, 25 Nov 2023 17:42:51 +0300 Subject: [PATCH 2/2] get profile info and update profile info implemented --- resq/backend/resq/pom.xml | 5 ++ .../resq/controller/UserController.java | 20 +------ .../controller/UserProfileController.java | 54 ++++++++++++++++++ .../com/groupa1/resq/entity/UserProfile.java | 2 +- .../repository/UserProfileRepository.java | 8 +++ .../com/groupa1/resq/service/AuthService.java | 10 ++++ .../resq/service/UserProfileService.java | 57 +++++++++++++++++++ .../com/groupa1/resq/service/UserService.java | 7 --- .../resq/util/NullAwareBeanUtilsBean.java | 18 ++++++ 9 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserProfileController.java create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/repository/UserProfileRepository.java create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/service/UserProfileService.java create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/util/NullAwareBeanUtilsBean.java diff --git a/resq/backend/resq/pom.xml b/resq/backend/resq/pom.xml index dd2bae06..7eebeb48 100644 --- a/resq/backend/resq/pom.xml +++ b/resq/backend/resq/pom.xml @@ -77,6 +77,11 @@ 0.12.3 runtime + + commons-beanutils + commons-beanutils + 1.9.4 + diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java index be70dbca..fff43b5a 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserController.java @@ -1,18 +1,16 @@ package com.groupa1.resq.controller; import com.groupa1.resq.config.ResqAppProperties; -import com.groupa1.resq.converter.ProfileConverter; import com.groupa1.resq.converter.UserConverter; -import com.groupa1.resq.dto.ProfileDto; import com.groupa1.resq.dto.UserDto; import com.groupa1.resq.entity.User; import com.groupa1.resq.service.UserService; import lombok.extern.slf4j.Slf4j; -import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; + @CrossOrigin(origins = "*", maxAge = 3600) @RestController @Slf4j @@ -28,8 +26,6 @@ public class UserController { @Autowired private UserConverter userConverter; - @Autowired - private ProfileConverter profileConverter; @PostMapping("/requestRole") public String requestRole(@RequestParam Long userId, @RequestParam String role) { log.info("Requested role: {} requested for user: {}", role, userId); @@ -86,18 +82,4 @@ public String coordinatorAccess() { return "Coordinator Board."; } - @GetMapping("getProfileInfo") - @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") - public ProfileDto getProfileInfo(@RequestParam Long userId) { - log.info("Get profile info requested for userId : {}", userId); - return profileConverter.convertToDto(userService.findById(userId).getUserProfile()); - } - -// @PostMapping("/updateProfile") -// @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") -// public String updateProfile(@RequestParam Long userId, @RequestBody ProfileDto profileDto) { -// log.info("Updating profile for user: {}", userId); -// userService.updateProfile(userId, profileDto); -// return "Profile successfully updated."; -// } } diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserProfileController.java b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserProfileController.java new file mode 100644 index 00000000..8f21fa4c --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserProfileController.java @@ -0,0 +1,54 @@ +package com.groupa1.resq.controller; + +import com.groupa1.resq.converter.ProfileConverter; +import com.groupa1.resq.dto.ProfileDto; +import com.groupa1.resq.service.UserProfileService; +import com.groupa1.resq.service.UserService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.lang.reflect.InvocationTargetException; + + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@Slf4j +@RequestMapping("/profile") +public class UserProfileController { + + @Autowired + private UserProfileService userProfileService; + + @Autowired + private ProfileConverter profileConverter; + + @Autowired + private UserService userService; + + + @GetMapping("/getProfileInfo") + @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") + public ProfileDto getProfileInfo(@RequestParam Long userId) { + log.info("Get profile info requested for userId : {}", userId); + return profileConverter.convertToDto(userService.findById(userId).getUserProfile()); + } + + + @PostMapping("/updateProfile") + @PreAuthorize("hasRole('FACILITATOR') or hasRole('COORDINATOR') or hasRole('RESPONDER') or hasRole('VICTIM')") + public String updateProfile(@RequestParam Long userId, @RequestBody + ProfileDto profileDto) throws InvocationTargetException, IllegalAccessException{ + log.info("Updating profile for user: {}", userId); + return userProfileService.updateProfile(userId, profileDto); + + } + +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java b/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java index 22769a19..f80b3717 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/entity/UserProfile.java @@ -22,7 +22,7 @@ public class UserProfile extends BaseEntity{ @Enumerated(EnumType.STRING) private EGender gender; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "user_id") private User user; diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/repository/UserProfileRepository.java b/resq/backend/resq/src/main/java/com/groupa1/resq/repository/UserProfileRepository.java new file mode 100644 index 00000000..fa77064b --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/repository/UserProfileRepository.java @@ -0,0 +1,8 @@ +package com.groupa1.resq.repository; + +import com.groupa1.resq.entity.UserProfile; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserProfileRepository extends + JpaRepository { +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/service/AuthService.java b/resq/backend/resq/src/main/java/com/groupa1/resq/service/AuthService.java index d9750e97..4936a264 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/service/AuthService.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/service/AuthService.java @@ -2,6 +2,7 @@ import com.groupa1.resq.auth.UserDetailsImpl; import com.groupa1.resq.entity.User; +import com.groupa1.resq.entity.UserProfile; import com.groupa1.resq.entity.enums.EUserRole; import com.groupa1.resq.request.LoginUserRequest; import com.groupa1.resq.request.RegisterUserRequest; @@ -34,6 +35,9 @@ public class AuthService { @Autowired PasswordEncoder encoder; + @Autowired + UserProfileService userProfileService; + @Autowired JwtUtils jwtUtils; @@ -53,6 +57,12 @@ public ResponseEntity signup(RegisterUserRequest registerUserRequest) { // Each registered user has VICTIM role by default. roles.add(EUserRole.VICTIM); user.setRoles(roles); + UserProfile userProfile = new UserProfile(); + userProfile.setName(registerUserRequest.getName()); + userProfile.setSurname(registerUserRequest.getSurname()); + userProfile.setUser(user); + userProfileService.saveProfile(userProfile); + user.setUserProfile(userProfile); userService.save(user); return ResponseEntity.ok("User registered successfully!"); diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserProfileService.java b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserProfileService.java new file mode 100644 index 00000000..1482cf83 --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserProfileService.java @@ -0,0 +1,57 @@ +package com.groupa1.resq.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.groupa1.resq.converter.ProfileConverter; +import com.groupa1.resq.dto.ProfileDto; +import com.groupa1.resq.entity.User; +import com.groupa1.resq.entity.UserProfile; +import com.groupa1.resq.exception.EntityNotFoundException; +import com.groupa1.resq.repository.UserProfileRepository; +import com.groupa1.resq.repository.UserRepository; +import com.groupa1.resq.util.NullAwareBeanUtilsBean; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.lang.reflect.InvocationTargetException; + +@Service +@Slf4j +public class UserProfileService { + + @Autowired + private UserProfileRepository userProfileRepository; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private UserRepository userRepository; + + @Autowired + private NullAwareBeanUtilsBean beanUtils; + + @Autowired + private ProfileConverter profileConverter; + + + + + public void saveProfile(UserProfile userProfile) { + userProfileRepository.save(userProfile); + } + + public String updateProfile(Long userId, ProfileDto profileDto) + throws InvocationTargetException, IllegalAccessException { + + User user = userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException("User not found")); + UserProfile userProfile = user.getUserProfile(); + ProfileDto updatedProfileDto = objectMapper.convertValue(profileDto, ProfileDto.class); + UserProfile updatedProfile = profileConverter.convertToEntity(updatedProfileDto); + beanUtils.copyProperties(userProfile, updatedProfile); + user.setUserProfile(userProfile); + userRepository.save(user); + return "Profile successfully updated."; + + } +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java index 301adbbe..04697ad6 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/service/UserService.java @@ -1,6 +1,5 @@ package com.groupa1.resq.service; -import com.groupa1.resq.dto.ProfileDto; import com.groupa1.resq.entity.User; import com.groupa1.resq.entity.enums.EUserRole; import com.groupa1.resq.exception.EntityNotFoundException; @@ -8,8 +7,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.PostMapping; - import java.util.Optional; import java.util.Set; @@ -44,8 +41,4 @@ public User requestRole(Long userId, String role) { public User findById(Long userId) { return userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException("User not found")); } - - - - } diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/util/NullAwareBeanUtilsBean.java b/resq/backend/resq/src/main/java/com/groupa1/resq/util/NullAwareBeanUtilsBean.java new file mode 100644 index 00000000..10d5ef9a --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/util/NullAwareBeanUtilsBean.java @@ -0,0 +1,18 @@ +package com.groupa1.resq.util; + +import org.apache.commons.beanutils.BeanUtilsBean; +import org.springframework.stereotype.Component; + +import java.lang.reflect.InvocationTargetException; + +@Component +public class NullAwareBeanUtilsBean extends BeanUtilsBean { + + @Override + public void copyProperty(Object dest, String name, Object value) + throws IllegalAccessException, InvocationTargetException { + if (value == null) + return; + super.copyProperty(dest, name, value); + } +} \ No newline at end of file