-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
328 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/site/billbill/apiserver/api/borrowPosts/converter/PostsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/site/billbill/apiserver/api/borrowPosts/dto/request/PostsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/site/billbill/apiserver/api/borrowPosts/dto/response/PostsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/site/billbill/apiserver/common/validation/EnumValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/site/billbill/apiserver/common/validation/EnumValidatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/site/billbill/apiserver/model/post/ItemsBorrowStatusJpaEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.