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

feat: 어드민 페이지 빵 추가시 빵추가 요청 id 연결 #383

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
package com.depromeet.breadmapbackend.domain.admin.bakery;

import com.depromeet.breadmapbackend.domain.admin.bakery.dto.*;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryAlarmBar;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryIsNewDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageBarDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageRegisterRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminProductDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminSimpleBakeryDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryAddDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryAddRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryLocationDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryProductsDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryUpdateReportDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryUpdateRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.NewReviewDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.ProductAddReportDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.param.AdminBakeryFilter;
import com.depromeet.breadmapbackend.domain.admin.bakery.param.AdminBakeryImageType;
import com.depromeet.breadmapbackend.domain.bakery.Bakery;
Expand All @@ -13,16 +44,25 @@
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportImage;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportImageRepository;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.*;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryAddReport;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryAddReportRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryReportImage;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryReportImageRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryUpdateReport;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryUpdateReportRepository;
import com.depromeet.breadmapbackend.domain.notice.dto.NoticeEventDto;
import com.depromeet.breadmapbackend.domain.notice.factory.NoticeType;
import com.depromeet.breadmapbackend.domain.review.*;
import com.depromeet.breadmapbackend.domain.review.Review;
import com.depromeet.breadmapbackend.domain.review.ReviewImage;
import com.depromeet.breadmapbackend.domain.review.ReviewImageRepository;
import com.depromeet.breadmapbackend.domain.review.ReviewProductRatingRepository;
import com.depromeet.breadmapbackend.domain.review.ReviewRepository;
import com.depromeet.breadmapbackend.domain.search.dto.OpenSearchIndex;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BakeryLoadData;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BreadLoadData;
import com.depromeet.breadmapbackend.domain.search.events.BakeryCreationEvent;
import com.depromeet.breadmapbackend.domain.search.events.BreadCreationEvent;
import com.depromeet.breadmapbackend.domain.search.events.BakeryDeletionEvent;
import com.depromeet.breadmapbackend.domain.search.events.BreadCreationEvent;
import com.depromeet.breadmapbackend.domain.user.User;
import com.depromeet.breadmapbackend.global.S3Uploader;
import com.depromeet.breadmapbackend.global.dto.PageResponseDto;
Expand All @@ -35,23 +75,9 @@
import com.depromeet.breadmapbackend.global.infra.feign.sgis.dto.SgisTranscoordDto;
import com.depromeet.breadmapbackend.global.infra.properties.CustomAWSS3Properties;
import com.depromeet.breadmapbackend.global.infra.properties.CustomSGISKeyProperties;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand Down Expand Up @@ -202,11 +228,11 @@ public BakeryAddDto addBakery(BakeryAddRequest request) {
if (bakery.getStatus().equals(BakeryStatus.POSTING)) {
if (pioneer != null) {
eventPublisher.publishEvent(
NoticeEventDto.builder()
.userId(pioneer.getId())
.contentId(bakery.getId())
.noticeType(NoticeType.REPORT_BAKERY_ADDED)
.build()
NoticeEventDto.builder()
.userId(pioneer.getId())
.contentId(bakery.getId())
.noticeType(NoticeType.REPORT_BAKERY_ADDED)
.build()
);
}
eventPublisher.publishEvent(
Expand All @@ -217,7 +243,9 @@ public BakeryAddDto addBakery(BakeryAddRequest request) {
.build()
);

BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this, new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this,
new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(),
bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBakery);
}

Expand All @@ -232,12 +260,15 @@ public void updateBakery(Long bakeryId, BakeryUpdateRequest request) {
List<String> images = getImagesIfExistsOrGetDefaultImage(request.getImages());

BakeryStatus status = request.getStatus();
if(status == BakeryStatus.POSTING) {
BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this, new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
if (status == BakeryStatus.POSTING) {
BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this,
new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(),
bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBakery);

} else if(status == BakeryStatus.UNPOSTING) {
BakeryDeletionEvent publishDeleteBakery = new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH, bakeryId);
} else if (status == BakeryStatus.UNPOSTING) {
BakeryDeletionEvent publishDeleteBakery = new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH,
bakeryId);
eventPublisher.publishEvent(publishDeleteBakery);
}

Expand All @@ -250,31 +281,57 @@ public void updateBakery(Long bakeryId, BakeryUpdateRequest request) {
request.getFacilityInfoList(), status);

