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(win9-tech) #5

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8c72b2f
[refactor] 패지키 구조 리팩토링
win9-tech May 14, 2024
6a408f6
[Feat] 게시글 생성 구현
win9-tech May 18, 2024
b05596f
[Feat] 전체 게시글조회 구현
win9-tech May 18, 2024
a7d5bb8
[Feat] 사용자 정보 수정 구현
win9-tech May 18, 2024
bdc83de
[Feat] 단일 게시글 조회 구현
win9-tech May 18, 2024
c6dac48
[Feat] 댓글 작성 구현
win9-tech May 18, 2024
dd1dfb9
[Feat] 게시글 삭제 구현
win9-tech May 18, 2024
42479cc
[refactor] dto패키지 위치 변경
win9-tech May 18, 2024
03bc387
[refactor] Member 생성 코드 개선
win9-tech May 18, 2024
9bbac6c
[refactor] Member update 기능 개선
win9-tech May 18, 2024
ea3ae0d
[refactor] Member find 기능 개선
win9-tech May 18, 2024
770abb8
[refactor] All Member find 기능 개선
win9-tech May 18, 2024
4357eb8
[refactor] CreateMemberRequest toEntity 메서드 Member entity 정적 팩토리메서드 사용
win9-tech May 18, 2024
fff4abf
[refactor] MemberController, MemberService 메서드명 수정
win9-tech May 18, 2024
27f80c3
[refactor] PostController, PostService 메서드명 수정
win9-tech May 19, 2024
c1f4049
[refactor] Post delete 반환타입 변경
win9-tech May 19, 2024
d8cc1c1
[refactor] Comment 생성 코드 개선
win9-tech May 19, 2024
5aac46f
[refactor] 코드 컨벤션 수정
win9-tech May 19, 2024
1e9956e
[refactor] baseEntity, TimestampedEntity 적용
win9-tech May 20, 2024
70f8834
[refactor] 연관관계 편의 메서드 추가, Dto 구조 변경
win9-tech May 20, 2024
c1ccef8
[refactor] CreatedCommentResponse 컨벤션 수정
win9-tech May 20, 2024
9efb6e3
database erd 추가
win9-tech May 21, 2024
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
Binary file added cow_mvc_practice_erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class CowMvcPracticeApplication {

public static void main(String[] args) {
SpringApplication.run(CowMvcPracticeApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cow.cow_mvc_practice.comment.controller;

import com.cow.cow_mvc_practice.comment.dto.request.CreateCommentRequest;
import com.cow.cow_mvc_practice.comment.dto.response.CreatedCommentResponse;
import com.cow.cow_mvc_practice.comment.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/post/{postId}/comment")
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

@PostMapping("/new")
public CreatedCommentResponse create(@PathVariable Long postId, @RequestBody final CreateCommentRequest commentRequest) {
return commentService.create(postId, commentRequest);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cow.cow_mvc_practice.comment.dto.request;

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

@Getter
public class CreateCommentRequest {

private Long id;
private String content;

public Comment toEntity() {
return Comment.builder()
.content(this.content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cow.cow_mvc_practice.comment.dto.response;

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

import java.time.LocalDateTime;

@Getter
public class CreatedCommentResponse {

private final Long id;
private final Long postId;
private final Long memberId;
private final String content;
private final LocalDateTime createTime;

@Builder
private CreatedCommentResponse(final Long id, final Long postId, final Long memberId, final String content, final LocalDateTime createTime) {
this.id = id;
this.postId = postId;
this.memberId = memberId;
this.content = content;
this.createTime = createTime;
}

public static CreatedCommentResponse from(final Comment comment) {
return CreatedCommentResponse.builder()
.id(comment.getId())
.postId(comment.getPost().getId())
.memberId(comment.getMember().getId())
.content(comment.getContent())
.createTime(comment.getCreatedAt()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cow.cow_mvc_practice.comment.entity;

import com.cow.cow_mvc_practice.member.entity.Member;
import com.cow.cow_mvc_practice.post.entity.Post;
import com.cow.cow_mvc_practice.utill.TimestampedEntity;
import jakarta.persistence.*;
import lombok.*;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@AttributeOverride(name = "id", column = @Column(name = "comment_id"))
public class Comment extends TimestampedEntity {

private String content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

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

@Builder
private Comment(final String content, final Post post, final Member member) {
this.content = content;
this.post = post;
this.member = member;
}

public void addMember(Member member) {
this.member = member;
}

public void addPost(Post post) {
this.post = post;
post.getComments().add(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cow.cow_mvc_practice.comment.repository;

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

public interface CommentJPARepository extends JpaRepository<Comment, Long> {

int countByPostId(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cow.cow_mvc_practice.comment.service;

import com.cow.cow_mvc_practice.comment.dto.request.CreateCommentRequest;
import com.cow.cow_mvc_practice.comment.dto.response.CreatedCommentResponse;

public interface CommentService {

CreatedCommentResponse create(Long postId, CreateCommentRequest createCommentRequest);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.cow.cow_mvc_practice.comment.service;

import com.cow.cow_mvc_practice.comment.dto.request.CreateCommentRequest;
import com.cow.cow_mvc_practice.comment.dto.response.CreatedCommentResponse;
import com.cow.cow_mvc_practice.comment.entity.Comment;
import com.cow.cow_mvc_practice.comment.repository.CommentJPARepository;
import com.cow.cow_mvc_practice.member.entity.Member;
import com.cow.cow_mvc_practice.member.repository.MemberJPARepository;
import com.cow.cow_mvc_practice.post.entity.Post;
import com.cow.cow_mvc_practice.post.repository.PostJPARepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class CommentServiceImpl implements CommentService {

private final MemberJPARepository memberJPARepository;
private final PostJPARepository postJPARepository;
private final CommentJPARepository commentJPARepository;

@Override
public CreatedCommentResponse create(Long postId, CreateCommentRequest createCommentRequest) {
Member member = findMember(createCommentRequest);
Post post = findPost(postId);
Comment comment = createCommentRequest.toEntity();
comment.addMember(member);
comment.addPost(post);
commentJPARepository.save(comment);
return CreatedCommentResponse.from(comment);
}

private Post findPost(Long postId) {
return postJPARepository.findById(postId)
.orElseThrow(() -> new EntityNotFoundException("게시글을 찾을 수 없습니다."));
}

private Member findMember(CreateCommentRequest createCommentRequest) {
return memberJPARepository.findById(createCommentRequest.getId())
.orElseThrow(() -> new EntityNotFoundException("사용자를 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,40 @@

import java.util.List;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.cow_mvc_practice.member.controller.dto.request.MemberRequest;
import com.cow.cow_mvc_practice.member.controller.dto.response.MemberResponse;
import com.cow.cow_mvc_practice.member.dto.request.UpdateMemberRequest;
import com.cow.cow_mvc_practice.member.dto.response.FoundMemberResponse;
import com.cow.cow_mvc_practice.member.dto.response.UpdatedMemberResponse;
import org.springframework.web.bind.annotation.*;
import com.cow.cow_mvc_practice.member.dto.request.CreateMemberRequest;
import com.cow.cow_mvc_practice.member.dto.response.CreatedMemberResponse;
import com.cow.cow_mvc_practice.member.service.MemberService;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/member")
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;

/* 기본 */
// @PostMapping("/new")
// public String create(@RequestBody final MemberRequest memberRequest) {
// memberService.join(memberRequest);
// return "회원저장 성공!";
// }
//
// @GetMapping("/{memberId}")
// public String findMember(@PathVariable final Long memberId) {
// Member member = memberService.findOne(memberId);
// return "member 아이디: " + member.getId() + ", member 이름: " + member.getName();
// }
//

/* MemberResponse dto 적용 */
@PostMapping("/new")
public MemberResponse create(@RequestBody final MemberRequest memberRequest) {
return memberService.join(memberRequest.getName());
}

@GetMapping("/{memberId}")
public MemberResponse findMember(@PathVariable final Long memberId) {
return memberService.findOne(memberId);
}

@GetMapping()
public MemberResponse findMemberQuery(@RequestParam final Long memberId) {
return memberService.findOne(memberId);
}

@GetMapping("all")
public List<MemberResponse> findMembers() {
return memberService.findAll();
}
private final MemberService memberService;

@PostMapping("/new")
public CreatedMemberResponse create(@RequestBody final CreateMemberRequest createMemberRequest) {
return memberService.create(createMemberRequest);
}

@GetMapping("/{memberId}")
public FoundMemberResponse find(@PathVariable final Long memberId) {
return memberService.find(memberId);
}

@GetMapping("/all")
public List<FoundMemberResponse> findAll() {
return memberService.findAll();
}

@PatchMapping("/{memberId}")
public UpdatedMemberResponse update(@PathVariable("memberId") Long memberId, @RequestBody final UpdateMemberRequest updateMemberRequest) {
return memberService.update(memberId, updateMemberRequest);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cow.cow_mvc_practice.member.dto.request;

import com.cow.cow_mvc_practice.member.entity.Member;
import lombok.Getter;

@Getter
public class CreateMemberRequest {

private String name;

public Member toEntity() {
return Member.builder()
.name(this.name)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cow.cow_mvc_practice.member.controller.dto.request;
package com.cow.cow_mvc_practice.member.dto.request;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cow.cow_mvc_practice.member.dto.response;

import com.cow.cow_mvc_practice.member.entity.Member;
import lombok.Builder;
import lombok.Getter;

@Getter
public class CreatedMemberResponse {

private final Long id;
private final String name;

@Builder
private CreatedMemberResponse(final Long id, final String name) {
this.id = id;
this.name = name;
}

public static CreatedMemberResponse from(final Member member) {
return CreatedMemberResponse.builder()
.id(member.getId())
.name(member.getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.cow.cow_mvc_practice.member.controller.dto.response;
package com.cow.cow_mvc_practice.member.dto.response;

import com.cow.cow_mvc_practice.member.entity.Member;

import lombok.Builder;
import lombok.Getter;

@Getter
public class MemberResponse {
public class FoundMemberResponse {

private final Long id;
private final String name;

@Builder
private MemberResponse(final Long id, final String name) {
private FoundMemberResponse(final Long id, final String name) {
this.id = id;
this.name = name;
}

public static MemberResponse from(final Member member) {
return MemberResponse.builder()
public static FoundMemberResponse from(final Member member) {
return FoundMemberResponse.builder()
.id(member.getId())
.name(member.getName())
.build();
Expand Down
Loading