Skip to content

Commit

Permalink
프로젝트 분리 (#39)
Browse files Browse the repository at this point in the history
* 프로젝트 분리 : admin-service(mvc+jpa), user-service(webflux+r2dbc)
  • Loading branch information
yeonkyungJoo committed May 31, 2023
1 parent d281aa8 commit 10fde93
Show file tree
Hide file tree
Showing 147 changed files with 4,724 additions and 3,173 deletions.
26 changes: 26 additions & 0 deletions admin-service/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

dependencies {
implementation project(path: ':common', configuration: 'default')

developmentOnly 'org.springframework.boot:spring-boot-devtools'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

runtimeOnly 'mysql:mysql-connector-java'

testImplementation 'com.h2database:h2'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

// lombok
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
testImplementation 'org.projectlombok:lombok'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bbaemin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AdminServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.bbaemin.admin.category.controller;

import lombok.RequiredArgsConstructor;
import org.bbaemin.admin.category.controller.request.CreateCategoryRequest;
import org.bbaemin.admin.category.controller.request.UpdateCategoryRequest;
import org.bbaemin.admin.category.controller.response.CategoryResponse;
import org.bbaemin.admin.category.service.CategoryService;
import org.bbaemin.admin.category.vo.Category;
import org.bbaemin.config.response.ApiResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/categories")
public class CategoryController {

private final CategoryService categoryService;

@GetMapping
public ApiResult<List<CategoryResponse>> listCategory() {
return ApiResult.ok(categoryService.listCategory().stream()
.map(category -> CategoryResponse.builder()
.code(category.getCode())
.name(category.getName())
.description(category.getDescription())
.parentCode(category.getParent().getCode())
.build())
.collect(Collectors.toList()));
}

@GetMapping("/{categoryId}")
public ApiResult<CategoryResponse> getCategory(@PathVariable Long categoryId) {
Category category = categoryService.getCategory(categoryId);
return ApiResult.ok(CategoryResponse.builder()
.code(category.getCode())
.name(category.getName())
.description(category.getDescription())
.parentCode(category.getParent().getCode())
.build());
}

@PostMapping
public ApiResult<CategoryResponse> createCategory(@Validated @RequestBody CreateCategoryRequest createCategoryRequest) {

Category category = Category.builder()
.code(createCategoryRequest.getCode())
.name(createCategoryRequest.getName())
.description(createCategoryRequest.getDescription())
.parent(createCategoryRequest.getParentId() != null ?
categoryService.getCategory(createCategoryRequest.getParentId()) : null)
.build();
Category saved = categoryService.createCategory(category);
CategoryResponse categoryResponse = CategoryResponse.builder()
.code(saved.getCode())
.name(saved.getName())
.description(saved.getDescription())
.parentCode(saved.getParent().getCode())
.build();
return ApiResult.created(categoryResponse);
}

@PutMapping("/{categoryId}")
public ApiResult<CategoryResponse> updateCategory(@PathVariable Long categoryId, @Validated @RequestBody UpdateCategoryRequest updateCategoryRequest) {

Category updated = categoryService.updateCategory(categoryId,
updateCategoryRequest.getCode(),
updateCategoryRequest.getName(),
updateCategoryRequest.getDescription(),
updateCategoryRequest.getParentId());

CategoryResponse categoryResponse = CategoryResponse.builder()
.code(updated.getCode())
.name(updated.getName())
.description(updated.getDescription())
.parentCode(updated.getParent().getCode())
.build();
return ApiResult.ok(categoryResponse);
}

@DeleteMapping("/{categoryId}")
public ApiResult<Long> deleteCategory(@PathVariable Long categoryId) {
return ApiResult.ok(categoryService.deleteCategory(categoryId));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.bbaemin.api.category.controller.request;
package org.bbaemin.admin.category.controller.request;

import lombok.Getter;

Expand All @@ -16,7 +16,5 @@ public class CreateCategoryRequest {
@NotBlank(message = "코드 상세내용은 필수입니다.")
private String description;

private boolean useYn;

private Long parentId; // 상위 카테고리
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.bbaemin.api.category.controller.request;
package org.bbaemin.admin.category.controller.request;

import lombok.Getter;

Expand All @@ -15,7 +15,6 @@ public class UpdateCategoryRequest {

@NotBlank(message = "코드 상세내용은 필수입니다.")
private String description;
private boolean useYn;

private Long parentId; // 상위 카테고리
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.bbaemin.admin.category.controller.response;

import lombok.Builder;
import lombok.Getter;

@Getter
public class CategoryResponse {

private Integer code;
private String name;
private String description;
private Integer parentCode;

@Builder
private CategoryResponse(Integer code, String name, String description, Integer parentCode) {
this.code = code;
this.name = name;
this.description = description;
this.parentCode = parentCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.bbaemin.api.category.repository;
package org.bbaemin.admin.category.repository;

import org.bbaemin.api.category.vo.Category;
import org.bbaemin.admin.category.vo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.bbaemin.admin.category.service;

import lombok.RequiredArgsConstructor;
import org.bbaemin.admin.category.repository.CategoryRepository;
import org.bbaemin.admin.category.vo.Category;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.NoSuchElementException;

@Service
@RequiredArgsConstructor
public class CategoryService {

private final CategoryRepository categoryRepository;


@Transactional(readOnly = true)
public List<Category> listCategory() {
return categoryRepository.findAll();
}

@Transactional(readOnly = true)
public Category getCategory(Long categoryId) {
return categoryRepository.findById(categoryId)
.orElseThrow(() -> new NoSuchElementException("categoryId : " + categoryId));
}

@Transactional
public Category createCategory(Category category) {
return categoryRepository.save(category);
}

@Transactional
public Category updateCategory(Long categoryId, Integer code, String name, String description, Long parentId) {

Category updated = getCategory(categoryId);
updated.setCode(code);
updated.setName(name);
updated.setDescription(description);

if (!ObjectUtils.isEmpty(parentId)) {
Category parent = getCategory(parentId);
updated.setParent(parent);
}

return updated;
}

@Transactional
public Long deleteCategory(Long categoryId) {
categoryRepository.deleteById(categoryId);
return categoryId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.bbaemin.admin.category.vo;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.bbaemin.admin.delivery.vo.Store;
import org.bbaemin.admin.item.vo.Item;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

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

@Column(name = "code", nullable = false, unique = true)
private Integer code;

@Column(name = "name")
private String name;

@Column(name = "description")
@Lob
private String description;

@JoinColumn(name = "parent_id")
@ManyToOne(fetch = FetchType.LAZY)
private Category parent;

@OneToMany(mappedBy = "parent", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, orphanRemoval = true)
private List<Category> children = new ArrayList<>();

@OneToMany(mappedBy = "storeCategory")
private List<Store> storeList = new ArrayList<>();

@OneToMany(mappedBy = "itemCategory")
private List<Item> itemList = new ArrayList<>();

@Builder
private Category(Long categoryId, Integer code, String name, String description, Category parent, List<Category> children, List<Store> storeList, List<Item> itemList) {
this.categoryId = categoryId;
this.code = code;
this.name = name;
this.description = description;
if (!Objects.isNull(parent)) {
this.parent = parent;
parent.addChildren(this);
}
this.children = children;
this.storeList = storeList;
this.itemList = itemList;
}

public void setCode(Integer code) {
this.code = code;
}

public void setName(String name) {
this.name = name;
}

public void setDescription(String description) {
this.description = description;
}

public void setParent(Category parent) {
this.parent = parent;
parent.addChildren(this);
}

public void addChildren(Category category) {
if (this.children == null) {
this.children = new ArrayList<>();
}
this.children.add(category);
}

public void addStore(Store store) {
if (this.storeList == null) {
this.storeList = new ArrayList<>();
}
this.storeList.add(store);
}

public void addItem(Item item) {
if (this.itemList == null) {
this.itemList = new ArrayList<>();
}
this.itemList.add(item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.bbaemin.admin.delivery.controller;

import lombok.RequiredArgsConstructor;
import org.bbaemin.admin.delivery.service.DeliveryService;
import org.bbaemin.config.response.ApiResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/v1/delivery")
@RestController
@RequiredArgsConstructor
public class DeliveryController {

private final DeliveryService deliveryService;

@GetMapping("/fee")
public ApiResult<Integer> getDeliveryFee(@RequestParam(name = "orderAmount") Integer orderAmount) {
int deliveryFee = deliveryService.getDeliveryFee(orderAmount);
return ApiResult.ok(deliveryFee);
}
}
Loading

0 comments on commit 10fde93

Please sign in to comment.