Skip to content

Commit

Permalink
Merge pull request #111 from ootd-zip/feature/add-test(#109)
Browse files Browse the repository at this point in the history
Feature/add test(#109)
  • Loading branch information
jinhoon227 authored Feb 17, 2024
2 parents d192697 + 7a486f9 commit 70ad11e
Show file tree
Hide file tree
Showing 19 changed files with 1,896 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import zip.ootd.ootdzip.comment.data.CommentPostReq;
import zip.ootd.ootdzip.comment.data.CommentPostRes;
import zip.ootd.ootdzip.comment.service.CommentService;
import zip.ootd.ootdzip.common.response.ApiResponse;
import zip.ootd.ootdzip.user.service.UserService;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,13 +25,16 @@ public class CommentController {

private final CommentService commentService;

private final UserService userService;

@Operation(summary = "comment 작성", description = "사용자가 작성한 comment 를 저장하는 api")
@PostMapping("")
public ApiResponse<Boolean> saveComment(@RequestBody @Valid CommentPostReq request) {
public ApiResponse<CommentPostRes> saveComment(@RequestBody @Valid CommentPostReq request) {

commentService.saveComment(request);
CommentPostRes response = new CommentPostRes(
commentService.saveComment(request, userService.getAuthenticatiedUser()));

return new ApiResponse<>(true);
return new ApiResponse<>(response);
}

@Operation(summary = "comment 삭제", description = "사용자가 작성한 comment 를 삭제하는 api 로 soft delete 가 적용 됩니다.")
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/zip/ootd/ootdzip/comment/data/CommentPostReq.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package zip.ootd.ootdzip.comment.data;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
public class CommentPostReq {

@NotNull
@NotNull(message = "OOTD id 값은 필수 입니다.")
private Long ootdId;

private String taggedUserName;

private Long commentParentId;

@NotNull(message = "부모댓글인지 자식댓글인지 값을 알려주어야 합니다.")
private int parentDepth;
@Max(1)
@Min(0)
private Integer parentDepth;

@NotBlank(message = "댓글 내용은 필수입니다.")
@Size(max = 3000, message = "댓글은 최대 3000자 입니다.")
private String content;
}
16 changes: 16 additions & 0 deletions src/main/java/zip/ootd/ootdzip/comment/data/CommentPostRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package zip.ootd.ootdzip.comment.data;

import lombok.Data;
import lombok.NoArgsConstructor;
import zip.ootd.ootdzip.comment.domain.Comment;

@Data
@NoArgsConstructor
public class CommentPostRes {

private Long id;

public CommentPostRes(Comment comment) {
this.id = comment.getId();
}
}
19 changes: 14 additions & 5 deletions src/main/java/zip/ootd/ootdzip/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,6 +24,12 @@
import zip.ootd.ootdzip.ootd.domain.Ootd;
import zip.ootd.ootdzip.user.domain.User;

/**
* 댓글 조회시 기본 필터링으로 가져오는 댓글
* 1. 부모댓글, 삭제X, 신고수 5 미만, 자식댓글없음
* 2. 부모댓글, 자식댓글존재
* 3. 자식댓글, 삭제X, 신고수 5 미만
*/
@Entity
@Table(name = "comments")
@Where(clause = "(depth = 1 AND is_deleted = false AND report_count < 5 AND child_count = 0) "
Expand Down Expand Up @@ -59,6 +64,9 @@ public class Comment extends BaseEntity {

private String contents;

@Column(nullable = false)
private Long topOotdId;

@Builder.Default
@Column(nullable = false)
private Boolean isDeleted = false;
Expand Down Expand Up @@ -91,8 +99,8 @@ public void addChildComment(Comment comment) {
* 일년 이상 : 몇년전
*/
public String compareCreatedTimeAndNow() {
LocalTime createdTimeLT = this.createdAt.toLocalTime();
LocalTime nowLT = LocalDateTime.now().toLocalTime();
LocalDateTime createdTimeLT = this.createdAt;
LocalDateTime nowLT = LocalDateTime.now();

LocalDate createdTimeLD = this.createdAt.toLocalDate();
LocalDate nowLD = LocalDateTime.now().toLocalDate();
Expand All @@ -109,10 +117,11 @@ public String compareCreatedTimeAndNow() {
} else if (seconds < 604800) { //604800 = 1주일
long days = ChronoUnit.DAYS.between(createdTimeLD, nowLD);
return days + "일전";
} else if (seconds < 2419200) { //2419200 = 1달
} else if (seconds < 3024000 && ChronoUnit.MONTHS.between(createdTimeLD, nowLD) == 0) { //3024000 = 35일
long weeks = ChronoUnit.WEEKS.between(createdTimeLD, nowLD);
return weeks + "주전";
} else if (seconds < 29030400) { // 29030400 = 1년
} else if (seconds < 31536000 && ChronoUnit.YEARS.between(createdTimeLD, nowLD) == 0) {
// 31536000 = 365일, 1년이 366일때는 대비해 년도 비교 추가
long months = ChronoUnit.MONTHS.between(createdTimeLD, nowLD);
return months + "달전";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import zip.ootd.ootdzip.ootd.repository.OotdRepository;
import zip.ootd.ootdzip.user.domain.User;
import zip.ootd.ootdzip.user.repository.UserRepository;
import zip.ootd.ootdzip.user.service.UserService;

@Service
@Transactional
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;
private final UserService userService;
private final UserRepository userRepository;
private final OotdRepository ootdRepository;

Expand All @@ -31,9 +29,8 @@ public class CommentService {
* 대댓글을 반드시 누구에 대한 대댓글인지 태깅이 명시되어야함
* Depth 의 경우 댓글이 1, 대댓글이 2
*/
public void saveComment(CommentPostReq request) {
public Comment saveComment(CommentPostReq request, User writer) {

User writer = userService.getAuthenticatiedUser();
Ootd ootd = ootdRepository.findById(request.getOotdId()).orElseThrow();
Comment comment;

Expand All @@ -43,24 +40,28 @@ public void saveComment(CommentPostReq request) {
.writer(writer)
.depth(parentDepth + 1)
.contents(request.getContent())
.topOotdId(ootd.getId())
.build();
ootd.addComment(comment); // 대댓글이 아닌 댓글만 ootd 에 저장
} else {
User taggedUser = null;
User taggedUser;
if (request.getTaggedUserName() != null && !request.getTaggedUserName().isEmpty()) {
taggedUser = userRepository.findByName(request.getTaggedUserName()).orElseThrow();
} else {
throw new CustomException(ErrorCode.NO_TAGGING_USER);
}
Comment parentComment = commentRepository.findById(request.getCommentParentId()).orElseThrow();
comment = Comment.builder()
.writer(writer)
.depth(parentDepth + 1)
.contents(request.getContent())
.taggedUser(taggedUser)
.topOotdId(ootd.getId())
.build();
parentComment.addChildComment(comment); // 대댓글의 경우 ootd 정보를 따로 저장하지 않아 ootd 확인시 부모댓글을 조회해서 ootd 를 확인해야함
}

commentRepository.save(comment);
return commentRepository.save(comment);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public enum ErrorCode {

NOT_FOUNT_COMMENT_ID(404, "CM001", "유효하지 않은 댓글 ID"),

DUPLICATE_DELETE_COMMENT(400, "CM002", "이미 삭제된 댓글 입니다.");
DUPLICATE_DELETE_COMMENT(404, "CM002", "이미 삭제된 댓글 입니다."),

NO_TAGGING_USER(404, "CM003", "대댓글에는 반드시 태깅이 있어야 합니다.");

private final Integer status;

Expand Down
23 changes: 13 additions & 10 deletions src/main/java/zip/ootd/ootdzip/ootd/controller/OotdController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import zip.ootd.ootdzip.ootd.data.OotdPostRes;
import zip.ootd.ootdzip.ootd.data.OotdPutReq;
import zip.ootd.ootdzip.ootd.service.OotdService;
import zip.ootd.ootdzip.user.service.UserService;

@RestController
@RequiredArgsConstructor
Expand All @@ -33,17 +34,19 @@ public class OotdController {

private final OotdService ootdService;

private final UserService userService;

@Operation(summary = "ootd 작성", description = "사용자가 작성한 ootd 글을 저장하는 api")
@PostMapping("/")
@PostMapping("")
public ApiResponse<OotdPostRes> saveOotdPost(@RequestBody @Valid OotdPostReq request) {

OotdPostRes response = new OotdPostRes(ootdService.postOotd(request));
OotdPostRes response = new OotdPostRes(ootdService.postOotd(request, userService.getAuthenticatiedUser()));

return new ApiResponse<>(response);
}

@Operation(summary = "ootd 내용, 공개/비공개 여부 수정", description = "ootd 글과 공개여부만 수정하는 api")
@PatchMapping("/{id}")
@PatchMapping("")
public ApiResponse<Boolean> updateOotdContentsAndIsPrivate(@RequestBody @Valid OotdPatchReq request) {

ootdService.updateContentsAndIsPrivate(request);
Expand All @@ -52,7 +55,7 @@ public ApiResponse<Boolean> updateOotdContentsAndIsPrivate(@RequestBody @Valid O
}

@Operation(summary = "ootd 전체 수정", description = "ootd 게시글 전체 수정 api")
@PutMapping("/{id}")
@PutMapping("")
public ApiResponse<Boolean> updateOotdAll(@RequestBody @Valid OotdPutReq request) {

ootdService.updateAll(request);
Expand All @@ -73,7 +76,7 @@ public ApiResponse<Boolean> deleteOotd(@PathVariable Long id) {
@GetMapping("/{id}")
public ApiResponse<OotdGetRes> getOotdPost(@PathVariable Long id) {

OotdGetRes response = ootdService.getOotd(id);
OotdGetRes response = ootdService.getOotd(id, userService.getAuthenticatiedUser());

return new ApiResponse<>(response);
}
Expand All @@ -82,7 +85,7 @@ public ApiResponse<OotdGetRes> getOotdPost(@PathVariable Long id) {
@GetMapping("/all")
public ApiResponse<List<OotdGetAllRes>> getOotdPosts() {

List<OotdGetAllRes> response = ootdService.getOotds();
List<OotdGetAllRes> response = ootdService.getOotds(userService.getAuthenticatiedUser());

return new ApiResponse<>(response);
}
Expand All @@ -91,7 +94,7 @@ public ApiResponse<List<OotdGetAllRes>> getOotdPosts() {
@PostMapping("/like/{id}")
public ApiResponse<Boolean> addLike(@PathVariable Long id) {

ootdService.addLike(id);
ootdService.addLike(id, userService.getAuthenticatiedUser());

return new ApiResponse<>(true);
}
Expand All @@ -100,7 +103,7 @@ public ApiResponse<Boolean> addLike(@PathVariable Long id) {
@DeleteMapping("/like/{id}")
public ApiResponse<Boolean> cancelLike(@PathVariable Long id) {

ootdService.cancelLike(id);
ootdService.cancelLike(id, userService.getAuthenticatiedUser());

return new ApiResponse<>(true);
}
Expand All @@ -109,7 +112,7 @@ public ApiResponse<Boolean> cancelLike(@PathVariable Long id) {
@PostMapping("/bookmark/{id}")
public ApiResponse<Boolean> addBookMark(@PathVariable Long id) {

ootdService.addBookmark(id);
ootdService.addBookmark(id, userService.getAuthenticatiedUser());

return new ApiResponse<>(true);
}
Expand All @@ -118,7 +121,7 @@ public ApiResponse<Boolean> addBookMark(@PathVariable Long id) {
@DeleteMapping("/bookmark/{id}")
public ApiResponse<Boolean> cancelBookMark(@PathVariable Long id) {

ootdService.cancelBookmark(id);
ootdService.cancelBookmark(id, userService.getAuthenticatiedUser());

return new ApiResponse<>(true);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdPatchReq.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zip.ootd.ootdzip.ootd.data;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
Expand All @@ -9,6 +10,7 @@ public class OotdPatchReq {
@NotNull
private Long id;

@Size(max = 3000, message = "게시글은 최대 3000자 입니다.")
private String content;

@NotNull
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdPostReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

import java.util.List;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
public class OotdPostReq {

@Size(max = 3000, message = "게시글은 최대 3000자 입니다.")
private String content;

private Boolean isPrivate;

private List<Long> styles;

@Size(min = 1, message = "OOTD 게시글에는 반드시 1장 이상의 이미지가 있어야 합니다.")
@Valid
@NotEmpty(message = "OOTD 게시글에는 반드시 1장 이상의 이미지가 있어야 합니다.")
private List<OotdImageReq> ootdImages;

@Data
public static class OotdImageReq {

@NotEmpty(message = "OOTD 이미지는 반드시 존재해야 합니다.")
@NotEmpty(message = "OOTD 이미지 URL 는 반드시 존재해야 합니다.")
private String ootdImage;

private List<ClothesTagReq> clothesTags;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdPostRes.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package zip.ootd.ootdzip.ootd.data;

import lombok.Data;
import lombok.NoArgsConstructor;
import zip.ootd.ootdzip.ootd.domain.Ootd;

@Data
@NoArgsConstructor
public class OotdPostRes {

private Long id;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/zip/ootd/ootdzip/ootd/data/OotdPutReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -13,20 +14,22 @@ public class OotdPutReq {
@NotNull
private Long id;

@Size(max = 3000, message = "게시글은 최대 3000자 입니다.")
private String content;

@NotNull
private Boolean isPrivate;

private List<Long> styles;

@Size(min = 1, message = "OOTD 게시글에는 반드시 1장 이상의 이미지가 있어야 합니다.")
@Valid
@NotEmpty(message = "OOTD 게시글에는 반드시 1장 이상의 이미지가 있어야 합니다.")
private List<OotdImageReq> ootdImages;

@Data
public static class OotdImageReq {

@NotEmpty(message = "OOTD 이미지는 반드시 존재해야 합니다.")
@NotEmpty(message = "OOTD 이미지 URL 는 반드시 존재해야 합니다.")
private String ootdImage;

private List<ClothesTagReq> clothesTags;
Expand Down
Loading

0 comments on commit 70ad11e

Please sign in to comment.