Skip to content

Commit

Permalink
[feat] 대여 게시물 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
jainefer committed Nov 19, 2024
1 parent 19fe5e4 commit 2457334
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/dev_deply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ jobs:
-e CLOUD_AWS_STACK_AUTO=false \
-e SPRING_SERVLET_MULTIPART_MAXFILESIZE=${{secrets.SPRING_SERVLET_MULTIPART_MAXFILESIZE}} \
-e SPRING_SERVLET_MULTIPART_MAXREQUESTSIZE=${{secrets.SPRING_SERVLET_MULTIPART_MAXREQUESTSIZE}} \
-e KAKAO_CLIENT_ID=${{secrets.KAKAO_CLIENT_ID}} \
-e KAKAO_REDIRECT_URI=${{secrets.KAKAO_REDIRECT_URI}} \
-e KAKAO_PROVIDER_AUTHORIZATION_URI=${{secrets.KAKAO_PROVIDER_AUTHORIZATION_URI}} \
-e KAKAO_PROVIDER_TOKEN_URI=${{secrets.KAKAO_PROVIDER_TOKEN_URI}} \
-e KAKAO_PROVIDER_USER_INFO_URI=${{secrets.KAKAO_PROVIDER_USER_INFO_URI}} \
-e PORTONE_API_SECRET=${{secrets.PORTONE_API_SECRET}} \
${{ secrets.DOCKERHUB_USERNAME }}/bill-api:latest
sudo docker rm $(sudo docker ps --filter 'status=exited' -a -q)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package site.billbill.apiserver.api.borrowPosts.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jboss.logging.MDC;
import org.springframework.web.bind.annotation.*;
import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;
import site.billbill.apiserver.api.borrowPosts.service.PostsService;
import site.billbill.apiserver.common.response.BaseResponse;
import site.billbill.apiserver.common.utils.jwt.JWTUtil;

