-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdminService.java
More file actions
267 lines (223 loc) · 9.81 KB
/
AdminService.java
File metadata and controls
267 lines (223 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.ongil.backend.domain.admin.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ongil.backend.domain.admin.dto.request.AdminBrandCreateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminBrandUpdateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminCategoryCreateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminCategoryUpdateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminProductCreateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminProductOptionCreateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminProductOptionUpdateRequest;
import com.ongil.backend.domain.admin.dto.request.AdminProductUpdateRequest;
import com.ongil.backend.domain.brand.converter.BrandConverter;
import com.ongil.backend.domain.brand.dto.response.BrandResponse;
import com.ongil.backend.domain.brand.entity.Brand;
import com.ongil.backend.domain.brand.repository.BrandRepository;
import com.ongil.backend.domain.category.converter.CategoryConverter;
import com.ongil.backend.domain.category.dto.response.CategorySimpleResponse;
import com.ongil.backend.domain.category.entity.Category;
import com.ongil.backend.domain.category.repository.CategoryRepository;
import com.ongil.backend.domain.product.converter.ProductConverter;
import com.ongil.backend.domain.product.dto.response.ProductOptionResponse;
import com.ongil.backend.domain.product.dto.response.ProductSimpleResponse;
import com.ongil.backend.domain.product.entity.Product;
import com.ongil.backend.domain.product.entity.ProductOption;
import com.ongil.backend.domain.product.enums.ProductType;
import com.ongil.backend.domain.product.repository.ProductOptionRepository;
import com.ongil.backend.domain.product.repository.ProductRepository;
import com.ongil.backend.global.common.exception.EntityNotFoundException;
import com.ongil.backend.global.common.exception.ErrorCode;
import com.ongil.backend.global.common.exception.ValidationException;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
@Transactional
public class AdminService {
private final BrandRepository brandRepository;
private final CategoryRepository categoryRepository;
private final ProductRepository productRepository;
private final ProductOptionRepository productOptionRepository;
private final BrandConverter brandConverter;
private final CategoryConverter categoryConverter;
private final ProductConverter productConverter;
public BrandResponse createBrand(AdminBrandCreateRequest request) {
Brand brand = Brand.builder()
.name(request.getName())
.description(request.getDescription())
.logoImageUrl(request.getLogoImageUrl())
.build();
Brand savedBrand = brandRepository.save(brand);
return brandConverter.toResponse(savedBrand);
}
public CategorySimpleResponse createCategory(AdminCategoryCreateRequest request) {
Category parentCategory = null;
if (request.getParentCategoryId() != null) {
parentCategory = categoryRepository.findById(request.getParentCategoryId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
}
Category category = Category.builder()
.name(request.getName())
.iconUrl(request.getIconUrl())
.displayOrder(request.getDisplayOrder())
.parentCategory(parentCategory)
.build();
Category savedCategory = categoryRepository.save(category);
return categoryConverter.toSimpleResponse(savedCategory);
}
public ProductSimpleResponse createProduct(AdminProductCreateRequest request) {
Brand brand = brandRepository.findById(request.getBrandId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.BRAND_NOT_FOUND));
Category category = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
// 상품은 하위 카테고리에만 등록 가능
if (category.getParentCategory() == null) {
throw new ValidationException(ErrorCode.INVALID_CATEGORY);
}
Integer discountPrice = null;
if (request.getPrice() != null && request.getDiscountRate() != null && request.getDiscountRate() > 0) {
discountPrice = request.getPrice() - (request.getPrice() * request.getDiscountRate() / 100);
}
Product product = Product.builder()
.name(request.getName())
.description(request.getDescription())
.price(request.getPrice())
.materialOriginal(request.getMaterialOriginal())
.imageUrls(request.getImageUrls())
.sizes(request.getSizes())
.colors(request.getColors())
.discountRate(request.getDiscountRate())
.discountPrice(discountPrice)
.productType(request.getProductType() != null ? request.getProductType() : ProductType.NORMAL)
.brand(brand)
.category(category)
.build();
Product savedProduct = productRepository.save(product);
return productConverter.toSimpleResponse(savedProduct);
}
public ProductOptionResponse createProductOption(AdminProductOptionCreateRequest request) {
Product product = productRepository.findById(request.getProductId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.PRODUCT_NOT_FOUND));
ProductOption productOption = ProductOption.builder()
.product(product)
.size(request.getSize())
.color(request.getColor())
.stock(request.getStock())
.build();
ProductOption savedOption = productOptionRepository.save(productOption);
return ProductOptionResponse.builder()
.optionId(savedOption.getId())
.size(savedOption.getSize())
.color(savedOption.getColor())
.stock(savedOption.getStock())
.stockStatus(savedOption.getStockStatus())
.build();
}
// 브랜드 수정
public BrandResponse updateBrand(Long brandId, AdminBrandUpdateRequest request) {
Brand brand = brandRepository.findById(brandId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.BRAND_NOT_FOUND));
brand.updateBrand(
request.getName(),
request.getDescription(),
request.getLogoImageUrl()
);
return brandConverter.toResponse(brand);
}
// 브랜드 삭제
public void deleteBrand(Long brandId) {
Brand brand = brandRepository.findById(brandId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.BRAND_NOT_FOUND));
// 해당 브랜드를 사용하는 상품이 있는지 확인
if (productRepository.existsByBrandId(brandId)) {
throw new ValidationException(ErrorCode.CANNOT_DELETE_BRAND_WITH_PRODUCTS);
}
brandRepository.delete(brand);
}
// 카테고리 수정
public CategorySimpleResponse updateCategory(Long categoryId, AdminCategoryUpdateRequest request) {
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
Category parentCategory = null;
if (request.getParentCategoryId() != null) {
parentCategory = categoryRepository.findById(request.getParentCategoryId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
}
category.updateCategory(
request.getName(),
request.getIconUrl(),
request.getDisplayOrder(),
parentCategory
);
return categoryConverter.toSimpleResponse(category);
}
// 카테고리 삭제
public void deleteCategory(Long categoryId) {
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
// 해당 카테고리를 사용하는 상품이 있는지 확인
if (productRepository.existsByCategoryId(categoryId)) {
throw new ValidationException(ErrorCode.CANNOT_DELETE_CATEGORY_WITH_PRODUCTS);
}
// 하위 카테고리가 있는지 확인
if (categoryRepository.existsByParentCategoryId(categoryId)) {
throw new ValidationException(ErrorCode.CANNOT_DELETE_CATEGORY_WITH_SUBCATEGORIES);
}
categoryRepository.delete(category);
}
// 상품 수정
public ProductSimpleResponse updateProduct(Long productId, AdminProductUpdateRequest request) {
Product product = productRepository.findById(productId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.PRODUCT_NOT_FOUND));
Brand brand = null;
if (request.getBrandId() != null) {
brand = brandRepository.findById(request.getBrandId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.BRAND_NOT_FOUND));
}
Category category = null;
if (request.getCategoryId() != null) {
category = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORY_NOT_FOUND));
// 상품은 하위 카테고리에만 등록 가능
if (category.getParentCategory() == null) {
throw new ValidationException(ErrorCode.INVALID_CATEGORY);
}
}
product.updateProduct(
request.getName(),
request.getDescription(),
request.getPrice(),
request.getMaterialOriginal(),
request.getImageUrls(),
request.getSizes(),
request.getColors(),
request.getDiscountRate(),
request.getProductType(),
brand,
category
);
return productConverter.toSimpleResponse(product);
}
// 상품 옵션 수정
public ProductOptionResponse updateProductOption(Long optionId, AdminProductOptionUpdateRequest request) {
ProductOption productOption = productOptionRepository.findById(optionId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.PRODUCT_OPTION_NOT_FOUND));
productOption.updateProductOption(
request.getSize(),
request.getColor(),
request.getStock()
);
return ProductOptionResponse.builder()
.optionId(productOption.getId())
.size(productOption.getSize())
.color(productOption.getColor())
.stock(productOption.getStock())
.stockStatus(productOption.getStockStatus())
.build();
}
// 상품 옵션 삭제
public void deleteProductOption(Long optionId) {
ProductOption productOption = productOptionRepository.findById(optionId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.PRODUCT_OPTION_NOT_FOUND));
productOptionRepository.delete(productOption);
}
}