if (request.getProductList() != null && !request.getProductList().isEmpty()) { // TODO
BakeryDeletionEvent publishDeleteBakery = new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH, bakeryId);
eventPublisher.publishEvent(publishDeleteBakery);
eventPublisher.publishEvent(new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH, bakeryId));
for (BakeryUpdateRequest.ProductUpdateRequest productUpdateRequest : request.getProductList()) {
Product product;
if (productUpdateRequest.getProductId() == null) { // 새로운 product 일 때
product = Product.builder()
.productType(productUpdateRequest.getProductType())
.name(productUpdateRequest.getProductName())
.price(productUpdateRequest.getPrice())
.image(productUpdateRequest.getImage())
.bakery(bakery).build(); // TODO
productRepository.save(product);
} else { // 기존 product 일 때
product = productRepository.findById(productUpdateRequest.getProductId())
.orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_NOT_FOUND));
product.update(productUpdateRequest.getProductType(), productUpdateRequest.getProductName(),
productUpdateRequest.getPrice(), productUpdateRequest.getImage());
}

BreadCreationEvent publishSaveBread = new BreadCreationEvent(this, new BreadLoadData(product.getId(), product.getName(), bakeryId, bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBread);
Product product = processProductUpdateRequest(productUpdateRequest, bakery);
eventPublisher.publishEvent(new BreadCreationEvent(this,
new BreadLoadData(product.getId(), product.getName(), bakeryId, bakery.getName(),
bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude())));
}
}
}

private Product processProductUpdateRequest(BakeryUpdateRequest.ProductUpdateRequest productUpdateRequest,
Bakery bakery) {
if (productUpdateRequest.getProductId() == null) {
return createNewProduct(productUpdateRequest, bakery);
} else {
return updateExistingProduct(productUpdateRequest);
}
}

private Product createNewProduct(BakeryUpdateRequest.ProductUpdateRequest productUpdateRequest, Bakery bakery) {
ProductAddReport productAddReport = productUpdateRequest.getReportId() != null ?
productAddReportRepository.findById(productUpdateRequest.getReportId())
.orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_ADD_REPORT_NOT_FOUND)) :
null;
Product product = Product.builder()
.productType(productUpdateRequest.getProductType())
.name(productUpdateRequest.getProductName())
.price(productUpdateRequest.getPrice())
.image(productUpdateRequest.getImage())
.bakery(bakery)
.productAddReport(productAddReport)
.build();
productRepository.save(product);
if (productAddReport != null) {
eventPublisher.publishEvent(NoticeEventDto.builder()
.userId(productAddReport.getUser().getId())
.contentId(productAddReport.getId())
.noticeType(NoticeType.ADD_PRODUCT)
.build());
}
return product;
}

private Product updateExistingProduct(BakeryUpdateRequest.ProductUpdateRequest productUpdateRequest) {
Product product = productRepository.findById(productUpdateRequest.getProductId())
.orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_NOT_FOUND));
product.update(productUpdateRequest.getProductType(), productUpdateRequest.getProductName(),
productUpdateRequest.getPrice(), productUpdateRequest.getImage());
return product;
}

