Skip to content

Commit

Permalink
[feat] 수정 및 삭제 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jainefer committed Nov 30, 2024
1 parent df52081 commit 3812ff1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ public BaseResponse<String> deletePostController(@PathVariable(value = "postId",
}
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 @@ -12,4 +12,5 @@ public interface PostsService {
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 @@ -144,7 +145,7 @@ 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);
Expand All @@ -154,8 +155,42 @@ public String deletePostService(String postId,String userId){
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);
}

0 comments on commit 3812ff1

Please sign in to comment.