Skip to content

Commit

Permalink
Merge pull request #129 from ootd-zip/fix/get-ootds(#128)
Browse files Browse the repository at this point in the history
마이페이지 OOTD 조회수정 및 래퍼타입변경
  • Loading branch information
jinhoon227 authored Mar 3, 2024
2 parents 052fddb + a017980 commit 2085f43
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CommentGetAllRes {

private String taggedUserName;

private int depth;
private Integer depth;

private Long parentId;

Expand All @@ -39,10 +39,10 @@ public static CommentGetAllRes of(Comment comment) {
.userImage(comment.getWriter().getProfileImage())
.content(comment.getContents())
.timeStamp(comment.compareCreatedTimeAndNow())
.taggedUserName(comment.getTaggedUser() == null ? null : comment.getTaggedUser().getName())
.taggedUserName(comment.getTaggedUserName())
.depth(comment.getDepth())
.groupId(comment.getGroupId())
.parentId(comment.getParent() == null ? null : comment.getParent().getId())
.parentId(comment.getParentCommentId())
.build();
}
}
18 changes: 18 additions & 0 deletions src/main/java/zip/ootd/ootdzip/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,22 @@ public void deleteComment() {
parent.setChildCount(parent.getChildCount() - 1);
}
}

// == Custom Getter == //

public String getTaggedUserName() {
if (taggedUser == null) {
return "";
}

return taggedUser.getName();
}

