-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #448 from bounswe/feature/be-447-get-update-profile
Feature/be 447 get update profile
- Loading branch information
Showing
11 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
resq/backend/resq/src/main/java/com/groupa1/resq/controller/UserProfileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
resq/backend/resq/src/main/java/com/groupa1/resq/converter/ProfileConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
resq/backend/resq/src/main/java/com/groupa1/resq/dto/ProfileDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
resq/backend/resq/src/main/java/com/groupa1/resq/repository/UserProfileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserProfile, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
resq/backend/resq/src/main/java/com/groupa1/resq/service/UserProfileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
resq/backend/resq/src/main/java/com/groupa1/resq/util/NullAwareBeanUtilsBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |