Skip to content

Commit

Permalink
[feat/fix] ERD 수정사항 반영 - 참여폼 등록 API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
minji1289 committed Jan 4, 2024
1 parent 52fb001 commit 4541fab
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<DemandItemResponseDto> getItemInfo(@PathVariable Long demand_project

//참여폼: 폼 등록
@PostMapping("/{demand_project_id}")
public ResponseEntity createDemandForm(@PathVariable Long demand_project_id, @RequestBody List<DemandDetailRequestDto> requestDto){
public ResponseEntity createDemandForm(@PathVariable Long demand_project_id, @RequestBody DemandFormRequestDto requestDto){
return demandDetailService.createDemandForm(demand_project_id, requestDto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<DemandDetailRequestDto> demandDetailRequestDtoList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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<DemandDetailRequestDto> 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<DemandItem> 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> 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;
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; //우편번호
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/wowmarket/wow_server/domain/DemandOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DemandOrder, Long> {

Page<DemandOrder> findByUser_Id(Long buyer_id, Pageable pageable);

boolean existsByUser_Id(Long buyer_id);
}

0 comments on commit 4541fab

Please sign in to comment.