Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
refactor : Category 영역에 대한 JPA Migration [#70]
Browse files Browse the repository at this point in the history
- MainCategory : SubCategory = 1:N 관계 고려
  • Loading branch information
beaniejoy committed Sep 8, 2021
1 parent 463d22d commit 7dbc1ce
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 173 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

// validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import kr.fiveminutesmarket.category.service.MainCategoryService;
import kr.fiveminutesmarket.common.dto.ResponseDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/subCategory")
Expand All @@ -20,22 +19,6 @@ public SubCategoryController(SubCategoryService subCategoryService) {
this.subCategoryService = subCategoryService;
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public ResponseDto<List<SubCategoryResponse>> getAll() {
List<SubCategoryResponse> subCategoryList = subCategoryService.findAll();

return new ResponseDto<>(0,null, subCategoryList);
}

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public ResponseDto<SubCategoryResponse> findById(@PathVariable("id") Long id) {
SubCategoryResponse subCategory = subCategoryService.findById(id);

return new ResponseDto<>(0,null, subCategory);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseDto<SubCategoryResponse> add(@Valid @RequestBody SubCategoryRequest resource) {
Expand All @@ -47,10 +30,10 @@ public ResponseDto<SubCategoryResponse> add(@Valid @RequestBody SubCategoryReque
@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public ResponseDto<?> update(@PathVariable("id") Long id,
@Valid @RequestBody SubCategoryRequest resource) {
@Valid @RequestBody SubCategoryRequest resource) {
subCategoryService.update(id, resource);

return new ResponseDto<>(0);
return new ResponseDto<>(0, "수정 완료하였습니다.");
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package kr.fiveminutesmarket.category.domain;

import kr.fiveminutesmarket.category.dto.request.MainCategoryRequest;
import kr.fiveminutesmarket.category.dto.response.MainCategoryResponse;

public class MainCategory {
import javax.persistence.*;
import java.util.List;

@Entity
public class MainCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long mainCategoryId;

private String mainCategoryName;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE ,mappedBy = "mainCategory")
private List<SubCategory> subCategoryList;

public MainCategory() {
}

Expand All @@ -24,15 +31,12 @@ public String getMainCategoryName() {
return mainCategoryName;
}

public List<SubCategory> getSubCategoryList() {
return subCategoryList;
}

public void updateInfo(MainCategoryRequest resource) {
this.mainCategoryName = resource.getMainCategoryName();
}

public MainCategoryResponse toResponse() {
MainCategoryResponse response = new MainCategoryResponse();
response.setMainCategoryId(mainCategoryId);
response.setMainCategoryName(mainCategoryName);

return response;
}
}
37 changes: 17 additions & 20 deletions src/main/java/kr/fiveminutesmarket/category/domain/SubCategory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package kr.fiveminutesmarket.category.domain;

import kr.fiveminutesmarket.category.dto.request.SubCategoryRequest;
import kr.fiveminutesmarket.category.dto.response.SubCategoryResponse;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class SubCategory {
import javax.persistence.*;

@Entity
public class SubCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long subCategoryId;

private String subCategoryName;

private Long mainCategoryId;
@ManyToOne
@JoinColumn(name = "main_category_id")
@JsonIgnore
private MainCategory mainCategory;

public SubCategory() {
}

public SubCategory(String subCategoryName, Long mainCategoryId) {
public SubCategory(String subCategoryName, MainCategory mainCategory) {
this.subCategoryName = subCategoryName;
this.mainCategoryId = mainCategoryId;
this.mainCategory = mainCategory;
}

public Long getSubCategoryId() {
Expand All @@ -27,21 +33,12 @@ public String getSubCategoryName() {
return subCategoryName;
}

public Long getMainCategoryId() {
return mainCategoryId;
}

public SubCategoryResponse toResponse() {
SubCategoryResponse response = new SubCategoryResponse();
response.setSubCategoryId(subCategoryId);
response.setSubCategoryName(subCategoryName);
response.setMainCategoryId(mainCategoryId);

return response;
public MainCategory getMainCategory() {
return mainCategory;
}

public void updateInfo(SubCategoryRequest resource) {
this.subCategoryName = resource.getSubCategoryName();
this.mainCategoryId = resource.getMainCategoryId();
public void updateInfo(String subCategoryName, MainCategory mainCategory) {
this.subCategoryName = subCategoryName;
this.mainCategory = mainCategory;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.fiveminutesmarket.category.dto.request;

import kr.fiveminutesmarket.category.domain.MainCategory;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
Expand All @@ -24,8 +23,4 @@ public String getMainCategoryName() {
return mainCategoryName;
}

public MainCategory toEntity() {
return new MainCategory(mainCategoryName);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.fiveminutesmarket.category.dto.request;

import kr.fiveminutesmarket.category.domain.SubCategory;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
Expand Down Expand Up @@ -30,8 +29,4 @@ public String getSubCategoryName() {
public Long getMainCategoryId() {
return mainCategoryId;
}

public SubCategory toEntity() {
return new SubCategory(subCategoryName, mainCategoryId);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package kr.fiveminutesmarket.category.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;

public class MainCategoryResponse {

private Long mainCategoryId;

private String mainCategoryName;

public Long getMainCategoryId() {
return mainCategoryId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<SubCategoryResponse> subCategoryResponses;

public MainCategoryResponse() {
}

public void setMainCategoryId(Long mainCategoryId) {
public MainCategoryResponse(Long mainCategoryId, String mainCategoryName, List<SubCategoryResponse> subCategoryResponses) {
this.mainCategoryId = mainCategoryId;
this.mainCategoryName = mainCategoryName;
this.subCategoryResponses = subCategoryResponses;
}

public Long getMainCategoryId() {
return mainCategoryId;
}

public String getMainCategoryName() {
return mainCategoryName;
}

public void setMainCategoryName(String mainCategoryName) {
this.mainCategoryName = mainCategoryName;
public List<SubCategoryResponse> getSubCategoryResponses() {
return subCategoryResponses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@ public class SubCategoryResponse {

private String subCategoryName;

private Long mainCategoryId;

public Long getSubCategoryId() {
return subCategoryId;
public SubCategoryResponse() {
}

public void setSubCategoryId(Long subCategoryId) {
public SubCategoryResponse(Long subCategoryId, String subCategoryName) {
this.subCategoryId = subCategoryId;
}

public String getSubCategoryName() {
return subCategoryName;
}

public void setSubCategoryName(String subCategoryName) {
this.subCategoryName = subCategoryName;
}

public Long getMainCategoryId() {
return mainCategoryId;
public Long getSubCategoryId() {
return subCategoryId;
}

public void setMainCategoryId(Long mainCategoryId) {
this.mainCategoryId = mainCategoryId;
public String getSubCategoryName() {
return subCategoryName;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package kr.fiveminutesmarket.category.repository;

import kr.fiveminutesmarket.category.domain.MainCategory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
public interface MainCategoryRepository extends JpaRepository<MainCategory, Long> {

@Mapper
public interface MainCategoryRepository {

int insert(@Param("mainCategory") MainCategory mainCategory);

List<MainCategory> findAll();

MainCategory findById(@Param("mainCategoryId") Long mainCategoryId);

int countByName(@Param("mainCategoryName") String mainCategoryName);

int updateMainCategory(@Param("mainCategoryId") Long mainCategoryId,
@Param("mainCategory") MainCategory mainCategory);

void deleteById(@Param("mainCategoryId") Long mainCategoryId);
long countByMainCategoryName(String mainCategoryName);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
package kr.fiveminutesmarket.category.repository;

import kr.fiveminutesmarket.category.domain.SubCategory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
public interface SubCategoryRepository extends JpaRepository<SubCategory, Long> {

@Mapper
public interface SubCategoryRepository {

int insert(@Param("subCategory") SubCategory subCategory);

List<SubCategory> findAll();

SubCategory findById(@Param("subCategoryId") Long subCategoryId);

int countByName(@Param("subCategoryName") String subCategoryName);

int updateSubCategory(@Param("subCategoryId") Long subCategoryId,
@Param("subCategory") SubCategory subCategory);

void deleteById(@Param("subCategoryId") Long subCategoryId);

void deleteByMainCategoryId(@Param("mainCategoryId") Long mainCategoryId);
long countBySubCategoryName(String subCategoryName);
}
Loading

0 comments on commit 7dbc1ce

Please sign in to comment.