@Transactional(rollbackFor = Exception.class)
public void deleteProduct(Long bakeryId, Long productId) {
Product product = productRepository.findById(productId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ public static class ProductUpdateRequest {
@NotBlank(message = "빵 가격은 필수 값입니다.", groups = ValidationGroups.NotEmptyGroup.class)
private String price;
private String image;
private Long reportId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.depromeet.breadmapbackend.domain.bakery.Bakery;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReport;
import com.depromeet.breadmapbackend.domain.review.ReviewProductRating;
import com.depromeet.breadmapbackend.global.BaseEntity;
import com.depromeet.breadmapbackend.global.converter.BooleanToYNConverter;
Expand Down Expand Up @@ -58,8 +60,12 @@ public class Product extends BaseEntity {
@Convert(converter = BooleanToYNConverter.class)
private boolean isTrue;

@OneToOne(fetch = FetchType.LAZY)
private ProductAddReport productAddReport;

@Builder
private Product(Long id, ProductType productType, String name, String price, String image, Bakery bakery, Boolean isTrue) {
private Product(Long id, ProductType productType, String name, String price, String image, Bakery bakery,
Boolean isTrue, ProductAddReport productAddReport) {
this.id = id;
this.productType = productType;
this.name = name;
Expand All @@ -71,6 +77,7 @@ private Product(Long id, ProductType productType, String name, String price, Str
else
this.isTrue = isTrue;
this.bakery.getProductList().add(this);
this.productAddReport = productAddReport;
}

public void update(ProductType productType, String name, String price, String image) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
// package com.depromeet.breadmapbackend.domain.notice.factory.push;
//
// import java.util.List;
//
// import org.springframework.stereotype.Component;
//
// import com.depromeet.breadmapbackend.domain.bakery.product.Product;
// import com.depromeet.breadmapbackend.domain.bakery.product.ProductRepository;
// import com.depromeet.breadmapbackend.domain.notice.Notice;
// import com.depromeet.breadmapbackend.domain.notice.dto.NoticeEventDto;
// import com.depromeet.breadmapbackend.domain.notice.factory.NoticeType;
// import com.depromeet.breadmapbackend.domain.user.User;
// import com.depromeet.breadmapbackend.domain.user.UserRepository;
// import com.depromeet.breadmapbackend.global.exception.DaedongException;
// import com.depromeet.breadmapbackend.global.exception.DaedongStatus;
// import com.depromeet.breadmapbackend.global.infra.properties.CustomAWSS3Properties;
//
// import lombok.RequiredArgsConstructor;
//
// @Component
// @RequiredArgsConstructor
// public class AddProductNoticeFactory implements NoticeFactory {
// private static final String NOTICE_TITLE_FORMAT = "내가 제보한 빵이 추가되었어요!";
//
// private static final NoticeType SUPPORT_TYPE = NoticeType.ADD_PRODUCT;
// private final CustomAWSS3Properties customAwss3Properties;
// private final ProductRepository productRepository;
// private final UserRepository userRepository;
//
// @Override
// public boolean support(final NoticeType noticeType) {
// return SUPPORT_TYPE == noticeType;
// }
//
// @Override
// public String getImage(final Notice notice) {
// return customAwss3Properties.getCloudFront() + "/" +
// customAwss3Properties.getDefaultImage().getReport()
// + ".png";
// }
//
// @Override
// public List<Notice> createNotice(final NoticeEventDto noticeEventDto) {
// final User user = userRepository.findById(noticeEventDto.userId())
// .orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND));
// final Product product = productRepository.findById(noticeEventDto.contentId())
// .orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_NOT_FOUND));
//
// return List.of(Notice.createNoticeWithContent(
// user,
// NOTICE_TITLE_FORMAT,
// noticeEventDto.contentId(),
// product.getBakery().getName() + " - " + product.getName(),
// noticeEventDto.noticeType()
// ));
// }
// }
package com.depromeet.breadmapbackend.domain.notice.factory.push;

import java.util.List;

import org.springframework.stereotype.Component;

import com.depromeet.breadmapbackend.domain.bakery.Bakery;
import com.depromeet.breadmapbackend.domain.bakery.BakeryRepository;
import com.depromeet.breadmapbackend.domain.bakery.product.Product;
import com.depromeet.breadmapbackend.domain.bakery.product.ProductRepository;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReport;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportRepository;
import com.depromeet.breadmapbackend.domain.notice.Notice;
import com.depromeet.breadmapbackend.domain.notice.dto.NoticeEventDto;
import com.depromeet.breadmapbackend.domain.notice.factory.NoticeType;
import com.depromeet.breadmapbackend.domain.user.User;
import com.depromeet.breadmapbackend.domain.user.UserRepository;
import com.depromeet.breadmapbackend.global.exception.DaedongException;
import com.depromeet.breadmapbackend.global.exception.DaedongStatus;
import com.depromeet.breadmapbackend.global.infra.properties.CustomAWSS3Properties;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class AddProductNoticeFactory implements NoticeFactory {
private static final String NOTICE_TITLE_FORMAT = "내가 제보한 빵이 추가되었어요!";

private static final NoticeType SUPPORT_TYPE = NoticeType.ADD_PRODUCT;
private final CustomAWSS3Properties customAwss3Properties;
private final ProductRepository productRepository;
private final BakeryRepository bakeryRepository;
private final ProductAddReportRepository productAddReportRepository;
private final UserRepository userRepository;

@Override
public boolean support(final NoticeType noticeType) {
return SUPPORT_TYPE == noticeType;
}

@Override
public String getImage(final Notice notice) {
return customAwss3Properties.getCloudFront() + "/" +
customAwss3Properties.getDefaultImage().getBreadAdd()
+ ".png";
}

@Override
public List<Notice> createNotice(final NoticeEventDto noticeEventDto) {
final User user = userRepository.findById(noticeEventDto.userId())
.orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND));
final ProductAddReport productAddReport = productAddReportRepository.findById(noticeEventDto.contentId())
.orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_ADD_REPORT_NOT_FOUND));
final Bakery bakery = bakeryRepository.findById(productAddReport.getBakery().getId())
.orElseThrow(() -> new DaedongException(DaedongStatus.BAKERY_NOT_FOUND));
final Product product = productRepository.findById(noticeEventDto.contentId())
.orElseThrow(() -> new DaedongException(DaedongStatus.PRODUCT_NOT_FOUND));

return List.of(Notice.createNoticeWithContent(
user,
NOTICE_TITLE_FORMAT,
bakery.getId(),
(product.getBakery().getName() + " - " + product.getName()),
null,
noticeEventDto.noticeType()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class BakeryAddNoticeFactory implements NoticeFactory {

@Override
public boolean support(final NoticeType noticeType) {
return SUPPORT_TYPE == noticeType;
return false;// SUPPORT_TYPE == noticeType;
}

@Override
Expand Down
Loading
Loading