Skip to content

Commit

Permalink
Feat : 정보 댓글 생성, 수정, 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
Astin01 committed Oct 20, 2024
1 parent 5786bff commit f05bb26
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import solitour_backend.solitour.image.exception.ImageRequestValidationFailedException;
import solitour_backend.solitour.information.exception.InformationNotExistsException;
import solitour_backend.solitour.information.exception.InformationNotManageException;
import solitour_backend.solitour.information_comment.exception.CommentNotOwnerException;
import solitour_backend.solitour.information_comment.exception.InformationCommentNotExistsException;
import solitour_backend.solitour.user.exception.BlockedUserException;
import solitour_backend.solitour.user.exception.DeletedUserException;
import solitour_backend.solitour.user.exception.DormantUserException;
Expand Down Expand Up @@ -79,6 +81,7 @@ public ResponseEntity<String> conflictException(Exception exception) {
GatheringBookMarkNotExistsException.class,
InformationBookMarkNotExistsException.class,
DiaryNotExistsException.class,
InformationCommentNotExistsException.class
})
public ResponseEntity<String> notFoundException(Exception exception) {
return ResponseEntity
Expand All @@ -89,7 +92,8 @@ public ResponseEntity<String> notFoundException(Exception exception) {
@ExceptionHandler({GatheringNotManagerException.class,
ForbiddenAccessException.class,
BlockedUserException.class,
DeletedUserException.class
DeletedUserException.class,
CommentNotOwnerException.class
})
public ResponseEntity<String> forbiddenException(Exception exception) {
return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package solitour_backend.solitour.information.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import solitour_backend.solitour.information.entity.Information;
import solitour_backend.solitour.user.entity.User;

import java.util.Optional;


public interface InformationRepository extends JpaRepository<Information, Long>, InformationRepositoryCustom {

@Query("SELECT i FROM Information i WHERE i.user.id = :userId")
Optional<Information> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package solitour_backend.solitour.information_comment.controller;


import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import solitour_backend.solitour.auth.config.Authenticated;
import solitour_backend.solitour.auth.config.AuthenticationPrincipal;
import solitour_backend.solitour.auth.support.JwtTokenProvider;
import solitour_backend.solitour.error.Utils;
import solitour_backend.solitour.information_comment.dto.request.InformationCommentRequest;
import solitour_backend.solitour.information_comment.dto.respose.InformationCommentResponse;
import solitour_backend.solitour.information_comment.service.InformationCommentService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/information/comment")
public class InformationCommentController {

private final InformationCommentService informationCommentService;

@Authenticated
@PostMapping
public ResponseEntity<InformationCommentResponse> createInformationComment(@AuthenticationPrincipal Long userId,
@Valid @RequestBody InformationCommentRequest informationCommentRequest) {

InformationCommentResponse informationCommentResponse = informationCommentService.createInformationComment(userId, informationCommentRequest);

return ResponseEntity
.status(HttpStatus.CREATED)
.body(informationCommentResponse);
}

@Authenticated
@PutMapping("/{informationCommentId}")
public ResponseEntity<Void> modifyInformationComment(@AuthenticationPrincipal Long userId,
@PathVariable Long informationCommentId,
@Valid @RequestBody InformationCommentRequest informationCommentRequest) {

informationCommentService.modifyInformationComment(userId, informationCommentId, informationCommentRequest);

return ResponseEntity.noContent().build();
}

@Authenticated
@DeleteMapping("/{informationCommentId}")
public ResponseEntity<Void> deleteInformationComment(@AuthenticationPrincipal Long userId,
@PathVariable Long informationCommentId) {
informationCommentService.deleteInformationComment(userId, informationCommentId);

return ResponseEntity.noContent().build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package solitour_backend.solitour.information_comment.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
public class InformationCommentRequest {
@NotBlank
private String comment;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package solitour_backend.solitour.information_comment.dto.respose;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
public class InformationCommentResponse {
@NotBlank
private Long commentId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package solitour_backend.solitour.information_comment.entity;

import jakarta.persistence.*;
import jakarta.validation.Valid;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import solitour_backend.solitour.category.entity.Category;
import solitour_backend.solitour.information.entity.Information;
import solitour_backend.solitour.information_comment.dto.request.InformationCommentRequest;
import solitour_backend.solitour.place.entity.Place;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.zone_category.entity.ZoneCategory;

import java.time.LocalDateTime;

@Entity
@Table(name = "information_comment")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class InformationComment {

@Id
@Column(name = "information_comment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "information_id")
private Information information;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Column(name = "information_comment_content")
private String content;

@CreatedDate
@Column(name = "information_comment_created_date")
private LocalDateTime createdDate;

@CreatedDate
@Column(name = "information_comment_updated_date")
private LocalDateTime updatedDate;

public void updateComment(@Valid InformationCommentRequest informationCommentRequest) {
this.content = informationCommentRequest.getComment();
this.updatedDate = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package solitour_backend.solitour.information_comment.exception;

public class CommentNotOwnerException extends RuntimeException {

public CommentNotOwnerException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package solitour_backend.solitour.information_comment.exception;

public class InformationCommentNotExistsException extends RuntimeException {

public InformationCommentNotExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package solitour_backend.solitour.information_comment.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import solitour_backend.solitour.information.entity.Information;
import solitour_backend.solitour.information.repository.InformationRepositoryCustom;
import solitour_backend.solitour.information_comment.entity.InformationComment;


public interface InformationCommentRepository extends JpaRepository<InformationComment, Long> {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package solitour_backend.solitour.information_comment.service;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import solitour_backend.solitour.information.entity.Information;
import solitour_backend.solitour.information.exception.InformationNotExistsException;
import solitour_backend.solitour.information.repository.InformationRepository;
import solitour_backend.solitour.information_comment.dto.request.InformationCommentRequest;
import solitour_backend.solitour.information_comment.dto.respose.InformationCommentResponse;
import solitour_backend.solitour.information_comment.entity.InformationComment;
import solitour_backend.solitour.information_comment.exception.CommentNotOwnerException;
import solitour_backend.solitour.information_comment.exception.InformationCommentNotExistsException;
import solitour_backend.solitour.information_comment.repository.InformationCommentRepository;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.user.exception.UserNotExistsException;
import solitour_backend.solitour.user.repository.UserRepository;

import java.time.LocalDateTime;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class InformationCommentService {

private final InformationRepository informationRepository;
private final UserRepository userRepository;
private final InformationCommentRepository informationCommentRepository;

@Transactional
public InformationCommentResponse createInformationComment(Long userId, @Valid InformationCommentRequest informationCommentRequest) {
Information information = informationRepository.findByUserId(userId)
.orElseThrow(() -> new InformationNotExistsException("해당하는 정보가 없습니다."));
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserNotExistsException("해당하는 사용자가 없습니다."));

InformationComment informationComment = InformationComment.builder()
.information(information)
.user(user)
.content(informationCommentRequest.getComment())
.createdDate(LocalDateTime.now())
.build();

InformationComment savedInformationComment = informationCommentRepository.save(informationComment);

return new InformationCommentResponse(savedInformationComment.getId());
}

@Transactional
public void modifyInformationComment(Long userId, Long informationCommentId, @Valid InformationCommentRequest informationCommentRequest) {
InformationComment informationComment = informationCommentRepository.findById(informationCommentId)
.orElseThrow(() -> new InformationCommentNotExistsException("정보에 해당하는 댓글이 없습니다."));

if (!informationComment.getUser().getId().equals(userId)) {
throw new CommentNotOwnerException("댓글을 작성한 사용자가 아닙니다.");
}

informationComment.updateComment(informationCommentRequest);
}

@Transactional
public void deleteInformationComment(Long userId, Long informationCommentId) {
InformationComment informationComment = informationCommentRepository.findById(informationCommentId)
.orElseThrow(() -> new InformationCommentNotExistsException("정보에 해당하는 댓글이 없습니다."));

if (!informationComment.getUser().getId().equals(userId)) {
throw new CommentNotOwnerException("댓글을 작성한 사용자가 아닙니다.");
}

informationCommentRepository.delete(informationComment);
}
}
16 changes: 15 additions & 1 deletion src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ DROP TABLE IF EXISTS `info_tag`;
DROP TABLE IF EXISTS `gathering_tag`;
DROP TABLE IF EXISTS `comment`;
DROP TABLE IF EXISTS `image`;
Drop TABLE IF EXISTS `information_comment`;
DROP TABLE IF EXISTS `information`;
DROP TABLE IF EXISTS `gathering_applicants`;
DROP TABLE IF EXISTS `gathering`;
Expand Down Expand Up @@ -314,7 +315,6 @@ CREATE TABLE `diary_day_content`
CONSTRAINT `FK_diary_day_content_TO_diary` FOREIGN KEY (`diary_id`) REFERENCES `diary` (`diary_id`)
);


CREATE TABLE `term`
(
`term_id` BIGINT NOT NULL AUTO_INCREMENT,
Expand All @@ -325,3 +325,17 @@ CREATE TABLE `term`
PRIMARY KEY (`term_id`),
CONSTRAINT `FK_term_TO_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
);

CREATE TABLE `information_comment`
(
`information_comment_id` BIGINT NOT NULL AUTO_INCREMENT,
`information_id` BIGINT NOT NULL,
`user_id` BIGINT NOT NULL,
`information_comment_content` TEXT NOT NULL,
`information_comment_created_date` DATETIME NOT NULL,
`information_comment_updated_date` DATETIME NULL,

PRIMARY KEY (`information_comment_id`),
CONSTRAINT `FK_information_comment_TO_information` FOREIGN KEY (`information_id`) REFERENCES `information` (`information_id`),
CONSTRAINT `FK_information_comment_TO_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
);

0 comments on commit f05bb26

Please sign in to comment.