public Long getParentCommentId() {
if (parent == null) {
return null;
}

return parent.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CommonSliceResponse<T> {

private Integer size = 30;

private boolean isLast;
private Boolean isLast;

public CommonSliceResponse(List<T> content, Pageable pageable, boolean isLast) {
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public ApiResponse<CommonSliceResponse<OotdGetSimilarRes>> getSimilarOotd(@Valid
@GetMapping("")
public ApiResponse<CommonSliceResponse<OotdGetByUserRes>> getUserOotd(@Valid OotdGetByUserReq request) {

CommonSliceResponse<OotdGetByUserRes> response = ootdService.getOotdByUser(request);
CommonSliceResponse<OotdGetByUserRes> response = ootdService.getOotdByUser(userService.getAuthenticatiedUser(),
request);

return new ApiResponse<>(response);
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdGetAllRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class OotdGetAllRes {

private Long id;

private boolean isLike;
private Boolean isLike;

private boolean isBookmark;
private Boolean isBookmark;

private String userName;

Expand All @@ -29,13 +29,13 @@ public class OotdGetAllRes {

private String contents;

private int viewCount;
private Integer viewCount;

private int reportCount;
private Integer reportCount;

private int likeCount;
private Integer likeCount;

private boolean isFollowing;
private Boolean isFollowing;

private LocalDateTime createAt;

Expand All @@ -44,9 +44,9 @@ public class OotdGetAllRes {
private List<OotdStyleRes> styles;

public OotdGetAllRes(Ootd ootd,
boolean isLike,
int viewCount,
int likeCount,
Boolean isLike,
Integer viewCount,
Integer likeCount,
User loginUser) {

this.isLike = isLike;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class OotdGetByUserRes {

public OotdGetByUserRes(Ootd ootd) {
this.id = ootd.getId();
this.image = ootd.getOotdImages().get(0).getImageUrl();
this.image = ootd.getFirstImage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class OotdGetOtherRes {

public OotdGetOtherRes(Ootd ootd) {
this.id = ootd.getId();
this.image = ootd.getOotdImages().get(0).getImageUrl();
this.image = ootd.getFirstImage();
}
}
20 changes: 10 additions & 10 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdGetRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class OotdGetRes {

private String contents;

private boolean isLike;
private Boolean isLike;

private int viewCount;
private Integer viewCount;

private int reportCount;
private Integer reportCount;

private int likeCount;
private Integer likeCount;

private boolean isBookmark;
private Boolean isBookmark;

private String userName;

Expand All @@ -47,18 +47,18 @@ public class OotdGetRes {

private LocalDateTime createAt;

private boolean isFollowing;
private Boolean isFollowing;

private boolean isPrivate;
private Boolean isPrivate;

private List<OotdImageRes> ootdImages;

private List<OotdStyleRes> styles;

public OotdGetRes(Ootd ootd,
boolean isLike,
int viewCount,
int likeCount,
Boolean isLike,
Integer viewCount,
Integer likeCount,
User loginUser) {

this.isLike = isLike;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/zip/ootd/ootdzip/ootd/domain/Ootd.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ public void deleteOotd() {
this.deletedAt = LocalDateTime.now();
}

// == Custom Getter == //
public String getFirstImage() {
if (ootdImages.isEmpty()) {
throw new IllegalArgumentException("저장된 OOTD 이미지가없는 OOTD 를 조회했습니다.");
}
return ootdImages.get(0).getImageUrl();
}

// == 연관관계 메서드 == //
public void addOotdImage(OotdImage ootdImage) {
ootdImages.add(ootdImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public interface OotdRepository extends JpaRepository<Ootd, Long> {
@Query("SELECT o from Ootd o where (o.isPrivate = false or o.writer.id = :userId) ")
Slice<Ootd> findAllByUserId(@Param("userId") Long userId, Pageable pageable);

@Query("SELECT o from Ootd o where o.writer.id = :userId "
+ "and (o.isPrivate = false or o.writer.id = :loginUserId) ")
Slice<Ootd> findAllByUserIdAndLoginUserId(@Param("userId") Long userId,
@Param("loginUserId") Long loginUserId,
Pageable pageable);

@Query("SELECT o from Ootd o where o.id in(:ootdIds)")
Slice<Ootd> findAllByIds(@Param("ootdIds") List<Long> ootdIds, Pageable pageable);

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/zip/ootd/ootdzip/ootd/service/OotdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,15 @@ public CommonSliceResponse<OotdGetSimilarRes> getOotdSimilar(OotdGetSimilarReq r

/**
* 마이페이지에서 OOTD 조회시 해당 유저가 가진 OOTD 정보를 제공합니다.
* 본인 조회시, 비공개글 조회가 됩니다.
* 본인 조회시, 비공개글 조회가 가능합니다. 그래서 loginUser 정보를 받아 검증할 필요가 있습니다.
*/
public CommonSliceResponse<OotdGetByUserRes> getOotdByUser(OotdGetByUserReq request) {
public CommonSliceResponse<OotdGetByUserRes> getOotdByUser(User loginUser, OotdGetByUserReq request) {

Long userId = request.getUserId();
Long loginUserId = loginUser.getId();
Pageable pageable = request.toPageable();

Slice<Ootd> ootds = ootdRepository.findAllByUserId(userId, pageable);
Slice<Ootd> ootds = ootdRepository.findAllByUserIdAndLoginUserId(userId, loginUserId, pageable);

List<OotdGetByUserRes> ootdGetByUserResList = ootds.stream()
.map(OotdGetByUserRes::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void getUserOotd() throws Exception {
Long userId = 1L;
Pageable pageable = PageRequest.of(0, 10);

when(ootdService.getOotdByUser(any()))
when(ootdService.getOotdByUser(any(), any()))
.thenReturn(new CommonSliceResponse<>(List.of(), pageable, true));

// when & then
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/zip/ootd/ootdzip/ootd/service/OotdServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,13 @@ void getOotdSimilarWithoutPrivate() {
void getOotdByUser() {
// given
User user = createUserBy("유저");
User user1 = createUserBy("유저1");
Ootd ootd = createOotdBy(user, "안녕", false);
Ootd ootd1 = createOotdBy(user, "안녕", false);
Ootd ootd2 = createOotdBy(user, "안녕", true);
Ootd ootd3 = createOotdBy(user, "안녕", true);
Ootd ootd4 = createOotdBy(user1, "안녕", false);
Ootd ootd5 = createOotdBy(user1, "안녕", true);

OotdGetByUserReq ootdGetByUserReq = new OotdGetByUserReq();
ootdGetByUserReq.setUserId(user.getId());
Expand All @@ -534,7 +537,7 @@ void getOotdByUser() {
ootdGetByUserReq.setSortDirection(Sort.Direction.DESC);

// when
CommonSliceResponse<OotdGetByUserRes> result = ootdService.getOotdByUser(ootdGetByUserReq);
CommonSliceResponse<OotdGetByUserRes> result = ootdService.getOotdByUser(user, ootdGetByUserReq);

// then
// ootd 는 작성시간 내림차순으로 정렬된다.
Expand All @@ -554,16 +557,18 @@ void getOotdByUserWithoutIsPrivate() {
Ootd ootd1 = createOotdBy(user, "안녕", false);
Ootd ootd2 = createOotdBy(user, "안녕", true);
Ootd ootd3 = createOotdBy(user, "안녕", true);
Ootd ootd4 = createOotdBy(user1, "안녕", false);
Ootd ootd5 = createOotdBy(user1, "안녕", true);

OotdGetByUserReq ootdGetByUserReq = new OotdGetByUserReq();
ootdGetByUserReq.setUserId(user1.getId());
ootdGetByUserReq.setUserId(user.getId());
ootdGetByUserReq.setPage(0);
ootdGetByUserReq.setSize(10);
ootdGetByUserReq.setSortCriteria("createdAt");
ootdGetByUserReq.setSortDirection(Sort.Direction.DESC);

// when
CommonSliceResponse<OotdGetByUserRes> result = ootdService.getOotdByUser(ootdGetByUserReq);
CommonSliceResponse<OotdGetByUserRes> result = ootdService.getOotdByUser(user1, ootdGetByUserReq);

// then
// ootd 는 작성시간 내림차순으로 정렬된다.
Expand Down

0 comments on commit 2085f43

Please sign in to comment.