Skip to content

Commit

Permalink
Merge pull request #448 from bounswe/feature/be-447-get-update-profile
Browse files Browse the repository at this point in the history
Feature/be 447 get update profile
  • Loading branch information
alitpc25 authored Nov 26, 2023
2 parents c1c9dc6 + 8b3fb05 commit 7e6c973
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 3 deletions.
5 changes: 5 additions & 0 deletions resq/backend/resq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<version>0.12.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
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
Expand Down Expand Up @@ -81,4 +81,5 @@ public String victimAccess() {
public String coordinatorAccess() {
return "Coordinator Board.";
}

}
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);

}

}
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;
}

}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;


}
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> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class AuthService {
@Autowired
PasswordEncoder encoder;

@Autowired
UserProfileService userProfileService;

@Autowired
JwtUtils jwtUtils;

Expand All @@ -53,6 +57,12 @@ public ResponseEntity<String> 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!");
Expand Down
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.";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.Set;

Expand Down
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);
}
}

0 comments on commit 7e6c973

Please sign in to comment.