Skip to content

Commit

Permalink
Merge pull request #303 from bounswe/backend/feature/302-put-user-pro…
Browse files Browse the repository at this point in the history
…file

feat(backend): update profile details
  • Loading branch information
atakanyasar authored May 17, 2024
2 parents f6a814b + 5dffb0b commit 7210ab2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor


public class UserProfileDto {
private Integer id;
private String username;
Expand All @@ -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<String> diets;
private Integer recipeCount;
private List<BookmarkDto> bookmarks;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions backend/src/main/java/com/group1/cuisines/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 34 additions & 12 deletions backend/src/main/java/com/group1/cuisines/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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();
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 7210ab2

Please sign in to comment.