Skip to content

Commit

Permalink
Merge pull request #33 from 9oormthon-univ/feature/GOORM-3-product
Browse files Browse the repository at this point in the history
[GOORM-3] - 상품 등록 API 구현
  • Loading branch information
lilloo04 authored Nov 23, 2024
2 parents 036b61a + 61ad111 commit 1b75d03
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.goormthon3.team49.domain.product.application;

import com.goormthon3.team49.domain.product.domain.entity.Product;
import com.goormthon3.team49.domain.product.domain.entity.Product;
import com.goormthon3.team49.domain.product.domain.entity.ProductReservation;
import com.goormthon3.team49.domain.product.presentation.dto.ParticipantDto;
import com.goormthon3.team49.domain.product.presentation.dto.ProductDto;
import com.goormthon3.team49.domain.product.presentation.dto.SummaryProductDto;
import com.goormthon3.team49.domain.product.domain.entity.ProductReservationId;
import com.goormthon3.team49.domain.product.presentation.dto.*;
import com.goormthon3.team49.domain.product.domain.repository.ProductRepository;
import com.goormthon3.team49.domain.product.domain.repository.ProductReservationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

Expand Down Expand Up @@ -71,4 +72,42 @@ public List<ParticipantDto> getProductParticipants(Long productId) {
))
.collect(Collectors.toList());
}
}

// 상품 등록하기
public AddProductDto createProduct(Long userId, AddProductRequestDto addProductRequestDto, ProductReservation reservation) {
// 상품 엔티티 생성
Product product = new Product();
product.setTitle(addProductRequestDto.getTitle());
product.setTotalNum(addProductRequestDto.getTotalNum());
reservation.setQuantity(addProductRequestDto.getQuantity());
product.setPrice(addProductRequestDto.getPrice());
product.setSavePrice(addProductRequestDto.getSavePrice());
product.setLink(addProductRequestDto.getLink());
product.setPickupLocation(addProductRequestDto.getPickupLocation());
// 필요시 이미지 처리 (예: product.setImgId(addProductRequestDto.getImg()));

// 상품 저장
Product savedProduct = productRepository.save(product);

// AddProductDto로 변환하여 반환
return AddProductDto.fromEntity(savedProduct, reservation);
}

