-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/solitour_backend/solitour/information/repository/InformationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
57 changes: 57 additions & 0 deletions
57
...olitour_backend/solitour/information_comment/controller/InformationCommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
.../solitour_backend/solitour/information_comment/dto/request/InformationCommentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
13 changes: 13 additions & 0 deletions
13
...solitour_backend/solitour/information_comment/dto/respose/InformationCommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/solitour_backend/solitour/information_comment/entity/InformationComment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ava/solitour_backend/solitour/information_comment/exception/CommentNotOwnerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
..._backend/solitour/information_comment/exception/InformationCommentNotExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...olitour_backend/solitour/information_comment/repository/InformationCommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
...java/solitour_backend/solitour/information_comment/service/InformationCommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters