Skip to content

Commit

Permalink
Merge branch 'dev' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyuung committed Jul 23, 2024
2 parents 4333f44 + a7ad153 commit 80e28b4
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 2 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions db/src/main/java/db/domain/usedgoods/UsedGoodsEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package db.domain.usedgoods;

import db.common.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "usedgoods")
@SuperBuilder
public class UsedGoodsEntity extends BaseEntity {

@Column(nullable = false, length = 50,columnDefinition = "VARCHAR(50)")
private String title;

private int price;

@Column(nullable = false, length = 1000)
private String description;

private LocalDateTime postedAt;

@Column(nullable = false)
private Long userId;

}

7 changes: 7 additions & 0 deletions db/src/main/java/db/domain/usedgoods/UsedGoodsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package db.domain.usedgoods;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UsedGoodsRepository extends JpaRepository<UsedGoodsEntity, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ private void checkEmptyGoodsListWithThrow(List<GoodsEntity> goodsList) {
}
}

public void setGoodsStatusBy(List<Long> goodsIdList, GoodsStatus status) {
List<GoodsEntity> goodsEntityList = goodsRepository.findAllByIdIn(goodsIdList);
goodsEntityList.forEach(goodsEntity -> {
goodsEntity.setStatus(status);
goodsRepository.save(goodsEntity);
});
}

public List<GoodsEntity> findAllByGoodsStatusWithThrow(GoodsStatus status) {
List<GoodsEntity> goodsEntityList = goodsRepository.findAllByStatusOrderByIdDesc(status);
Expand Down Expand Up @@ -137,5 +144,5 @@ public List<GoodsEntity> findAllByTakeBackIdWithThrow(Long takeBackId) {
}
return goodsEntityList;
}*/

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package warehouse.domain.usedgoods.business;

import db.domain.goods.GoodsEntity;
import db.domain.goods.enums.GoodsStatus;
import db.domain.image.ImageEntity;
import db.domain.image.enums.ImageKind;
import db.domain.usedgoods.UsedGoodsEntity;
import global.annotation.Business;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import warehouse.domain.goods.controller.model.GoodsResponse;
import warehouse.domain.goods.converter.GoodsConverter;
import warehouse.domain.goods.service.GoodsService;
import warehouse.domain.image.controller.model.ImageListResponse;
import warehouse.domain.image.converter.ImageConverter;
import warehouse.domain.image.service.ImageService;
import warehouse.domain.usedgoods.controller.model.request.UsedGoodsPostRequest;
import warehouse.domain.usedgoods.controller.model.response.TransformUsedGoodsResponse;
import warehouse.domain.usedgoods.controller.model.response.UsedGoodsPostResponse;
import warehouse.domain.usedgoods.converter.UsedGoodsConverter;
import warehouse.domain.usedgoods.service.UsedGoodsService;
import warehouse.domain.users.service.UsersService;

