Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/borrow posts #66

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public BaseResponse<PostsResponse.ViewPostResponse> getPostController(@PathVaria

return new BaseResponse<>(postsService.ViewPostService(postId));
}
@DeleteMapping("/{postId}")
public BaseResponse<String> deletePostController(@PathVariable(value = "postId",required = true)String postId){

String userId = "";
if(MDC.get(JWTUtil.MDC_USER_ID) != null) {
userId= MDC.get(JWTUtil.MDC_USER_ID).toString();
}
return new BaseResponse<>(postsService.deletePostService(postId,userId));
}
@PatchMapping("/{postId}")
public BaseResponse<String> updatePostController(@PathVariable(value="postId",required = true)String postId,
@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.UpdatePostService(postId,userId,request));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;
import site.billbill.apiserver.model.post.ItemsBorrowJpaEntity;
import site.billbill.apiserver.model.post.ItemsBorrowStatusJpaEntity;
import site.billbill.apiserver.model.post.ItemsCategoryJpaEntity;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;

Expand All @@ -20,7 +21,7 @@ public static PostsResponse.UploadResponse toUploadResponse(String id){
postId(id).
build();
}
public static ItemsJpaEntity toItem(String postId,PostsRequest.UploadRequest request, UserJpaEntity user){
public static ItemsJpaEntity toItem(String postId, PostsRequest.UploadRequest request, UserJpaEntity user, ItemsCategoryJpaEntity category){
return ItemsJpaEntity.builder()
.id(postId)
.title(request.getTitle())
Expand All @@ -29,6 +30,7 @@ public static ItemsJpaEntity toItem(String postId,PostsRequest.UploadRequest req
.owner(user)
.viewCount(0)
.images(request.getImages())
.category(category)
.itemStatus(request.getItemStatus()).build();
}
public static ItemsBorrowJpaEntity toItemBorrow(ItemsJpaEntity item, PostsRequest.UploadRequest request){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public static class UploadRequest {
private String content;
@Schema(description = "이미지", example = "[\"이미지링크\",\"이미지링크\"]")
private List<String> images;

@Schema(description = "카테고리", example = "camp, sports, tools")
private String category;
private List<NoRentalPeriod> noRental;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public interface PostsService {
PostsResponse.ViewAllResultResponse ViewAllPostService(String category, int page, Sort.Direction direction, String orderType);

PostsResponse.ViewPostResponse ViewPostService(String postId);

String deletePostService(String postId,String userId);
String UpdatePostService(String postId,String userId,PostsRequest.UploadRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
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;
Expand All @@ -18,6 +19,7 @@
import site.billbill.apiserver.exception.CustomException;
import site.billbill.apiserver.model.post.ItemsBorrowJpaEntity;
import site.billbill.apiserver.model.post.ItemsBorrowStatusJpaEntity;
import site.billbill.apiserver.model.post.ItemsCategoryJpaEntity;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.ItemsBorrowRepository;
Expand All @@ -35,7 +37,6 @@
@RequiredArgsConstructor

@Slf4j

public class PostsServiceImpl implements PostsService {

private final UserRepository userRepository;
Expand All @@ -47,13 +48,13 @@ public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest
//먼저 item 생성,
Optional<UserJpaEntity> isUser=userRepository.findById(userId);
String postsId = ULIDUtil.generatorULID("BORROW");

ItemsCategoryJpaEntity category = itemsCategoryRepository.findByName(request.getCategory());
UserJpaEntity user=new UserJpaEntity();
if(isUser.isPresent()){
user=isUser.get();
}
//Item 생성
ItemsJpaEntity newItem= PostsConverter.toItem(postsId,request,user);
ItemsJpaEntity newItem= PostsConverter.toItem(postsId,request,user,category);
itemsRepository.save(newItem);
ItemsJpaEntity item=itemsRepository.findById(postsId).orElse(newItem);
//BorrowItem 생성
Expand Down Expand Up @@ -143,7 +144,53 @@ public PostsResponse.ViewPostResponse ViewPostService(String postId){
}
return PostsConverter.toViewPost(item,borrowItem,noRentalPeriods,status);

}
@Transactional
public String deletePostService(String postId,String userId){
ItemsJpaEntity item= itemsRepository.findById(postId).orElse(null);
UserJpaEntity user=userRepository.findById(userId).orElse(null);
if(item==null){
throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST);
}else if(!item.getOwner().equals(user)){
throw new CustomException(ErrorCode.BadRequest, "해당 게시물 작성자가 아닙니다.", HttpStatus.BAD_REQUEST);
}

item.setDelYn(true);


return "Succes";
}
@Transactional
public String UpdatePostService(String postId,String userId,PostsRequest.UploadRequest request) {
ItemsJpaEntity item = itemsRepository.findById(postId).orElse(null);
UserJpaEntity user = userRepository.findById(userId).orElse(null);
ItemsCategoryJpaEntity category = itemsCategoryRepository.findByName(request.getCategory());
ItemsBorrowJpaEntity borrowItem = itemsBorrowRepository.findById(postId).orElse(null);

if (item == null) {
throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST);
} else if (!item.getOwner().equals(user)) {
throw new CustomException(ErrorCode.BadRequest, "해당 게시물 작성자가 아닙니다.", HttpStatus.BAD_REQUEST);
}

item.setTitle(request.getTitle());
item.setImages(request.getImages());
item.setItemStatus(request.getItemStatus());
item.setCategory(category);
item.setContent(request.getContent());
borrowItem.setDeposit(request.getDeposit());
borrowItem.setPrice(request.getPrice());

List<ItemsBorrowStatusJpaEntity> existingStatuses = itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCode(postId, "RENTAL_NOT_POSSIBLE");
itemsBorrowStatusRepository.deleteAll(existingStatuses);
//대여 불가 날짜 새로 배정, 똑같아도 새로 배정되는 느낌
if (request.getNoRental() != null && !request.getNoRental().isEmpty()) {
List<ItemsBorrowStatusJpaEntity> newStatuses = request.getNoRental().stream()
.map(status -> PostsConverter.toItemBorrowStatus(item, "RENTAL_NOT_POSSIBLE", status))
.toList();
itemsBorrowStatusRepository.saveAll(newStatuses);

}
return "success";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ItemsCategoryJpaEntity {
private String id;

@ManyToOne
@JoinColumn(name="uppder_cat_id",nullable = false)
@JoinColumn(name="upper_cat_id",nullable = false)
private ItemsCategoryJpaEntity upperCategory;

@Column(name="name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import site.billbill.apiserver.model.post.ItemsCategoryJpaEntity;

public interface ItemsCategoryRepository extends JpaRepository<ItemsCategoryJpaEntity,String> {
ItemsCategoryRepository findByName(String name);
ItemsCategoryJpaEntity findByName(String name);
}