Skip to content

Commit

Permalink
Refactor: 스터디 준비를 위한 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Feb 24, 2024
1 parent bb91358 commit 9194e0d
Show file tree
Hide file tree
Showing 25 changed files with 322 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.seminar.common.exception;

public class CategoryException extends RuntimeException {
public CategoryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.seminar.common.exception;

public class MemberException extends RuntimeException {
public MemberException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.seminar.common.exception;

public class PostException extends RuntimeException {
public PostException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.example.seminar.controller;


import com.example.seminar.domain.CategoryId;
import com.example.seminar.dto.response.category.CategoryResponse;
import com.example.seminar.service.CategoryService;
import com.example.seminar.service.category.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.seminar.dto.request.member.MemberCreateRequest;
import com.example.seminar.dto.request.member.MemberProfileUpdateRequest;
import com.example.seminar.dto.response.MemberGetResponse;
import com.example.seminar.service.MemberService;
import com.example.seminar.service.member.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,17 +20,6 @@ public class MemberController {

private final MemberService memberService;

@GetMapping("/{memberId}")
public ResponseEntity<MemberGetResponse> getMemberProfileV1(@PathVariable Long memberId) {
return ResponseEntity.ok(memberService.getMemberByIdV2(memberId));
}

@GetMapping(value = "/{memberId}/v2", consumes = "application/json", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<MemberGetResponse> getMemberProfileV2(@PathVariable Long memberId) {
return ResponseEntity.ok(memberService.getMemberByIdV2(memberId));
}


@PostMapping
public ResponseEntity<Void> createMember(@RequestBody MemberCreateRequest request) {
URI location = URI.create(memberService.create(request));
Expand All @@ -49,7 +38,7 @@ public ResponseEntity<Void> updateMemberSoptInfo(@PathVariable Long memberId, @R
}

@DeleteMapping("/{memberId}")
public ResponseEntity deleteMember(@PathVariable Long memberId) {
public ResponseEntity<Void> deleteMember(@PathVariable Long memberId) {
memberService.deleteMember(memberId);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.seminar.dto.request.post.PostCreateRequest;
import com.example.seminar.dto.request.post.PostUpdateRequest;
import com.example.seminar.dto.response.post.PostGetResponse;
import com.example.seminar.service.PostService;
import com.example.seminar.service.post.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.example.seminar.domain;


import com.example.seminar.common.exception.CategoryException;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
public class Category {

@Id @GeneratedValue
private Short id;
@Id @GeneratedValue(strategy = IDENTITY)
private Integer id;

private String content;

@Builder
private Category(String content) {
validateContent(content);
this.content = content;
}

private void validateContent(final String content) {
if (content.length() > 10) {
throw new CategoryException("카테고리 글자 제한을 넘었습니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTimeEntity{
public class Member extends BaseTimeEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String nickname;
Expand All @@ -29,12 +30,23 @@ public class Member extends BaseTimeEntity{

@Builder
public Member(String name, String nickname, int age, SOPT sopt) {
validateAge(age);
this.name = name;
this.nickname = nickname;
this.age = age;
this.sopt = sopt;
}

private void validateAge(final int age) {
if (0 < age && age < 100) {
throw new IllegalArgumentException("나이는 0보다 커야 합니다.");
}
}

private void validateName(final String name) {

}

public void updateSOPT(SOPT sopt) {
this.sopt = sopt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@Table(name = "post")
public class Post extends BaseTimeEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;
Expand All @@ -37,6 +38,7 @@ public void updateContent(String content) {
this.content = content;
}


public void addCategory(CategoryId categoryId) {
this.categoryId = categoryId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

import jakarta.persistence.Embeddable;
import jakarta.persistence.Enumerated;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import static jakarta.persistence.EnumType.*;

@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class SOPT {
private int generation;

@Enumerated(STRING)
private Part part;

@Builder
public SOPT(int generation, Part part) {
this.generation = generation;
this.part = part;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MemberRepository {
@PersistenceContext
private final EntityManager em;

void removeMemberEntity(Member member) {
public void remove(Member member) {
em.remove(member);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.seminar.service.category;

import com.example.seminar.domain.Category;
import com.example.seminar.repository.CategoryJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.xml.catalog.CatalogException;

@Component
@RequiredArgsConstructor
public class CategoryFinder {

private final CategoryJpaRepository categoryJpaRepository;

public Category findById(Short id) {
return categoryJpaRepository.findById(id)
.orElseThrow( () -> new CatalogException("존재하지 않는 카테고리입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.seminar.service;
package com.example.seminar.service.category;


import com.example.seminar.common.exception.BusinessException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.seminar.service.member;


import com.example.seminar.domain.Member;
import com.example.seminar.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MemberRemover {
private final MemberRepository memberRepository;

public void remove(Member member) {
memberRepository.remove(member);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.seminar.service.member;


import com.example.seminar.domain.Member;
import com.example.seminar.repository.MemberJpaRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MemberRetriever {

private final MemberJpaRepository memberJpaRepository;

public MemberRetriever(MemberJpaRepository memberJpaRepository) {
this.memberJpaRepository = memberJpaRepository;
}

public Member findById(Long id) {
return memberJpaRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("해당하는 회원이 없습니다."));
}

public List<Member> findAll() {
return memberJpaRepository.findAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.seminar.service.member;


import com.example.seminar.domain.Member;
import com.example.seminar.repository.MemberJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MemberSaver {
private final MemberJpaRepository memberJpaRepository;

public Member save(Member member) {
return memberJpaRepository.save(member);
}

}
Loading

0 comments on commit 9194e0d

Please sign in to comment.