@Slf4j
@RestController
@Tag(name = "borrowPosts", description = "대여 게시물 관련")
@RequestMapping("/api/v1/posts/borrowPosts")
@RequiredArgsConstructor
public class PostsController {
private final PostsService postsService;
@PostMapping("")
public BaseResponse<PostsResponse.UploadResponse> uploadPostsController(@RequestBody @Valid PostsRequest.UploadRequest request){

String userId = "";
if(MDC.get(JWTUtil.MDC_USER_ID) != null) {
userId= MDC.get(JWTUtil.MDC_USER_ID).toString();
}

return new BaseResponse<>(postsService.uploadPostService(request,userId));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package site.billbill.apiserver.api.borrowPosts.converter;

import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;
import site.billbill.apiserver.common.enums.items.PriceStandard;
import site.billbill.apiserver.model.post.ItemsBorrowJpaEntity;
import site.billbill.apiserver.model.post.ItemsBorrowStatusJpaEntity;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.ItemsBorrowStatusRepository;

public class PostsConverter {

public static PostsResponse.UploadResponse toUploadResponse(String id){
return PostsResponse.UploadResponse.builder().
postId(id).
build();
}
public static ItemsJpaEntity toItem(String postId,PostsRequest.UploadRequest request, UserJpaEntity user){
return ItemsJpaEntity.builder()
.id(postId)
.title(request.getTitle())
.content(request.getContent())
.delYn(false)
.owner(user)
.viewCount(0)
.images(request.getImages())
.itemStatus(request.getItemStatus()).build();
}
public static ItemsBorrowJpaEntity toItemBorrow(ItemsJpaEntity item, PostsRequest.UploadRequest request){
return ItemsBorrowJpaEntity.builder()
.item(item)
.priceStandard(request.getPriceStandard())
.price(request.getPrice())
.deposit(request.getDeposit())
.build();
}
public static ItemsBorrowStatusJpaEntity toItemBorrowStatus(ItemsJpaEntity item, String status, PostsRequest.NoRentalPeriod noRentalPeriod){
return ItemsBorrowStatusJpaEntity.builder().startDate(noRentalPeriod.getStartDate())
.endDate(noRentalPeriod.getEndDate())
.borrowStatusCode(status)
.item(item)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package site.billbill.apiserver.api.borrowPosts.dto.request;

import lombok.Getter;
import lombok.Setter;
import site.billbill.apiserver.common.enums.items.PriceStandard;
import site.billbill.apiserver.common.validation.EnumValidator;

import java.time.LocalDate;
import java.util.List;

public class PostsRequest {
@Getter
@Setter
public static class UploadRequest {
private String title;
private int price;
@EnumValidator(enumClass = PriceStandard.class, message = "priceStandard 값은 DAY, MONTH, YEAR 중 하나여야 합니다.")
private PriceStandard priceStandard;
private int deposit;
private int itemStatus;
private String content;
private List<String> images;
private List<NoRentalPeriod> noRental;

}
@Getter
@Setter
public static class NoRentalPeriod{
private LocalDate startDate;
private LocalDate endDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package site.billbill.apiserver.api.borrowPosts.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;


public class PostsResponse {
@Builder
@Getter
public static class UploadResponse {
private String postId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.billbill.apiserver.api.borrowPosts.service;

import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;

public interface PostsService {
PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request,String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package site.billbill.apiserver.api.borrowPosts.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import site.billbill.apiserver.api.borrowPosts.controller.PostsController;
import site.billbill.apiserver.api.borrowPosts.converter.PostsConverter;
import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;
import site.billbill.apiserver.common.utils.ULID.ULIDUtil;
import site.billbill.apiserver.model.post.ItemsBorrowJpaEntity;
import site.billbill.apiserver.model.post.ItemsBorrowStatusJpaEntity;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.ItemsBorrowRepository;
import site.billbill.apiserver.repository.borrowPosts.ItemsBorrowStatusRepository;
import site.billbill.apiserver.repository.borrowPosts.ItemsRepository;
import site.billbill.apiserver.repository.user.UserRepository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@org.springframework.stereotype.Service
@RequiredArgsConstructor

@Slf4j

public class PostsServiceImpl implements PostsService {

private final UserRepository userRepository;
private final ItemsRepository itemsRepository;
private final ItemsBorrowRepository itemsBorrowRepository;
private final ItemsBorrowStatusRepository itemsBorrowStatusRepository;
public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request,String userId){
//먼저 item 생성,
Optional<UserJpaEntity> isUser=userRepository.findById(userId);
String postsId = ULIDUtil.generatorULID("BORROW");

UserJpaEntity user=new UserJpaEntity();
if(isUser.isPresent()){
user=isUser.get();
}
//Item 생성
ItemsJpaEntity newItem= PostsConverter.toItem(postsId,request,user);
itemsRepository.save(newItem);
ItemsJpaEntity item=itemsRepository.findById(postsId).orElse(newItem);
//BorrowItem 생성
ItemsBorrowJpaEntity newBorrowItem= PostsConverter.toItemBorrow(item,request);
itemsBorrowRepository.save(newBorrowItem);
//대여 불가 기간 생성
if (request.getNoRental() != null && !request.getNoRental().isEmpty()) {
List<ItemsBorrowStatusJpaEntity> itemsBorrowStatusList = request.getNoRental().stream()
.map(status -> PostsConverter.toItemBorrowStatus(item, "RENTAL_NOT_POSSIBLE", status))
.toList();
itemsBorrowStatusRepository.saveAll(itemsBorrowStatusList);
}



return PostsConverter.toUploadResponse(postsId);





}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

public interface S3Service {
S3Response.uploadResponse uploadPostsFiles(List<MultipartFile> files, String userId, long id);
S3Response.uploadResponse uploadPostsFiles(List<MultipartFile> files, String userId);
S3Response.uploadResponse uploadChatFiles(List<MultipartFile> files,String userId,long id);
S3Response.uploadResponse uploadUserFiles(List<MultipartFile> files);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public class S3ServiceImpl implements S3Service {
private String CHATS_IMG_DIR="chats/";
private String USERS_IMG_DIR="users/";
@Override
public S3Response.uploadResponse uploadPostsFiles(List<MultipartFile> files, String userId, long id){
public S3Response.uploadResponse uploadPostsFiles(List<MultipartFile> files, String userId){

StringBuilder imgDir= new StringBuilder();
imgDir.append(Posts_IMG_DIR)
.append(File.separator) // 운영 체제에 맞는 구분자를 추가
.append(userId)
.append(File.separator)
.append(id);
.append("posts");
List<String>list= files.stream()
.map(file->{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BaseResponse<S3Response.uploadResponse> upload(@Valid List<MultipartFile>

}
return switch (option) {
case "post" -> new BaseResponse<>(s3Service.uploadPostsFiles(images, userId,id));
case "post" -> new BaseResponse<>(s3Service.uploadPostsFiles(images, userId));
case "chat" -> new BaseResponse<>(s3Service.uploadChatFiles(images, userId,id));
case "user" -> new BaseResponse<>(s3Service.uploadUserFiles(images));
default -> null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site.billbill.apiserver.common.validation;


import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = EnumValidatorImpl.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EnumValidator {
String message() default "유효하지 않은 ENUM 값입니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Class<? extends Enum<?>> enumClass();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package site.billbill.apiserver.common.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Arrays;

public class EnumValidatorImpl implements ConstraintValidator<EnumValidator, Enum<?>> {
private Class<? extends Enum<?>> enumClass;

@Override
public void initialize(EnumValidator constraintAnnotation) {
this.enumClass = constraintAnnotation.enumClass();
}

@Override
public boolean isValid(Enum<?> value, ConstraintValidatorContext context) {
if (value == null) {
return true; // null 허용 (Optional)
}
return Arrays.stream(enumClass.getEnumConstants())
.anyMatch(enumValue -> enumValue.equals(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpServerErrorException;
Expand All @@ -28,7 +29,13 @@ public ResponseEntity<ExceptionResponse> handleIllegalArgumentException(IllegalA
exceptionResult.setMessage(ex.getMessage());
return new ResponseEntity<>(exceptionResult, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(HttpMessageConversionException.class)
public ResponseEntity<ExceptionResponse> handleHttpMessageConversionException(HttpMessageConversionException ex) {
ExceptionResponse exceptionResult = new ExceptionResponse();
exceptionResult.setCode("400");
exceptionResult.setMessage("입력값이 잘못되었습니다. ENUM으로 들어가는 값이나 필수 값을 잘 입력했는지 확인해보세요.: "+ ex.getMessage());
return new ResponseEntity<>(exceptionResult, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(HttpServerErrorException.class)
public ResponseEntity<ExceptionResponse> handlerHttpServerErrorException(HttpServerErrorException ex) {
ExceptionResponse exceptionResult = new ExceptionResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ public class ItemsBorrowJpaEntity extends BaseTime {
private String id;
@OneToOne
@MapsId
private ItemsJpaEntity items;
@JoinColumn(name="item_id")
private ItemsJpaEntity item;
@Enumerated(EnumType.STRING)
@Column(name = "price_standard")
private PriceStandard priceStandard;
@Column(name = "price", nullable = false)
private int price;
@Column(name="deposit",nullable = false)
private int deposit;
@Column(name="start_date",nullable = false)
private LocalDate startDate;
@Column(name = "end_date", nullable = false)
private LocalDate endDate;
@Column(name ="borrow_satus_code")
private String borrowStatusCode;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package site.billbill.apiserver.model.post;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
import site.billbill.apiserver.model.BaseTime;

import java.time.LocalDate;
@DynamicUpdate
@Entity
@Builder
@Table(name = "items_borrow_status")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ItemsBorrowStatusJpaEntity extends BaseTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="status_seq")
private String id;
@ManyToOne
@JoinColumn(name="item_id")
private ItemsJpaEntity item;
@Column(name="start_date",nullable = false)
private LocalDate startDate;
@Column(name = "end_date", nullable = false)
private LocalDate endDate;
@Column(name ="borrow_status_code")
private String borrowStatusCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class ItemsExchangeJpaEntity extends BaseTime {

@ManyToOne
@MapsId
private ItemsJpaEntity items;
@JoinColumn(name="item_id")
private ItemsJpaEntity item;
@Convert(converter = StringListConverter.class)
@JoinColumn(name="wisiList",nullable = false)
String wishList;
Expand Down
Loading

0 comments on commit 2457334

Please sign in to comment.