// 상품 예약하기 (참여자 등록)
public void participateProduct(ParticipantDto participantDto) {
// ProductReservationId 생성 및 설정
ProductReservationId reservationId = new ProductReservationId();
reservationId.setProductId(participantDto.getProductId());
reservationId.setUserId(participantDto.getUserId());

// ProductReservation 엔티티 생성
ProductReservation reservation = new ProductReservation();
reservation.setId(reservationId); // 복합키 설정
reservation.setQuantity(participantDto.getQuantity()); // 예약 수량 설정
//reservation.setRole(participantDto.Role.참여자());
reservation.setCreatedAt(LocalDateTime.now()); // 생성 일시 설정

// 예약 정보 저장
productReservationRepository.save(reservation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.goormthon3.team49.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Setter
@Getter
@Entity(name = "ProductReservation")
public class ProductReservation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import jakarta.persistence.Embeddable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Objects;

@Setter
@Getter
@Embeddable
public class ProductReservationId implements Serializable {
Expand Down Expand Up @@ -43,4 +45,5 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(userId, productId, schoolId);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.goormthon3.team49.domain.product.presentation;

import com.goormthon3.team49.domain.product.application.ProductService;
import com.goormthon3.team49.domain.product.presentation.dto.ParticipantDto;
import com.goormthon3.team49.domain.product.presentation.dto.ProductDto;
import com.goormthon3.team49.domain.product.presentation.dto.SummaryProductDto;
import com.goormthon3.team49.domain.product.domain.entity.ProductReservation;
import com.goormthon3.team49.domain.product.presentation.dto.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -50,4 +49,24 @@ public ResponseEntity<List<ParticipantDto>> getProductParticipants(@PathVariable
List<ParticipantDto> participants = productService.getProductParticipants(productId);
return ResponseEntity.ok(participants);
}

//상품 등록하기
@PostMapping("/{userId}/leadCreate")
public ResponseEntity<AddProductDto> createProduct(@PathVariable Long userId, @RequestBody AddProductRequestDto addProductRequestDto) {
// AddProductRequestDto에서 ProductReservation 추출
ProductReservation reservation = addProductRequestDto.getReservation(); // getReservation() 메서드는 AddProductRequestDto에 정의되어 있어야 합니다.

// ProductService 호출 시 ProductReservation 객체 전달
AddProductDto createdProduct = productService.createProduct(userId, addProductRequestDto, reservation);

return ResponseEntity.ok(createdProduct);
}


// 상품 구매하기 (예약하기)
@PostMapping("/participantCreate")
public ResponseEntity<String> participateProduct(@RequestBody ParticipantDto participantDto) {
productService.participateProduct(participantDto);
return ResponseEntity.ok("ok");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.goormthon3.team49.domain.product.presentation.dto;

import com.goormthon3.team49.domain.product.domain.entity.Product;

import com.goormthon3.team49.domain.product.domain.entity.ProductReservation;
import jakarta.transaction.Transactional;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AddProductDto {
private Long productId;
private String title;
private Integer totalNum;
private Integer quantity;
private Integer price;
private Integer savePrice;
private String link;
private String pickupLocation;
private String img; // 이미지 주소



@Transactional
public static AddProductDto fromEntity(Product product, ProductReservation reservation) {
AddProductDto dto = new AddProductDto();
dto.setProductId(product.getProductId());
dto.setTitle(product.getTitle());
dto.setTotalNum(product.getTotalNum());
dto.setQuantity(reservation.getQuantity());
dto.setPrice(product.getPrice());
dto.setSavePrice(product.getSavePrice());
dto.setLink(product.getLink());
dto.setPickupLocation(product.getPickupLocation());
// dto.setImg(product.getProductImgId()); // 이미지 주소
return dto;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.goormthon3.team49.domain.product.presentation.dto;

import com.goormthon3.team49.domain.product.domain.entity.ProductReservation;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AddProductRequestDto {
private String title;
private Integer totalNum;
private Integer quantity;
private Integer price;
private Integer savePrice;
private String link;
private String pickupLocation;
private String img; // 이미지 주소

private ProductReservation reservation;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ProductDto {
private String link;
private String pickupLocation;
private Long productImgId;
//private String s3Key;
private String s3Key;

public static ProductDto fromEntity(Product product, ProductReservation productReservation) {
return ProductDto.builder()
Expand All @@ -34,7 +34,7 @@ public static ProductDto fromEntity(Product product, ProductReservation productR
.link(product.getLink())
.pickupLocation(product.getPickupLocation())
.productImgId(product.getProductImage() != null ? product.getProductImage().getProductImgId() : null)
// .s3Key(product.getProductImage() != null ? product.getProductImage().getS3Key() : null)
.s3Key(product.getProductImage() != null ? product.getProductImage().getS3Key() : null)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public class SummaryProductDto {

public static SummaryProductDto fromEntity(Product product) {
Long productImgId = product.getProductImage() != null ? product.getProductImage().getProductImgId() : null;
// String s3Key = product.getProductImage() != null ? product.getProductImage().getS3Key() : null;
String s3Key = product.getProductImage() != null ? product.getProductImage().getS3Key() : null;

return SummaryProductDto.builder()
.productId(product.getProductId())
.title(product.getTitle())
.savePrice(product.getSavePrice())
//.participantCount(product.getParticipants().size()) // 수정 필요
.productImgId(productImgId)
// .s3Key(s3Key)
// .s3Key(s3Key)
.build();
}
}

0 comments on commit 1b75d03

Please sign in to comment.