From 4541fab0570444144461e25df4fd031247e37b0e Mon Sep 17 00:00:00 2001 From: Minji Kim Date: Thu, 4 Jan 2024 21:53:57 +0900 Subject: [PATCH] =?UTF-8?q?[feat/fix]=20ERD=20=EC=88=98=EC=A0=95=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B0=98=EC=98=81=20-=20=EC=B0=B8=EC=97=AC?= =?UTF-8?q?=ED=8F=BC=20=EB=93=B1=EB=A1=9D=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DemandProjectController.java | 2 +- .../dto/DemandFormRequestDto.java | 13 +++ .../service/DemandDetailService.java | 101 ++++++++---------- .../project/dto/OrderFormRequestDto.java | 2 - .../wow_server/domain/DemandOrder.java | 8 +- .../repository/DemandOrderRepository.java | 4 + 6 files changed, 69 insertions(+), 61 deletions(-) create mode 100644 src/main/java/wowmarket/wow_server/detail/demandproject/dto/DemandFormRequestDto.java diff --git a/src/main/java/wowmarket/wow_server/detail/demandproject/controller/DemandProjectController.java b/src/main/java/wowmarket/wow_server/detail/demandproject/controller/DemandProjectController.java index 7d191bc..1413446 100644 --- a/src/main/java/wowmarket/wow_server/detail/demandproject/controller/DemandProjectController.java +++ b/src/main/java/wowmarket/wow_server/detail/demandproject/controller/DemandProjectController.java @@ -42,7 +42,7 @@ public List getItemInfo(@PathVariable Long demand_project //참여폼: 폼 등록 @PostMapping("/{demand_project_id}") - public ResponseEntity createDemandForm(@PathVariable Long demand_project_id, @RequestBody List requestDto){ + public ResponseEntity createDemandForm(@PathVariable Long demand_project_id, @RequestBody DemandFormRequestDto requestDto){ return demandDetailService.createDemandForm(demand_project_id, requestDto); } diff --git a/src/main/java/wowmarket/wow_server/detail/demandproject/dto/DemandFormRequestDto.java b/src/main/java/wowmarket/wow_server/detail/demandproject/dto/DemandFormRequestDto.java new file mode 100644 index 0000000..15dc112 --- /dev/null +++ b/src/main/java/wowmarket/wow_server/detail/demandproject/dto/DemandFormRequestDto.java @@ -0,0 +1,13 @@ +package wowmarket.wow_server.detail.demandproject.dto; + + +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +public class DemandFormRequestDto { + //상품명 & 판매가 리스트 + List demandDetailRequestDtoList; +} \ No newline at end of file diff --git a/src/main/java/wowmarket/wow_server/detail/demandproject/service/DemandDetailService.java b/src/main/java/wowmarket/wow_server/detail/demandproject/service/DemandDetailService.java index 2653731..fb35d2c 100644 --- a/src/main/java/wowmarket/wow_server/detail/demandproject/service/DemandDetailService.java +++ b/src/main/java/wowmarket/wow_server/detail/demandproject/service/DemandDetailService.java @@ -4,6 +4,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import wowmarket.wow_server.detail.demandproject.dto.DemandDetailRequestDto; +import wowmarket.wow_server.detail.demandproject.dto.DemandFormRequestDto; import wowmarket.wow_server.domain.*; import wowmarket.wow_server.global.jwt.SecurityUtil; import wowmarket.wow_server.repository.*; @@ -19,75 +20,61 @@ public class DemandDetailService { private final DemandProjectRepository demandProjectRepository; private final DemandItemRepository demandItemRepository; + private final DemandOrderRepository demandOrderRepository; - public DemandDetailService(DemandDetailRepository demandDetailRepository, DemandItemRepository itemRepository, UserRepository userRepository, DemandProjectRepository demandProjectRepository, DemandItemRepository demandItemRepository) { + public DemandDetailService(DemandDetailRepository demandDetailRepository, DemandItemRepository itemRepository, UserRepository userRepository, DemandProjectRepository demandProjectRepository, DemandItemRepository demandItemRepository, DemandOrderRepository demandOrderRepository) { this.itemRepository = itemRepository; this.demandDetailRepository = demandDetailRepository; this.userRepository = userRepository; this.demandProjectRepository = demandProjectRepository; this.demandItemRepository= demandItemRepository; + this.demandOrderRepository=demandOrderRepository; } - public ResponseEntity createDemandForm(Long demand_project_id, List requestDto) + public ResponseEntity createDemandForm(Long demand_project_id, DemandFormRequestDto requestDto) { -// User user = userRepository.findByEmail(SecurityUtil.getLoginUsername()) -// .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST)); -// DemandProject demandProject = demandProjectRepository.findByDemandProject_Id(demand_project_id); -// -// //사용자의 대학교가 수요조사 프로젝트의 대학교와 일치하지 않으면 -// if (!Objects.equals(user.getUniv(), demandProject.getUser().getUniv())) -// { -// //에러 반환 -// return new ResponseEntity(HttpStatus.BAD_REQUEST); //400 -// } -// -// -// List demandItems= demandItemRepository.findDemandItemByDemandProject_Id(demand_project_id); -// //이미 수요조사폼을 작성한 사용자의 경우 -// for (int i = 0; i < demandItems.size(); i++) { -// if (demandDetailRepository.existsByUserIdAndDemandItemId(user.getId(), demandItems.get(i).getId())) -// { -// return new ResponseEntity(HttpStatus.FORBIDDEN); //403: Forbidden -// } -// } -// -// for (int i = 0; i < requestDto.size(); i++) { -// DemandItem demandItem = itemRepository.findDemandItemById(requestDto.get(i).getDemandItemId()); //프론트에서 demand_item_id 받아오기 -// int count = requestDto.get(i).getCount(); -// -// DemandDetail demandDetail = DemandDetail.builder() -// .user(user) //User user -// .demandItem(demandItem) //DemandItem demandItem -// .count(count) //int Count -// .build(); -// demandDetailRepository.save(demandDetail); -// } -// -// //참여인원 업데이트 -// demandProjectRepository.updateParticipantNumber(demand_project_id); + DemandProject demandProject = demandProjectRepository.findByDemandProject_Id(demand_project_id); + User user = userRepository.findByEmail(SecurityUtil.getLoginUsername()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST)); + + System.out.print("here"); + //* 사용자의 대학교가 수요조사 프로젝트의 대학교와 일치하지 않으면 -> 추후 수정될 조건 * + if (!Objects.equals(user.getUniv(), demandProject.getUser().getUniv())) + { + //에러 반환 + return new ResponseEntity(HttpStatus.BAD_REQUEST); + } + + //이미 수요조사폼을 작성한 사용자의 경우, 작성 못하게하는 로직 + if (demandOrderRepository.existsByUser_Id(user.getId())) + { + return new ResponseEntity(HttpStatus.FORBIDDEN); //403: Forbidden + + } + + DemandOrder demandOrder = DemandOrder.builder() + .user(user) + .demandProject(demandProject) + .build(); + demandOrderRepository.save(demandOrder); + + for (int i = 0; i < requestDto.getDemandDetailRequestDtoList().size(); i++) { + DemandItem item = demandItemRepository.findDemandItemById(requestDto.getDemandDetailRequestDtoList().get(i).getDemandItemId()); + int count = requestDto.getDemandDetailRequestDtoList().get(i).getCount(); + DemandDetail demandDetail = DemandDetail.builder() + .demandOrder(demandOrder) + .demandItem(item) + .count(count) + .build(); + demandDetailRepository.save(demandDetail); + } + + //참여인원 업데이트 + demandProjectRepository.updateParticipantNumber(demand_project_id); return new ResponseEntity(HttpStatus.OK); - } -// public DemandDetailRequestDto createDemandForm2(Long demand_project_id, DemandDetailListRequestDto requestDto) -// { -// for (int i = 0; i < requestDto.getDemandDetailRequestDtoList().size(); i++) { -// User user = userRepository.findByEmail(SecurityUtil.getLoginUsername()) -// .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST)); -// //List demandItem = itemRepository.findDemandItemByDemandProject_Id(demand_project_id); -// DemandItem demandItem = itemRepository.findDemandItemById(requestDto.getDemandDetailRequestDtoList().get(i).getDemandItemId()); //프론트에서 demand_item_id 받아오기 -// int count = requestDto.getDemandDetailRequestDtoList().get(i).getCount(); -// -// DemandDetail demandDetail = DemandDetail.builder() -// .user(user) //User user -// .demandItem(demandItem) //DemandItem demandItem -// .count(count) //int Count -// .build(); -// demandDetailRepository.save(demandDetail); -// } -// -// return null; -// } + } } \ No newline at end of file diff --git a/src/main/java/wowmarket/wow_server/detail/project/dto/OrderFormRequestDto.java b/src/main/java/wowmarket/wow_server/detail/project/dto/OrderFormRequestDto.java index aed4337..60c39fb 100644 --- a/src/main/java/wowmarket/wow_server/detail/project/dto/OrderFormRequestDto.java +++ b/src/main/java/wowmarket/wow_server/detail/project/dto/OrderFormRequestDto.java @@ -11,8 +11,6 @@ public class OrderFormRequestDto { private int total_price; //총 금액 private String delivery_msg; //배송메세지 - - //직접수령이면 zipcode, address, address_detail 필요 없긴 함.. service에서 구현하나..? 아니면 굳이??? //[배송정보] private String receiver; //수취인명 private String zipcode; //우편번호 diff --git a/src/main/java/wowmarket/wow_server/domain/DemandOrder.java b/src/main/java/wowmarket/wow_server/domain/DemandOrder.java index 4c335f6..3c70647 100644 --- a/src/main/java/wowmarket/wow_server/domain/DemandOrder.java +++ b/src/main/java/wowmarket/wow_server/domain/DemandOrder.java @@ -27,5 +27,11 @@ public class DemandOrder extends BaseEntity{ @ColumnDefault("0") @Setter - private int status; + private int isDel; + + @Builder + public DemandOrder(DemandProject demandProject, User user){ + this.demandProject = demandProject; + this.user = user; + } } diff --git a/src/main/java/wowmarket/wow_server/repository/DemandOrderRepository.java b/src/main/java/wowmarket/wow_server/repository/DemandOrderRepository.java index cc10880..e3efe7e 100644 --- a/src/main/java/wowmarket/wow_server/repository/DemandOrderRepository.java +++ b/src/main/java/wowmarket/wow_server/repository/DemandOrderRepository.java @@ -3,10 +3,14 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import wowmarket.wow_server.domain.DemandOrder; +import java.util.Optional; + public interface DemandOrderRepository extends JpaRepository { Page findByUser_Id(Long buyer_id, Pageable pageable); + boolean existsByUser_Id(Long buyer_id); }