From a297e38f6380d8838ac8c279e39f5e88e0dfc09a Mon Sep 17 00:00:00 2001 From: DongGeon Lee <71763322+himodu@users.noreply.github.com> Date: Tue, 14 May 2024 01:02:24 +0900 Subject: [PATCH 1/4] [MODIFY&ADD] TEST update LIKE feature & add comment CRD featrue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. LIKE 관련 feature test 진행 후 디버깅 완료 2. userHash 값 기반 comment CRD featrue 개발완료 3. jpa 영속성 컨텍스트 캐싱 최적화 --- .../booth/controller/BoothController.java | 12 ++- .../domain/booth/entity/BoothEntity.java | 4 +- .../domain/booth/service/BoothService.java | 5 +- .../booth/service/BoothServiceImpl.java | 88 ++++++++++++++----- .../comment/controller/CommentController.java | 20 +++-- .../KNUfest/domain/comment/dto/Comment.java | 14 +-- .../domain/comment/dto/CommentRequest.java | 1 - .../domain/comment/entity/CommentEntity.java | 11 +-- .../comment/service/CommentService.java | 4 +- .../comment/service/CommentServiceImpl.java | 72 ++++++++------- .../domain/user/entity/UserBoothEntity.java | 14 ++- .../domain/user/entity/UserEntity.java | 11 ++- .../domain/user/service/UserBoothService.java | 13 --- .../user/service/UserBoothServiceImpl.java | 29 ------ .../domain/user/service/UserServiceImpl.java | 2 - .../KNUfest/global/basic/BasicEntity.java | 8 +- .../KNUfest/global/basic/BasicResponse.java | 3 + .../global/error/CustomExceptionHandler.java | 10 +-- .../KNUfest/global/error/ErrorMessage.java | 3 +- .../global/error/PasswordWrongException.java | 7 -- .../global/error/UserHashWrongException.java | 7 ++ 21 files changed, 184 insertions(+), 154 deletions(-) delete mode 100644 src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothService.java delete mode 100644 src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothServiceImpl.java delete mode 100644 src/main/java/LlikelionKNU/KNUfest/global/error/PasswordWrongException.java create mode 100644 src/main/java/LlikelionKNU/KNUfest/global/error/UserHashWrongException.java diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/booth/controller/BoothController.java b/src/main/java/LlikelionKNU/KNUfest/domain/booth/controller/BoothController.java index a0e0b9c..df15d74 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/booth/controller/BoothController.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/booth/controller/BoothController.java @@ -9,6 +9,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.time.LocalDateTime; + @RestController @RequestMapping("api/v1/booth") @RequiredArgsConstructor @@ -35,14 +37,16 @@ public ResponseEntity getBooth( } @PatchMapping("{boothId}") - @Operation(summary = "특정 부스 좋아요 업데이트", description = "특정 부스의 좋아요를 +1 한다.") + @Operation(summary = "특정 부스 좋아요 업데이트", description = "특정 부스의 좋아요를 변경한다.") public ResponseEntity updateLikes( - @PathVariable("boothId") Long boothId + @PathVariable("boothId") Long boothId, + @RequestParam("userHash") String userHash ){ - service.updateLikes(boothId); + String message = service.updateLikes(boothId, userHash); BasicResponse response = BasicResponse.builder() - .message("좋아요를 성공적으로 업데이트(+1) 하였습니다.") + .message(message) .status(200) + .timeStamp(LocalDateTime.now()) .build(); return ResponseEntity.ok().body(response); } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/booth/entity/BoothEntity.java b/src/main/java/LlikelionKNU/KNUfest/domain/booth/entity/BoothEntity.java index 5b07d2f..34d2956 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/booth/entity/BoothEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/booth/entity/BoothEntity.java @@ -29,8 +29,8 @@ public class BoothEntity extends BasicEntity { @OneToMany(mappedBy = "booth", fetch = FetchType.LAZY) private List commentEntityList; - @OneToMany(mappedBy = "boothEntity", fetch = FetchType.LAZY) - private UserBoothEntity userBoothEntity; + @OneToMany(mappedBy = "booth", fetch = FetchType.LAZY) + private List userBoothEntity; @ElementCollection @CollectionTable( diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothService.java b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothService.java index 7c56400..c06f3df 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothService.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothService.java @@ -2,10 +2,13 @@ import LlikelionKNU.KNUfest.domain.booth.dto.AllBooth; import LlikelionKNU.KNUfest.domain.booth.dto.BoothDetail; +import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity; public interface BoothService { AllBooth getAllbooth(String userHash); BoothDetail getBooth(Long id, String userHash); - void updateLikes(Long id); + String updateLikes(Long id, String userHash); + + BoothEntity findById(Long id); } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java index 5a02557..85ca970 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java @@ -7,9 +7,9 @@ import LlikelionKNU.KNUfest.domain.booth.repository.BoothRepository; import LlikelionKNU.KNUfest.domain.comment.service.CommentService; import LlikelionKNU.KNUfest.domain.user.entity.UserBoothEntity; -import LlikelionKNU.KNUfest.domain.user.service.UserBoothService; +import LlikelionKNU.KNUfest.domain.user.entity.UserEntity; +import LlikelionKNU.KNUfest.domain.user.repository.UserBoothRepository; import LlikelionKNU.KNUfest.domain.user.service.UserService; -import LlikelionKNU.KNUfest.domain.user.service.UserServiceImpl; import LlikelionKNU.KNUfest.global.error.NoExistException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -23,8 +23,9 @@ public class BoothServiceImpl implements BoothService{ private final BoothRepository boothrepository; + private final UserBoothRepository userBoothRepository; + private final CommentService commentService; - private final UserBoothService userBoothService; private final UserService userService; @Override @@ -33,8 +34,7 @@ public AllBooth getAllbooth(String userHash) { List boothes = boothrepository.findAll(); List boothDtos; - List userBoothEntityList = userBoothService.getAllUserBooth( - userService.getUserByHash(userHash).getId()); + List userBoothEntityList = userBoothRepository.findAllByUserId(userService.getUserByHash(userHash).getId()); if(boothes.isEmpty()) { throw new NoExistException("부스 전체 정보가 없습니다."); @@ -50,9 +50,9 @@ public AllBooth getAllbooth(String userHash) { } for(UserBoothEntity userBooth : userBoothEntityList){ - Booth tempbooth = boothDtos.get(userBooth.getBoothEntity().getId().intValue()); + Booth tempbooth = boothDtos.get(userBooth.getBooth().getId().intValue()-1); tempbooth.setLikable(false); - boothDtos.set(userBooth.getBoothEntity().getId().intValue(), tempbooth); + boothDtos.set(userBooth.getBooth().getId().intValue()-1, tempbooth); } return AllBooth.builder() @@ -63,41 +63,83 @@ public AllBooth getAllbooth(String userHash) { } @Override - public BoothDetail getBooth(Long id, String userHash) { - Optional boothOp = boothrepository.findById(id); + public BoothDetail getBooth(Long boothId, String userHash) { + Optional boothOp; - Optional userBoothEntity = userBoothService.getUserBooth(id, userService.getUserByHash(userHash).getId()); - - boolean temp = userBoothEntity.isEmpty(); + Optional userBoothEntity = userBoothRepository.findByUserIdAndBoothId(userService.getUserByHash(userHash).getId(), boothId); + BoothEntity booth; - if(boothOp.isEmpty()){ - throw new NoExistException("해당 부스 정보가 없습니다. (id 확인요망)"); + boolean temp; + if(userBoothEntity.isEmpty()){ + boothOp = boothrepository.findById(boothId); + temp = true; + if(boothOp.isEmpty()){ + throw new NoExistException("해당 부스 정보가 없습니다. (id 확인요망)"); + }else{ + booth = boothOp.get(); + } }else{ - BoothEntity booth = boothOp.get(); - + temp = false; + booth = userBoothEntity.get().getBooth(); + } return BoothDetail.builder() .id(booth.getId()) .boothName(booth.getBoothName()) .likes(booth.getLikes()) .urls(booth.getUrls()) .Likable(temp) - .comments(commentService.getCommentPage(boothOp.get().getId(),5,1, "default")) + .comments(commentService.getCommentPage(booth.getId(),5,1, "default", userHash)) .build(); - } } @Override - public void updateLikes(Long id) { + public String updateLikes(Long boothId, String userHash) { + + UserEntity user = userService.getUserByHash(userHash); + Optional userBoothEntity = userBoothRepository.findByUserIdAndBoothId(user.getId(), boothId); + UserBoothEntity userBooth; - Optional boothOp = boothrepository.findById(id); BoothEntity booth; + if(userBoothEntity.isEmpty()){ + Optional boothOp = boothrepository.findById(boothId); + + if(boothOp.isEmpty()){ + throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)"); + }else{ + booth = boothOp.get(); + booth.setLikes(booth.getLikes()+1); + boothrepository.save(booth); + + userBooth = UserBoothEntity.builder() + .booth(booth) + .user(user) + .build(); + + userBoothRepository.save(userBooth); + return "좋아요를 업데이트(+1) 하였습니다."; + } + + + }else{ + booth = userBoothEntity.get().getBooth(); + booth.setLikes(booth.getLikes()-1); + boothrepository.save(booth); + + userBoothRepository.delete(userBoothEntity.get()); + + return "좋아요를 업데이트(-1) 하였습니다."; + } + + } + + @Override + public BoothEntity findById(Long id){ + Optional boothOp = boothrepository.findById(id); if(boothOp.isEmpty()){ throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)"); }else{ - booth = boothOp.get(); - booth.setLikes(booth.getLikes()+1); - boothrepository.save(booth); + return boothOp.get(); } } } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java index d4782bb..e7da024 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.net.URI; +import java.time.LocalDateTime; import java.util.List; @RestController @@ -23,9 +24,10 @@ public ResponseEntity> getExtraCommentPage( @PathVariable("boothId") Long boothId, @RequestParam("perpage") int perpage, @RequestParam("page") int page, - @RequestParam("order") String order + @RequestParam("order") String order, + @RequestParam("userHash") String userHash ){ - List result = service.getCommentPage(boothId, perpage, page, order); + List result = service.getCommentPage(boothId, perpage, page, order, userHash); return ResponseEntity.ok().body(result); } @@ -33,14 +35,16 @@ public ResponseEntity> getExtraCommentPage( @Operation(summary = "특정부스 댓글 생성", description = "특정 부스에 댓글을 생성한다.") public ResponseEntity postComment( @PathVariable("boothId") Long boothId, - @RequestBody CommentRequest comment + @RequestParam("userHash") String userHash, + @RequestBody CommentRequest commentRequest ){ - Long id = service.postComment(boothId, comment); + Long id = service.postComment(boothId, commentRequest, userHash); BasicResponse response = BasicResponse.builder() - .message(boothId+"번 부스에 댓글을 생성하였습니다.") + .message(boothId+"번 부스에 " + id + "번째 댓글을 생성하였습니다.") .status(201) + .timeStamp(LocalDateTime.now()) .build(); return ResponseEntity.created(URI.create("comment"+id)).body(response); @@ -50,14 +54,14 @@ public ResponseEntity postComment( @Operation(summary = "특정 댓글 삭제", description = "특정 댓글을 password 가 일치할 경우 삭제한다.") public ResponseEntity deleteComment( @PathVariable("commentId") Long commentId, - @RequestParam("password") String password + @RequestParam("userHash") String userHash ){ - - service.deleteComment(commentId, password); + service.deleteComment(commentId, userHash); BasicResponse basicResponse = BasicResponse.builder() .message("성공적으로 삭제되었습니다.") .status(200) + .timeStamp(LocalDateTime.now()) .build(); return ResponseEntity.ok().body(basicResponse); diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/Comment.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/Comment.java index d9573c2..ba5e07c 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/Comment.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/Comment.java @@ -2,10 +2,12 @@ import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity; import LlikelionKNU.KNUfest.domain.comment.entity.CommentEntity; +import LlikelionKNU.KNUfest.domain.user.entity.UserEntity; import lombok.Builder; import lombok.Data; import java.time.Instant; +import java.time.LocalDateTime; @Data @Builder @@ -13,15 +15,15 @@ public class Comment { private Long id; private String name; private String comment; - private String password; - private Instant created; + private LocalDateTime created; + private Boolean deleteable; - public CommentEntity toEntity(BoothEntity booth){ + public static CommentEntity toEntity(CommentRequest commentRequest, BoothEntity booth, UserEntity user){ return CommentEntity.builder() - .name(this.name) - .password(this.password) - .comment(this.comment) + .name(commentRequest.getName()) + .comment(commentRequest.getComment()) .booth(booth) + .user(user) .build(); } } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/CommentRequest.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/CommentRequest.java index ddb11fd..bbf3dcc 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/CommentRequest.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/dto/CommentRequest.java @@ -8,5 +8,4 @@ public class CommentRequest { private String name; private String comment; - private String password; } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/entity/CommentEntity.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/entity/CommentEntity.java index 884d1e1..ef38bfb 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/entity/CommentEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/entity/CommentEntity.java @@ -3,6 +3,7 @@ import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity; import LlikelionKNU.KNUfest.domain.comment.dto.Comment; +import LlikelionKNU.KNUfest.domain.user.entity.UserEntity; import LlikelionKNU.KNUfest.global.basic.BasicEntity; import jakarta.persistence.*; import lombok.*; @@ -23,18 +24,18 @@ public class CommentEntity extends BasicEntity { @Column(name = "comment") private String comment; - @Column(name = "password", length = 10) - private String password; - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "booth_id") + @JoinColumn(name = "boothId") private BoothEntity booth; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "userId") + private UserEntity user; + public Comment toDto(){ return Comment.builder() .id(this.getId()) .name(this.name) - .password(this.password) .comment(this.comment) .created(this.getCreatedAt()) .build(); diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentService.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentService.java index e112c3f..adf4178 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentService.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentService.java @@ -6,8 +6,8 @@ import java.util.List; public interface CommentService { - List getCommentPage(Long boothId, int perpage, int page, String order); - Long postComment(Long id, CommentRequest comment); + List getCommentPage(Long boothId, int perpage, int page, String order, String userHash); + Long postComment(Long id, CommentRequest comment, String userHash); void deleteComment(Long commentId, String password); } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java index 3dbbd2b..ab0cefc 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java @@ -6,8 +6,10 @@ import LlikelionKNU.KNUfest.domain.comment.dto.CommentRequest; import LlikelionKNU.KNUfest.domain.comment.entity.CommentEntity; import LlikelionKNU.KNUfest.domain.comment.repository.CommentRepository; +import LlikelionKNU.KNUfest.domain.user.entity.UserEntity; +import LlikelionKNU.KNUfest.domain.user.service.UserService; import LlikelionKNU.KNUfest.global.error.NoExistException; -import LlikelionKNU.KNUfest.global.error.PasswordWrongException; +import LlikelionKNU.KNUfest.global.error.UserHashWrongException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -19,64 +21,74 @@ @RequiredArgsConstructor public class CommentServiceImpl implements CommentService{ private final CommentRepository commentRepository; + private final BoothRepository boothRepository; + private final UserService userService; + + @Override - public List getCommentPage(Long boothId, int perpage, int page, String order) { + public List getCommentPage(Long boothId, int perpage, int page, String order, String userHash) { Optional boothOp = boothRepository.findById(boothId); - List comments; - List result; - + BoothEntity booth; if(boothOp.isEmpty()){ - throw new NoExistException("댓글을 불러올 부스 정보가 없습니다. (id 확인 요망)"); + throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)"); }else{ - result = new ArrayList<>(); + booth = boothOp.get(); + } + + List comments; + List result = new ArrayList<>(); - int offset = (page-1) * perpage; + int offset = (page-1) * perpage; - if(order.equals("desc")){ - comments = commentRepository.findAllOrderByIdDESC(boothId, perpage, offset); + if(order.equals("desc")){ + comments = commentRepository.findAllOrderByIdDESC(booth.getId(), perpage, offset); + }else{ + comments = commentRepository.findAllOrderById(booth.getId(), perpage, offset); + } + for(CommentEntity c: comments){ + Comment comment = c.toDto(); + if(c.getUser().getUserHash().equals(userHash)){ + comment.setDeleteable(true); }else{ - comments = commentRepository.findAllOrderById(boothId, perpage, offset); + comment.setDeleteable(false); } - for(CommentEntity c: comments){ - result.add(c.toDto()); - } - return result; + result.add(comment); } + return result; + } @Override - public Long postComment(Long boothId, CommentRequest commentRequest) { + public Long postComment(Long boothId, CommentRequest commentRequest, String userHash) { - Optional boothOp = boothRepository.findById(boothId); - - Comment comment = Comment.builder() - .name(commentRequest.getName()) - .comment(commentRequest.getComment()) - .password(commentRequest.getPassword()) - .build(); + UserEntity user = userService.getUserByHash(userHash); + Optional boothOp = boothRepository.findById(boothId); + BoothEntity booth; if(boothOp.isEmpty()){ - throw new NoExistException("댓글을 추가할 부스 정보가 없습니다. (id 확인 요망)"); + throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)"); }else{ - CommentEntity newComment = comment.toEntity(boothOp.get()); - commentRepository.save(newComment); - return newComment.getId(); + booth = boothOp.get(); } + + CommentEntity newComment = Comment.toEntity(commentRequest, booth, user); + commentRepository.save(newComment); + return newComment.getId(); } @Override - public void deleteComment(Long commentId, String password) { + public void deleteComment(Long commentId, String userHash) { Optional commentOp = commentRepository.findById(commentId); if(commentOp.isEmpty()) { throw new NoExistException("해당 댓글은 없습니다. (id 확인 요망)"); }else{ - if(commentOp.get().getPassword().equals(password)){ + if(commentOp.get().getUser().getUserHash().equals(userHash)){ commentRepository.delete(commentOp.get()); }else{ - throw new PasswordWrongException("비밀번호가 틀렸습니다."); + throw new UserHashWrongException("userHash 값이 일치하지 않습니다."); } } } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java index 813dcf6..ef682ec 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java @@ -2,10 +2,7 @@ import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity; import LlikelionKNU.KNUfest.global.basic.BasicEntity; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -16,14 +13,15 @@ @Entity @NoArgsConstructor @AllArgsConstructor +@Table(name = "likes") public class UserBoothEntity extends BasicEntity { @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "userId") - private UserEntity userEntity; + @JoinColumn(name = "sex") + private UserEntity user; @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "boothId") - private BoothEntity boothEntity; + @JoinColumn(name = "ex") + private BoothEntity booth; } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserEntity.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserEntity.java index 560bd99..ce89e1d 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserEntity.java @@ -1,6 +1,7 @@ package LlikelionKNU.KNUfest.domain.user.entity; +import LlikelionKNU.KNUfest.domain.comment.entity.CommentEntity; import LlikelionKNU.KNUfest.global.basic.BasicEntity; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -8,16 +9,22 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.List; + @Data @AllArgsConstructor @NoArgsConstructor @Builder @Entity +@Table(name="users") public class UserEntity extends BasicEntity { @Column(name = "userHash") private String userHash; - @OneToMany(mappedBy = "userEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL) - private UserBoothEntity userBooth; + @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL) + private List userBoothes; + + @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) + private List comments; } diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothService.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothService.java deleted file mode 100644 index 2ddac37..0000000 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothService.java +++ /dev/null @@ -1,13 +0,0 @@ -package LlikelionKNU.KNUfest.domain.user.service; - - -import LlikelionKNU.KNUfest.domain.booth.dto.Booth; -import LlikelionKNU.KNUfest.domain.user.entity.UserBoothEntity; - -import java.util.List; -import java.util.Optional; - -public interface UserBoothService { - List getAllUserBooth(Long userId); - Optional getUserBooth(Long userId, Long boothId); -} diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothServiceImpl.java deleted file mode 100644 index 70dd05d..0000000 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserBoothServiceImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -package LlikelionKNU.KNUfest.domain.user.service; - -import LlikelionKNU.KNUfest.domain.booth.dto.Booth; -import LlikelionKNU.KNUfest.domain.user.entity.UserBoothEntity; -import LlikelionKNU.KNUfest.domain.user.repository.UserBoothRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.Optional; - -@Service -@RequiredArgsConstructor -public class UserBoothServiceImpl implements UserBoothService{ - - private final UserBoothRepository userBoothRepository; - - @Override - public List getAllUserBooth(Long userId) { - return userBoothRepository.findAllByUserId(userId); - } - - @Override - public Optional getUserBooth(Long userId, Long boothId) { - return userBoothRepository.findByUserIdAndBoothId(userId, boothId); - } - - -} diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserServiceImpl.java index 23cd017..8cf57ff 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserServiceImpl.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/user/service/UserServiceImpl.java @@ -12,8 +12,6 @@ @RequiredArgsConstructor public class UserServiceImpl implements UserService{ private final UserRepository userRepository; - - @Override public UserEntity getUserByHash(String userHash) { diff --git a/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicEntity.java b/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicEntity.java index 75a0151..9be5b26 100644 --- a/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicEntity.java @@ -4,12 +4,10 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import org.springframework.context.annotation.Configuration; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; - -import java.time.Instant; +import java.time.LocalDateTime; @MappedSuperclass @EntityListeners(AuditingEntityListener.class) @@ -24,10 +22,10 @@ public class BasicEntity { @CreatedDate @Column(updatable = false) - private Instant createdAt; + private LocalDateTime createdAt; @LastModifiedDate @Column(updatable = true) - private Instant updatedAt; + private LocalDateTime updatedAt; } diff --git a/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicResponse.java b/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicResponse.java index 32f8104..06b64b7 100644 --- a/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicResponse.java +++ b/src/main/java/LlikelionKNU/KNUfest/global/basic/BasicResponse.java @@ -5,6 +5,8 @@ import lombok.Data; import lombok.RequiredArgsConstructor; +import java.time.LocalDateTime; + @Data @Builder @RequiredArgsConstructor @@ -12,4 +14,5 @@ public class BasicResponse { private int status; private String message; + private LocalDateTime timeStamp; } diff --git a/src/main/java/LlikelionKNU/KNUfest/global/error/CustomExceptionHandler.java b/src/main/java/LlikelionKNU/KNUfest/global/error/CustomExceptionHandler.java index 0e478ec..12b9e6b 100644 --- a/src/main/java/LlikelionKNU/KNUfest/global/error/CustomExceptionHandler.java +++ b/src/main/java/LlikelionKNU/KNUfest/global/error/CustomExceptionHandler.java @@ -1,13 +1,13 @@ package LlikelionKNU.KNUfest.global.error; -import org.apache.coyote.Response; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.time.LocalDateTime; import java.util.Date; @RestControllerAdvice @@ -18,19 +18,19 @@ public ResponseEntity NoExistException(NoExistException ex){ ErrorMessage message = ErrorMessage.builder() .status(HttpStatus.NOT_FOUND.value()) .message(ex.getMessage()) - .timeStamp(new Date()) + .timeStamp(LocalDateTime.now()) .build(); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(message); } - @ExceptionHandler(PasswordWrongException.class) + @ExceptionHandler(UserHashWrongException.class) @ResponseStatus(HttpStatus.NOT_ACCEPTABLE) - public ResponseEntity PasswordWrongException(PasswordWrongException ex){ + public ResponseEntity PasswordWrongException(UserHashWrongException ex){ ErrorMessage message = ErrorMessage.builder() .status(HttpStatus.NOT_ACCEPTABLE.value()) .message(ex.getMessage()) - .timeStamp(new Date()) + .timeStamp(LocalDateTime.now()) .build(); return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(message); diff --git a/src/main/java/LlikelionKNU/KNUfest/global/error/ErrorMessage.java b/src/main/java/LlikelionKNU/KNUfest/global/error/ErrorMessage.java index f470249..01d4eb5 100644 --- a/src/main/java/LlikelionKNU/KNUfest/global/error/ErrorMessage.java +++ b/src/main/java/LlikelionKNU/KNUfest/global/error/ErrorMessage.java @@ -2,6 +2,7 @@ import lombok.*; +import java.time.LocalDateTime; import java.util.Date; @Data @@ -9,5 +10,5 @@ public class ErrorMessage { private int status; private String message; - private Date timeStamp; + private LocalDateTime timeStamp; } diff --git a/src/main/java/LlikelionKNU/KNUfest/global/error/PasswordWrongException.java b/src/main/java/LlikelionKNU/KNUfest/global/error/PasswordWrongException.java deleted file mode 100644 index 5281e7c..0000000 --- a/src/main/java/LlikelionKNU/KNUfest/global/error/PasswordWrongException.java +++ /dev/null @@ -1,7 +0,0 @@ -package LlikelionKNU.KNUfest.global.error; - -public class PasswordWrongException extends RuntimeException{ - public PasswordWrongException(String message){ - super(message); - } -} diff --git a/src/main/java/LlikelionKNU/KNUfest/global/error/UserHashWrongException.java b/src/main/java/LlikelionKNU/KNUfest/global/error/UserHashWrongException.java new file mode 100644 index 0000000..b9758da --- /dev/null +++ b/src/main/java/LlikelionKNU/KNUfest/global/error/UserHashWrongException.java @@ -0,0 +1,7 @@ +package LlikelionKNU.KNUfest.global.error; + +public class UserHashWrongException extends RuntimeException{ + public UserHashWrongException(String message){ + super(message); + } +} From 5a79f3857099f90904cf300182d5398f469091bd Mon Sep 17 00:00:00 2001 From: DongGeon Lee <71763322+himodu@users.noreply.github.com> Date: Tue, 14 May 2024 01:06:53 +0900 Subject: [PATCH 2/4] [MODIFY] time jet lag update & comment feature discription update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. server time return 을 local 시간에 맞게 수정 (LocalTimeDate 이용) 2. comment 삭제 기능 discription 수정 --- .../KNUfest/domain/comment/controller/CommentController.java | 2 +- .../KNUfest/global/time/controller/TimeController.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java index e7da024..fe45286 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/controller/CommentController.java @@ -51,7 +51,7 @@ public ResponseEntity postComment( } @DeleteMapping("comment/{commentId}") - @Operation(summary = "특정 댓글 삭제", description = "특정 댓글을 password 가 일치할 경우 삭제한다.") + @Operation(summary = "특정 댓글 삭제", description = "특정 댓글을 userHash 값을 통해 비교하여 삭제한다.") public ResponseEntity deleteComment( @PathVariable("commentId") Long commentId, @RequestParam("userHash") String userHash diff --git a/src/main/java/LlikelionKNU/KNUfest/global/time/controller/TimeController.java b/src/main/java/LlikelionKNU/KNUfest/global/time/controller/TimeController.java index b03320c..dc122fb 100644 --- a/src/main/java/LlikelionKNU/KNUfest/global/time/controller/TimeController.java +++ b/src/main/java/LlikelionKNU/KNUfest/global/time/controller/TimeController.java @@ -8,12 +8,13 @@ import org.springframework.web.bind.annotation.RestController; import java.time.Instant; +import java.time.LocalDateTime; @RestController("api/v1") public class TimeController { @GetMapping("time") @Operation(summary = "서버 시간 조회", description = "서버 현재 시간을 보내준다.") - public ResponseEntity getServerTime(){ - return ResponseEntity.ok().body(Instant.now()); + public ResponseEntity getServerTime(){ + return ResponseEntity.ok().body(LocalDateTime.now()); } } From 1bf3c85fd542974ef854b51ea28652b4f4cb4429 Mon Sep 17 00:00:00 2001 From: DongGeon Lee <71763322+himodu@users.noreply.github.com> Date: Tue, 14 May 2024 01:15:09 +0900 Subject: [PATCH 3/4] [MODIFY] Table column name update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit likes 테이블의 컬럼명 수정 --- .../KNUfest/domain/user/entity/UserBoothEntity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java index ef682ec..e8a8880 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/user/entity/UserBoothEntity.java @@ -17,11 +17,11 @@ public class UserBoothEntity extends BasicEntity { @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "sex") + @JoinColumn(name = "userId") private UserEntity user; @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "ex") + @JoinColumn(name = "boothId") private BoothEntity booth; } From deb4241ca553ad7fa848545556887b929015bafd Mon Sep 17 00:00:00 2001 From: DongGeon Lee <71763322+himodu@users.noreply.github.com> Date: Tue, 14 May 2024 19:19:24 +0900 Subject: [PATCH 4/4] [MODIFY] optimize ListNullcheck MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - List null 일 경우 loop execute 를 skip 합니다. --- .../domain/booth/service/BoothServiceImpl.java | 11 +++++++---- .../comment/service/CommentServiceImpl.java | 17 ++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java index 85ca970..bb11e85 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/booth/service/BoothServiceImpl.java @@ -49,12 +49,15 @@ public AllBooth getAllbooth(String userHash) { .build()); } - for(UserBoothEntity userBooth : userBoothEntityList){ - Booth tempbooth = boothDtos.get(userBooth.getBooth().getId().intValue()-1); - tempbooth.setLikable(false); - boothDtos.set(userBooth.getBooth().getId().intValue()-1, tempbooth); + if(!userBoothEntityList.isEmpty()){ + for(UserBoothEntity userBooth : userBoothEntityList){ + Booth tempbooth = boothDtos.get(userBooth.getBooth().getId().intValue()-1); + tempbooth.setLikable(false); + boothDtos.set(userBooth.getBooth().getId().intValue()-1, tempbooth); + } } + return AllBooth.builder() .count(boothDtos.size()) .boothDtoes(boothDtos) diff --git a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java index ab0cefc..5ede49c 100644 --- a/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java +++ b/src/main/java/LlikelionKNU/KNUfest/domain/comment/service/CommentServiceImpl.java @@ -47,14 +47,17 @@ public List getCommentPage(Long boothId, int perpage, int page, String }else{ comments = commentRepository.findAllOrderById(booth.getId(), perpage, offset); } - for(CommentEntity c: comments){ - Comment comment = c.toDto(); - if(c.getUser().getUserHash().equals(userHash)){ - comment.setDeleteable(true); - }else{ - comment.setDeleteable(false); + + if(!comments.isEmpty()) { + for (CommentEntity c : comments) { + Comment comment = c.toDto(); + if (c.getUser().getUserHash().equals(userHash)) { + comment.setDeleteable(true); + } else { + comment.setDeleteable(false); + } + result.add(comment); } - result.add(comment); } return result;