Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8주차]Cow_mvc_practice(fuirian) #8

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.cow.cow_mvc_practice.comment.controller;

import com.cow.cow_mvc_practice.comment.controller.dto.request.CommentRequest;
import com.cow.cow_mvc_practice.comment.controller.dto.request.UpdateCommentRequest;
import com.cow.cow_mvc_practice.comment.entity.Comment;
import com.cow.cow_mvc_practice.comment.service.CommentService;
import com.cow.cow_mvc_practice.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.hibernate.annotations.Comments;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.cert.Extension;
import java.util.List;

@RequiredArgsConstructor
@RestController
public class CommentController {

private final CommentService commentService;

@PostMapping("/api/comments")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/api/comments 경로는 클래스위에 @RequestMapping으로 전체 경로 지정해주세요.

public ResponseEntity<Comment> Comment(@RequestBody CommentRequest request, Member member) {
Comment savedComment = commentService.save(request, member.getId());

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

@GetMapping("/api/comments/{commentId}")
public void findComment(@PathVariable long commentId) {
Comment comment = commentService.findComment(commentId);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db에서 정보를 가져오는 부분인데 response가 존재하지 않습니다


@PutMapping("/api/comments/{commentId}")
public ResponseEntity<Comment> updateComment(@PathVariable long id, @RequestBody UpdateCommentRequest request) {
Comment updatedComment = commentService.update(id, request);

return ResponseEntity.ok()
.body(updatedComment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cow.cow_mvc_practice.comment.controller.dto.request;

import java.sql.ConnectionBuilder;
import java.util.List;
import com.cow.cow_mvc_practice.comment.entity.Comment;
import com.cow.cow_mvc_practice.member.controller.dto.response.MemberResponse;
import com.cow.cow_mvc_practice.member.entity.Member;
import lombok.Getter;

@Getter
public class CommentRequest {

private Member member;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Member로 선언하면 json파싱이 되나요?

private String title;
private String comment;


public Comment toEntity(Member member) {
return Comment.builder()
.member(member)
.title(title)
.comment(comment)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cow.cow_mvc_practice.comment.controller.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class UpdateCommentRequest {
private String title;
private String comment;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cow.cow_mvc_practice.comment.controller.dto.response;

import com.cow.cow_mvc_practice.comment.entity.Comment;
import lombok.Builder;
import lombok.Getter;

@Getter
public class CommentDto {
private final Long commentId;
private final String title;
private final String comment;

@Builder
private CommentDto(Long commentId, String title, String comment) {
this.commentId = commentId;
this.title = title;
this.comment = comment;
}

public static CommentDto of(Comment comment) {
return CommentDto.builder()
.commentId(comment.getId())
.title(comment.getTitle())
.comment(comment.getComment())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cow.cow_mvc_practice.comment.controller.dto.response;

import com.cow.cow_mvc_practice.comment.entity.Comment;
import lombok.Builder;

import java.util.List;

@Builder
public class CommentResponse {
private final String title;
private final String comment;

@Builder
public CommentResponse(String title, String comment) {
this.title = title;
this.comment = comment;
}

public static CommentResponse from(Comment comment) {
return CommentResponse.builder()
.title(comment.getTitle())
.comment(comment.getComment())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.cow.cow_mvc_practice.comment.entity;

import com.cow.cow_mvc_practice.member.entity.Member;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Comment {

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

private String title;
private String comment;

@Builder
private Comment(Member member, String title, String comment) {
this.member = member;
this.title = title;
this.comment = comment;
}

public void update(String title, String comment) {
this.title = title;
this.comment = comment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cow.cow_mvc_practice.comment.repository;

import com.cow.cow_mvc_practice.comment.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CommentRepository extends JpaRepository<Comment, Long> {

@Override
Optional<Comment> findById(Long commentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cow.cow_mvc_practice.comment.service;

import com.cow.cow_mvc_practice.comment.controller.dto.request.CommentRequest;
import com.cow.cow_mvc_practice.comment.controller.dto.request.UpdateCommentRequest;
import com.cow.cow_mvc_practice.comment.entity.Comment;
import com.cow.cow_mvc_practice.comment.repository.CommentRepository;
import com.cow.cow_mvc_practice.member.service.MemberService;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
@Transactional
public class CommentService {

private final CommentRepository commentRepository;
private final MemberService memberService;

public Comment save(CommentRequest commentRequest, Long memberId) {
Comment comment = CommentRequest.toEntity(memberService.findOne(memberId));
return commentRepository.save(comment);
}

public Comment findComment(long id) {
return commentRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("not found : " + id));
}

@Transactional
public Comment update(long id, UpdateCommentRequest request) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("not found : " + id));

comment.update(request.getTitle(), request.getComment());

return comment;
}
}