Skip to content

Commit

Permalink
Merge pull request #234 from haedoang/feature/liked
Browse files Browse the repository at this point in the history
[feature/profile] 프로필 조회/수정
  • Loading branch information
haedoang authored Oct 18, 2023
2 parents aa15970 + 8f415d3 commit 0043ba5
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import com.koliving.api.location.domain.Location;
import com.koliving.api.location.domain.LocationType;
import com.querydsl.core.annotations.QueryProjection;


public record LocationResponse(Long id, Long upperLocationId, String name, String displayName,
LocationType locationType) {

@QueryProjection
public LocationResponse {
}

public static LocationResponse valueOf(Location entity) {
return new LocationResponse(
entity.getId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.koliving.api.my.application.dto;

import com.koliving.api.file.domain.ImageFile;
import com.koliving.api.user.Gender;
import com.koliving.api.user.User;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;

@Schema(description = "작성자 정보")
public record UserProfileUpdateRequest(

@NotNull
@Schema(description = "이미지 URL 고유 Key")
Long profileId,

@NotNull
@Schema(description = "성별")
Gender gender,

@NotNull
@Schema(description = "이름")
String firstName,

@NotNull
@Schema(description = "성")
String lastName,

@NotNull
@Schema(description = "생년월일")
LocalDate birthDate,

@Nullable
@Schema(description = "설명")
String description
) {
public User toUser(ImageFile imageFile) {
return User.of(imageFile, gender, firstName, lastName, birthDate, description);
}
}
73 changes: 73 additions & 0 deletions src/main/java/com/koliving/api/my/ui/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.koliving.api.my.ui;

import com.koliving.api.base.ErrorResponse;
import com.koliving.api.my.application.dto.UserProfileUpdateRequest;
import com.koliving.api.user.User;
import com.koliving.api.user.application.UserService;
import com.koliving.api.user.application.dto.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "MY API", description = "MY API")
@RestController
@RequestMapping("api/v1/my")
@RequiredArgsConstructor
public class MyController {
private final UserService userService;

@Operation(
summary = "프로필 수정",
description = "프로필을 수정합니다.",
responses = {
@ApiResponse(
responseCode = "204",
description = "프로필 수정 성공"
),
@ApiResponse(
responseCode = "400",
description = "프로필 수정 실패",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
),
})
@PutMapping("/profile")
public ResponseEntity<Void> updateProfile(@RequestBody UserProfileUpdateRequest request, @AuthenticationPrincipal User user) {
userService.updateProfile(request, user.getId());
return ResponseEntity.noContent().build();
}


@Operation(
summary = "프로필 조회",
description = "프로필 정보를 조회합니다",
responses = {
@ApiResponse(
responseCode = "200",
description = "프로필 조회 성공",
content = @Content(schema = @Schema(implementation = UserResponse.class))
),
@ApiResponse(
responseCode = "400",
description = "프로필 조회 실패",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
),
})
@GetMapping
public ResponseEntity<UserResponse> myProfile(@AuthenticationPrincipal User user) {
UserResponse response = userService.findById(user.getId());

return ResponseEntity.ok()
.body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import static com.koliving.api.base.ServiceError.FORBIDDEN;
import static com.koliving.api.base.ServiceError.RECORD_NOT_EXIST;
import static com.koliving.api.base.ServiceError.UNAUTHORIZED;

/**
* author : haedoang date : 2023/08/26 description :
Expand Down Expand Up @@ -97,7 +96,7 @@ private Location getLocationById(Long locationId) {
return location;
}

public Page<Room> search(Pageable pageable, RoomSearchCondition condition) {
public Page<RoomResponse> search(Pageable pageable, RoomSearchCondition condition) {
return roomRepository.search(pageable, condition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import com.koliving.api.room.domain.Room;
import com.koliving.api.room.domain.info.RoomInfo;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* author : haedoang date : 2023/08/26 description :
Expand Down Expand Up @@ -48,11 +46,10 @@ public record RoomResponse(
String description,

@Schema(description = "유저 정보")
MockUser mockUser,
WriterResponse user,

@Schema(description = "방 이미지 정보")
Set<ImageFile> images

) {

public static RoomResponse valueOf(Room entity) {
Expand All @@ -66,21 +63,8 @@ public static RoomResponse valueOf(Room entity) {
entity.getFurnishings(),
entity.getAvailableDate(),
entity.getDescription(),
new MockUser(),
WriterResponse.of(entity.getUser()),
entity.getImageFiles()
);
}

@Getter
public static class MockUser {
private final String firstName = "NAMI";
private final String lastName = "OH";
private final String profileImage = "https://cdn-icons-png.flaticon.com/512/3135/3135823.png";
private final int age = 30;
private final Gender gender = Gender.FEMALE;
}

public enum Gender {
MALE, FEMALE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.koliving.api.room.application.dto;

import com.koliving.api.user.Gender;
import com.koliving.api.user.User;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDate;
import java.util.Objects;

@Schema(description = "작성자 정보")
public record WriterResponse(
@Schema(description = "작성자 이름")
String firstName,
@Schema(description = "작성자 성")
String lastName,

@Schema(description = "작성자 성별")
Gender gender,
@Schema(description = "작성자 생년월일")
LocalDate birthDate,

@Schema(description = "작성자 소개")
String description,

@Schema(description = "작성자 프로필 URL")
String imageUrl
) {

public static WriterResponse of(User entity) {
String imageUrl = Objects.isNull(entity.getImageFile()) ? null : entity.getImageFile().getPath();
return new WriterResponse(entity.getFirstName(), entity.getLastName(), entity.getGender(), entity.getBirthDate(), entity.getDescription(), imageUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.koliving.api.room.infra;

import com.koliving.api.room.application.dto.RoomResponse;
import com.koliving.api.room.application.dto.RoomSearchCondition;
import com.koliving.api.room.domain.FurnishingType;
import com.koliving.api.room.domain.Room;
Expand All @@ -15,6 +16,7 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.koliving.api.room.domain.QRoom.room;

Expand All @@ -23,7 +25,7 @@ public class RoomRepositoryImpl implements RoomRepositoryQueryDsl {
private final JPAQueryFactory queryFactory;

@Override
public Page<Room> search(Pageable pageable, RoomSearchCondition condition) {
public Page<RoomResponse> search(Pageable pageable, RoomSearchCondition condition) {
List<Room> rooms = queryFactory.selectFrom(room)
.where(
filterByLocationIds(condition.locationIds()),
Expand All @@ -50,8 +52,9 @@ public Page<Room> search(Pageable pageable, RoomSearchCondition condition) {
.fetch()
.size();


return PageableExecutionUtils.getPage(rooms, pageable, () -> count);
return PageableExecutionUtils.getPage(rooms.stream()
.map(RoomResponse::valueOf)
.collect(Collectors.toList()), pageable, () -> count);
}

private BooleanExpression filterByFurnishings(List<FurnishingType> furnishingTypes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.koliving.api.room.infra;

import com.koliving.api.room.application.dto.RoomResponse;
import com.koliving.api.room.application.dto.RoomSearchCondition;
import com.koliving.api.room.domain.Room;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface RoomRepositoryQueryDsl {
Page<Room> search(Pageable pageable, RoomSearchCondition condition);
Page<RoomResponse> search(Pageable pageable, RoomSearchCondition condition);
}
2 changes: 1 addition & 1 deletion src/main/java/com/koliving/api/room/ui/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ResponseEntity<Long> save(@RequestBody RoomSaveRequest request, @Authenti
),
})
@PostMapping("/search")
public ResponseEntity<Page<Room>> search(@ParameterObject @PageableDefault Pageable pageable, @ParameterObject RoomSearchCondition condition) {
public ResponseEntity<Page<RoomResponse>> search(@ParameterObject @PageableDefault Pageable pageable, @ParameterObject RoomSearchCondition condition) {
return ResponseEntity.ok()
.body(roomService.search(pageable, condition));
}
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/com/koliving/api/user/User.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.koliving.api.user;

import com.koliving.api.base.exception.KolivingServiceException;
import com.koliving.api.file.domain.ImageFile;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import lombok.AccessLevel;
Expand All @@ -25,7 +29,6 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.swing.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
Expand Down Expand Up @@ -61,10 +64,12 @@ public class User implements UserDetails {
@Column(name = "BIRTH_DATE")
private LocalDate birthDate;

@Lob
private String description;

@Column
private String imageUrl;
@OneToOne
@JoinColumn(name = "IMAGE_FILE_ID")
private ImageFile imageFile;

@Enumerated(EnumType.STRING)
@Column(name = "USER_ROLE")
Expand Down Expand Up @@ -95,16 +100,26 @@ public User(String email) {
this.userRole = UserRole.USER;
}

private User(String email, String password, UserRole userRole) {
private User(String email, String password, String firstName, String lastName, Gender gender, LocalDate birthDate, String description, ImageFile imageFile, UserRole userRole) {
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.birthDate = birthDate;
this.description = description;
this.imageFile = imageFile;
this.userRole = userRole;
}

@Deprecated
public static User valueOf(String email, String encodedPassword, UserRole role) {
return new User(email, encodedPassword, role);
return new User(email, encodedPassword, null, null, null, null, null,null, role);
}

public static User of(ImageFile imageFile, Gender gender, String firstName, String lastName, LocalDate birthDate, String description) {
return new User(null, null, firstName, lastName, gender, birthDate,description, imageFile, null);
}
public void setPassword(String password) {
this.password = password;
this.signUpStatus = SignUpStatus.PROFILE_INFORMATION_PENDING;
Expand Down Expand Up @@ -159,4 +174,13 @@ public void checkPassword(PasswordEncoder passwordEncoder, String rawPassword) {
throw new KolivingServiceException(UNAUTHORIZED);
}
}

public void update(User updatable) {
this.imageFile = updatable.imageFile;
this.firstName = updatable.firstName;
this.lastName = updatable.lastName;
this.gender = updatable.gender;
this.birthDate = updatable.birthDate;
this.description = updatable.description;
}
}
Loading

0 comments on commit 0043ba5

Please sign in to comment.