diff --git a/backend/src/main/java/com/group1/cuisines/controllers/UserController.java b/backend/src/main/java/com/group1/cuisines/controllers/UserController.java index 5e6f941..0f17a25 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/UserController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/UserController.java @@ -4,6 +4,7 @@ import com.group1.cuisines.dao.response.SuccessResponse; import com.group1.cuisines.dto.UserDto; import com.group1.cuisines.dto.UserProfileDto; +import com.group1.cuisines.dto.UserUpdateFormDto; import com.group1.cuisines.entities.User; import com.group1.cuisines.repositories.UserRepository; import com.group1.cuisines.services.UserService; @@ -37,9 +38,22 @@ public ResponseEntity getUserById(@PathVariable Integer userId, Authenticatio String currentUsername = authentication != null ? authentication.getName() : null; try { UserProfileDto userProfile = userService.getUserProfileById(userId, currentUsername); - return ResponseEntity.ok(new SuccessResponse<>(200,userProfile, "User profile fetched successfully")); + return ResponseEntity.ok(new SuccessResponse<>(200, userProfile, "User profile fetched successfully")); } catch (EntityNotFoundException e) { - return ResponseEntity.ok(new ErrorResponse(204,"User not found")); + return ResponseEntity.ok(new ErrorResponse(204, "User not found")); + } + } + + @PutMapping("/{userId}") + public ResponseEntity updateUser(@PathVariable Integer userId, @RequestBody UserUpdateFormDto userUpdateFormDto) { + if (userId == null || userUpdateFormDto == null) { + return ResponseEntity.ok(new ErrorResponse(204,"Invalid user data")); + } + try { + UserProfileDto updatedUser = userService.updateUserProfile(userId, userUpdateFormDto); + return ResponseEntity.ok(new SuccessResponse<>(200, updatedUser, "User updated successfully")); + } catch (EntityNotFoundException e) { + return ResponseEntity.ok(new ErrorResponse(204, "User not found")); } } diff --git a/backend/src/main/java/com/group1/cuisines/dto/UserProfileDto.java b/backend/src/main/java/com/group1/cuisines/dto/UserProfileDto.java index 6bf6714..a132d31 100644 --- a/backend/src/main/java/com/group1/cuisines/dto/UserProfileDto.java +++ b/backend/src/main/java/com/group1/cuisines/dto/UserProfileDto.java @@ -4,10 +4,9 @@ import java.util.List; @Data +@Builder @NoArgsConstructor @AllArgsConstructor - - public class UserProfileDto { private Integer id; private String username; @@ -16,8 +15,8 @@ public class UserProfileDto { private boolean selfFollowing; private Integer followersCount; private Integer followingCount; - //private String gender; - // private String profilePicture; + private String gender; + private String profilePicture; // private List diets; private Integer recipeCount; private List bookmarks; diff --git a/backend/src/main/java/com/group1/cuisines/dto/UserUpdateFormDto.java b/backend/src/main/java/com/group1/cuisines/dto/UserUpdateFormDto.java new file mode 100644 index 0000000..5f0d94a --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/UserUpdateFormDto.java @@ -0,0 +1,19 @@ +package com.group1.cuisines.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class UserUpdateFormDto { + private String username; + private String firstName; + private String lastName; + private String bio; + private String gender; + private String profilePicture; +} diff --git a/backend/src/main/java/com/group1/cuisines/entities/User.java b/backend/src/main/java/com/group1/cuisines/entities/User.java index a483141..82ee30d 100644 --- a/backend/src/main/java/com/group1/cuisines/entities/User.java +++ b/backend/src/main/java/com/group1/cuisines/entities/User.java @@ -33,6 +33,9 @@ public class User implements UserDetails { private String password; private String Bio; + private String gender; + private String profilePicture; + @ManyToMany @JoinTable( name = "user_followers", diff --git a/backend/src/main/java/com/group1/cuisines/services/UserService.java b/backend/src/main/java/com/group1/cuisines/services/UserService.java index c3628cb..956abb8 100644 --- a/backend/src/main/java/com/group1/cuisines/services/UserService.java +++ b/backend/src/main/java/com/group1/cuisines/services/UserService.java @@ -6,6 +6,7 @@ import com.group1.cuisines.entities.User; import com.group1.cuisines.repositories.UserRepository; import jakarta.persistence.EntityNotFoundException; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -115,18 +116,21 @@ public UserProfileDto getUserProfileById(Integer userId, String currentUsername) boolean isSelf = user.getUsername().equals(currentUsername); - UserProfileDto profile = new UserProfileDto(); - profile.setId(user.getId()); - profile.setUsername(user.getUsername()); - profile.setName(user.getFirstName() + " " + user.getLastName()); - profile.setBio(user.getBio()); - profile.setSelfFollowing(isFollowing(currentUserId, userId)); - profile.setFollowersCount(user.getFollowers().size()); - profile.setFollowingCount(user.getFollowing().size()); - profile.setRecipeCount(user.getRecipes().size()); - profile.setRecipes(user.getRecipes().stream() - .map(recipeService::convertToRecipeDetailsDto) - .collect(Collectors.toList())); + UserProfileDto profile = UserProfileDto.builder() + .id(user.getId()) + .username(user.getUsername()) + .name(user.getFirstName() + " " + user.getLastName()) + .bio(user.getBio()) + .gender(user.getGender()) + .profilePicture(user.getProfilePicture()) + .selfFollowing(isFollowing(currentUserId, userId)) + .followersCount(user.getFollowers().size()) + .followingCount(user.getFollowing().size()) + .recipeCount(user.getRecipes().size()) + .recipes(user.getRecipes().stream() + .map(recipeService::convertToRecipeDetailsDto) + .collect(Collectors.toList())) + .build(); if (isSelf) { profile.setBookmarks(user.getBookmarks().stream() @@ -139,6 +143,22 @@ public UserProfileDto getUserProfileById(Integer userId, String currentUsername) return profile; } + @Transactional + public UserProfileDto updateUserProfile(Integer userId, UserUpdateFormDto profileDto) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new EntityNotFoundException("User not found")); + + user.setFirstName(profileDto.getFirstName()); + user.setLastName(profileDto.getLastName()); + user.setBio(profileDto.getBio()); + user.setGender(profileDto.getGender()); + user.setProfilePicture(profileDto.getProfilePicture()); + + userRepository.save(user); + + return getUserProfileById(userId, user.getUsername()); + } + private BookmarkDto convertToBookmarkDto(Bookmark bookmark) { Recipe recipe = bookmark.getRecipe(); @@ -189,6 +209,8 @@ public UserProfileDto getUserProfileDtoById(Integer userId) { profile.setUsername(user.getUsername()); profile.setName(user.getFirstName() + " " + user.getLastName()); profile.setBio(user.getBio()); + profile.setGender(user.getGender()); + profile.setProfilePicture(user.getProfilePicture()); profile.setFollowersCount(user.getFollowers().size()); profile.setFollowingCount(user.getFollowing().size());