@Business
@RequiredArgsConstructor
@Slf4j
public class UsedGoodsBusiness {

private final GoodsService goodsService;
private final UsersService usersService;
private final UsedGoodsService usedGoodsService;
private final ImageService imageService;

private final UsedGoodsConverter usedGoodsConverter;
private final ImageConverter imageConverter;
private final GoodsConverter goodsConverter;

public TransformUsedGoodsResponse transformUsedGoods(Long goodsId) {

// GoodsStatus 가 STORAGE 가 아닌 경우 예외
goodsService.checkStoredGoodsAndStatusWithThrowBy(List.of(goodsId), GoodsStatus.STORAGE);

// GoodsStatus 를 USED 로 변경
goodsService.setGoodsStatusBy(List.of(goodsId), GoodsStatus.USED);

GoodsEntity goodsEntity = goodsService.getGoodsListBy(goodsId);

return usedGoodsConverter.toResponse(goodsEntity);
}

public List<TransformUsedGoodsResponse> transformUsedGoods(List<Long> goodsIdList) {

goodsService.checkStoredGoodsAndStatusWithThrowBy(goodsIdList, GoodsStatus.STORAGE);

goodsService.setGoodsStatusBy(goodsIdList, GoodsStatus.USED);

List<GoodsEntity> goodsEntityList = goodsService.getGoodsListBy(goodsIdList);

return usedGoodsConverter.toResponse(goodsEntityList);
}

public TransformUsedGoodsResponse cancelUsedGoodsRequest(Long goodsId) {

goodsService.checkStoredGoodsAndStatusWithThrowBy(goodsId, GoodsStatus.USED);

goodsService.setGoodsStatusBy(List.of(goodsId), GoodsStatus.STORAGE);

GoodsEntity goodsEntity = goodsService.getGoodsListBy(goodsId);

return usedGoodsConverter.toResponse(goodsEntity);
}

public List<TransformUsedGoodsResponse> cancelUsedGoodsRequest(List<Long> goodsIdList) {

goodsService.checkStoredGoodsAndStatusWithThrowBy(goodsIdList, GoodsStatus.USED);

goodsService.setGoodsStatusBy(goodsIdList, GoodsStatus.STORAGE);

List<GoodsEntity> goodsEntityList = goodsService.getGoodsListBy(goodsIdList);

return usedGoodsConverter.toResponse(goodsEntityList);
}

public UsedGoodsPostResponse post(UsedGoodsPostRequest request, String email) {

goodsService.checkStoredGoodsAndStatusWithThrowBy(request.getGoodsId(), GoodsStatus.USED);

Long userId = usersService.getUserWithThrow(email).getId();

UsedGoodsEntity usedGoodsEntity = usedGoodsConverter.toEntity(request);

UsedGoodsEntity savedEntity = usedGoodsService.post(usedGoodsEntity, userId);

GoodsEntity goodsEntity = goodsService.getGoodsListBy(request.getGoodsId());

GoodsResponse goodsResponse = getGoodsResponse(
goodsEntity);

return usedGoodsConverter.toResponse(savedEntity, goodsResponse);
}


private GoodsResponse getGoodsResponse(GoodsEntity goodsEntity) {
List<ImageEntity> basicImageEntityList = imageService.getImageUrlListBy(goodsEntity.getId(),
ImageKind.BASIC);
List<ImageEntity> faultImageEntityList = imageService.getImageUrlListBy(goodsEntity.getId(),
ImageKind.FAULT);
ImageListResponse imageListResponse = imageConverter.toImageListResponse(
basicImageEntityList, faultImageEntityList);
return goodsConverter.toResponse(goodsEntity, imageListResponse);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package warehouse.domain.usedgoods.controller;

import global.annotation.ApiValid;
import global.api.Api;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import warehouse.domain.usedgoods.business.UsedGoodsBusiness;
import warehouse.domain.usedgoods.controller.model.request.UsedGoodsPostRequest;
import warehouse.domain.usedgoods.controller.model.response.TransformUsedGoodsResponse;
import warehouse.domain.usedgoods.controller.model.response.UsedGoodsPostResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/usedgoods")
public class UsedGoodsApiController {

private final UsedGoodsBusiness usedGoodsBusiness;

@PostMapping("/{goodsId}")
public Api<TransformUsedGoodsResponse> transformUsedGoods(@PathVariable Long goodsId) {
TransformUsedGoodsResponse response = usedGoodsBusiness.transformUsedGoods(goodsId);
return Api.OK(response);
}

@PostMapping
public Api<List<TransformUsedGoodsResponse>> transformUsedGoods(
@RequestBody Api<List<Long>> goodsIdList
) {
List<TransformUsedGoodsResponse> responses = usedGoodsBusiness.transformUsedGoods(
goodsIdList.getBody());
return Api.OK(responses);
}

@PostMapping("/cancel/{goodsId}")
public Api<TransformUsedGoodsResponse> cancelUsedGoodsRequest(@PathVariable Long goodsId) {
TransformUsedGoodsResponse response = usedGoodsBusiness.cancelUsedGoodsRequest(goodsId);
return Api.OK(response);
}

@PostMapping("/cancel")
public Api<List<TransformUsedGoodsResponse>> cancelUsedGoodsRequest(
@RequestBody Api<List<Long>> goodsIdList) {
List<TransformUsedGoodsResponse> response = usedGoodsBusiness.cancelUsedGoodsRequest(
goodsIdList.getBody());
return Api.OK(response);
}

@PostMapping("/post")
public Api<UsedGoodsPostResponse> post(@AuthenticationPrincipal User user,
@RequestBody @ApiValid Api<UsedGoodsPostRequest> request) {
UsedGoodsPostResponse response = usedGoodsBusiness.post(
request.getBody(), user.getUsername());
return Api.OK(response);
}

}
Loading

0 comments on commit 80e28b4

Please sign in to comment.