Skip to content

Commit

Permalink
Merge pull request #239 from Juinjang/refactor/#238
Browse files Browse the repository at this point in the history
[refactor/#238] 임장 수정 리팩
  • Loading branch information
PicturePark1101 authored Oct 7, 2024
2 parents 6fdef92 + 6fb4485 commit 7550af2
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 193 deletions.
20 changes: 7 additions & 13 deletions src/main/java/umc/th/juinjang/controller/LimjangController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import org.springframework.web.bind.annotation.RestController;
import umc.th.juinjang.apiPayload.ApiResponse;
import umc.th.juinjang.apiPayload.code.status.SuccessStatus;
import umc.th.juinjang.converter.limjang.LimjangPostRequestConverter;
import umc.th.juinjang.model.dto.limjang.enums.LimjangSortOptions;
import umc.th.juinjang.model.dto.limjang.request.LimjangPatchRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangPostRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangUpdateRequestDTO;
import umc.th.juinjang.model.dto.limjang.request.LimjangsDeleteRequest;
import umc.th.juinjang.model.dto.limjang.response.LimjangDetailResponseDTO;
import umc.th.juinjang.model.dto.limjang.response.LimjangPostResponseDTO;
import umc.th.juinjang.model.dto.limjang.response.LimjangPostResponse;
import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO;
import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse;
import umc.th.juinjang.model.dto.limjang.response.LimjangsMainGetResponse;
Expand All @@ -44,8 +43,8 @@ public class LimjangController {
@CrossOrigin
@Operation(summary = "임장 생성 API")
@PostMapping
public ApiResponse<LimjangPostResponseDTO.PostDTO> postLimjang(@RequestBody @Valid LimjangPostRequest postDto, @AuthenticationPrincipal Member member){
return ApiResponse.onSuccess(LimjangPostRequestConverter.toPostDTO(limjangCommandService.postLimjang(postDto, member)));
public ApiResponse<LimjangPostResponse> postLimjang(@RequestBody @Valid LimjangPostRequest postDto, @AuthenticationPrincipal Member member){
return ApiResponse.onSuccess(LimjangPostResponse.of(limjangCommandService.postLimjang(postDto, member)));
}

@CrossOrigin
Expand Down Expand Up @@ -94,15 +93,10 @@ public ApiResponse<LimjangDetailResponseDTO.DetailDto> getDetailLimjang(
}

@CrossOrigin
@Operation(summary = "임장 기본정보 수정", description = "임장 기본정보수정 api입니다. 수정할 정보를 전달해주세요.")
@Operation(summary = "임장 기본정보 수정", description = "임장 기본 정보 수정 api입니다. 수정할 정보를 전달해주세요.")
@PatchMapping("/{limjangId}")
public ApiResponse updateLimjang(
@AuthenticationPrincipal Member member,
@PathVariable(name = "limjangId") @Valid long limjangId,
@RequestBody @Valid
LimjangUpdateRequestDTO.UpdateDto updateLimjang
) {
limjangCommandService.updateLimjang(member.getMemberId(), limjangId, updateLimjang);
public ApiResponse updateLimjang(@AuthenticationPrincipal Member member, @PathVariable(name = "limjangId") long id, @RequestBody @Valid LimjangPatchRequest request) {
limjangCommandService.updateLimjang(member, id, request);
return ApiResponse.onSuccess(SuccessStatus.LIMJANG_UPDATE);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package umc.th.juinjang.model.dto.limjang.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.List;

public record LimjangPatchRequest(
int priceType,
@Size(min = 1, max = 2)
List<String> priceList,
@NotBlank
String address,
String addressDetail,
@NotBlank
String nickname
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.List;
import umc.th.juinjang.validation.annotation.VaildPriceListSize;
import umc.th.juinjang.model.entity.Limjang;
import umc.th.juinjang.model.entity.enums.LimjangPriceType;
import umc.th.juinjang.model.entity.enums.LimjangPropertyType;
import umc.th.juinjang.model.entity.enums.LimjangPurpose;

public record LimjangPostRequest(
@NotNull
Integer purposeType,
Integer propertyType,
Integer priceType,
@VaildPriceListSize(minSize = 1, maxSize = 2)
@Size(min = 1, max = 2)
List<String> price,
@NotBlank
String address,
String addressDetail,
@NotBlank
String nickname
) {
public Limjang toEntity() {
return Limjang.builder()
.purpose(LimjangPurpose.find(purposeType))
.propertyType(LimjangPropertyType.find(propertyType))
.priceType(LimjangPriceType.find(priceType))
.address(address)
.addressDetail(addressDetail)
.nickname(nickname)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
package umc.th.juinjang.model.dto.limjang.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import umc.th.juinjang.validation.annotation.VaildPriceListSize;

public class LimjangUpdateRequestDTO {

@Getter
public static class UpdateDto {

@NotNull
private Integer priceType;

@VaildPriceListSize(minSize = 1, maxSize = 2)
private List<String> priceList;

@NotBlank
private String address;

@NotBlank
private String addressDetail;

@NotBlank
private String nickname;
}

@Getter
public static class LimjangMemoDto{
private String memo;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package umc.th.juinjang.model.dto.limjang.response;

import java.time.LocalDateTime;
import umc.th.juinjang.model.entity.Limjang;

public record LimjangPostResponse(Long limjangId, LocalDateTime createdAt) {
public static LimjangPostResponse of(Limjang limjang) {
return new LimjangPostResponse(limjang.getLimjangId(), limjang.getCreatedAt());
}
}

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/java/umc/th/juinjang/model/entity/Limjang.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ public void removeScrap(){
this.scrap = null;
}

public void updateLimjang(Limjang newLimjang){
this.address = newLimjang.getAddress();
this.addressDetail = newLimjang.getAddressDetail();
this.nickname = newLimjang.getNickname();
this.priceType = newLimjang.getPriceType();
public void updateLimjang(String address, String addressDetail, String nickname, LimjangPriceType priceType){
this.address = address;
this.addressDetail = addressDetail;
this.nickname = nickname;
this.priceType = priceType;
}

public void updateMemo(String memo){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LimjangPrice extends BaseEntity {
@OneToOne(mappedBy = "limjangPrice", cascade = CascadeType.ALL, orphanRemoval = true)
private Limjang limjang;

public void updateLimjangPriceList(LimjangPrice newLimjangPrice){
public void updateLimjangPrice(LimjangPrice newLimjangPrice){
this.marketPrice = newLimjangPrice.getMarketPrice();
this.sellingPrice = newLimjangPrice.getSellingPrice();
this.depositPrice = newLimjangPrice.getDepositPrice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public interface LimjangRepository extends JpaRepository<Limjang, Long>, Limjang
@Query(value = "DELETE FROM limjang l WHERE l.member_id = :memberId", nativeQuery = true)
void deleteAllByMemberId(@Param("memberId") Long memberId);

// void deleteByMember(Member member);
@Query("SELECT l FROM Limjang l join fetch l.limjangPrice WHERE l.limjangId = :id AND l.memberId = :member AND l.deleted = false")
Optional<Limjang> findByLimjangIdAndMemberIdWithLimjangPriceAndDeletedIsFalse(@Param("id") Long id, @Param("member") Member member);
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package umc.th.juinjang.service.LimjangService;

import umc.th.juinjang.model.dto.limjang.request.LimjangPatchRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangPostRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangUpdateRequestDTO;
import umc.th.juinjang.model.dto.limjang.request.LimjangsDeleteRequest;
Expand All @@ -12,5 +13,5 @@ public interface LimjangCommandService {

void deleteLimjangs(LimjangsDeleteRequest deleteIds, Member member);

void updateLimjang(long memberId, long limjangId, LimjangUpdateRequestDTO.UpdateDto requestUpdateInfo);
void updateLimjang(Member member, long limjangId, LimjangPatchRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import umc.th.juinjang.apiPayload.code.status.ErrorStatus;
import umc.th.juinjang.apiPayload.exception.handler.LimjangHandler;
import umc.th.juinjang.apiPayload.exception.handler.MemberHandler;
import umc.th.juinjang.converter.limjang.LimjangPostRequestConverter;
import umc.th.juinjang.converter.limjang.LimjangUpdateConverter;
import umc.th.juinjang.model.dto.limjang.request.LimjangPatchRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangPostRequest;
import umc.th.juinjang.model.dto.limjang.request.LimjangUpdateRequestDTO;
import umc.th.juinjang.model.dto.limjang.request.LimjangsDeleteRequest;
import umc.th.juinjang.model.entity.Limjang;
import umc.th.juinjang.model.entity.LimjangPrice;
import umc.th.juinjang.model.entity.Member;
import umc.th.juinjang.model.entity.enums.LimjangPriceType;
import umc.th.juinjang.repository.limjang.LimjangRepository;
import umc.th.juinjang.repository.limjang.MemberRepository;

Expand All @@ -28,17 +27,16 @@ public class LimjangCommandServiceImpl implements LimjangCommandService {

private final LimjangRepository limjangRepository;
private final MemberRepository memberRepository;
private final LimjangRetriever limjangRetriever;

@Override
@Transactional
public Limjang postLimjang(LimjangPostRequest postDto, Member member) {
Limjang limjang = LimjangPostRequestConverter.toLimjang(postDto);
limjang.saveMemberAndPrice(findMemberById(member), findLimajngPrice(postDto));
Limjang limjang = postDto.toEntity();
limjang.saveMemberAndPrice(getMemberById(member), getLimjangPrice(postDto.price(), postDto.purposeType(), postDto.priceType()));
return limjangRepository.save(limjang);
}

private Member findMemberById(Member member) {
private Member getMemberById(Member member) {
return memberRepository.findById(member.getMemberId())
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
}
Expand All @@ -52,7 +50,7 @@ public void deleteLimjangs(LimjangsDeleteRequest requestIds, Member member) {
}

private void checkLimjangsExistence(List<Long> ids, Member member) {
if (isRequestSizeMismatch(ids, limjangRetriever.findAllByIdsInAndMemberAndDeletedIsFalse(ids, member))) {
if (isRequestSizeMismatch(ids, limjangRepository.findAllByLimjangIdInAndMemberIdAndDeletedIsFalse(ids, member))) {
throw new LimjangHandler(ErrorStatus.LIMJANG_NOTFOUND_ERROR);
}
}
Expand All @@ -63,30 +61,18 @@ private boolean isRequestSizeMismatch(List<Long> ids, List<Limjang> findLimjangs

@Override
@Transactional
public void updateLimjang(long memberId, long limjangId, LimjangUpdateRequestDTO.
UpdateDto requestUpdateInfo) {

List<String> newPriceList = requestUpdateInfo.getPriceList();
// 임장 찾기
Limjang originalLimjang = limjangRepository.findByIdWithLimjangPrice(memberId, limjangId);
// 임장 가격
LimjangPrice findLimjangPrice = originalLimjang.getLimjangPrice();

// 새 정보
Limjang newLimjang = LimjangUpdateConverter.toLimjang(requestUpdateInfo);
LimjangPrice newLimjangPrice = determineLimjangPrice(newPriceList, originalLimjang.getPurpose().getValue(), newLimjang.getPriceType().getValue());

//업데이트
originalLimjang.updateLimjang(newLimjang);
findLimjangPrice.updateLimjangPriceList(newLimjangPrice);
public void updateLimjang(Member member, long id, LimjangPatchRequest request) {
Limjang limjang = getByMemberAndIdWithLimjangPrice(member, id);
limjang.updateLimjang(request.address(), request.addressDetail(), request.nickname(), LimjangPriceType.find(request.priceType()));
limjang.getLimjangPrice().updateLimjangPrice(getLimjangPrice(request.priceList(), limjang.getPurpose().getValue(), request.priceType()));
}

private LimjangPrice findLimajngPrice (LimjangPostRequest postDto) {

List<String> priceList = postDto.price();
Integer purpose = postDto.purposeType();
Integer priceType = postDto.priceType();

private LimjangPrice getLimjangPrice(List<String> priceList, int purpose, int priceType) {
return determineLimjangPrice(priceList, purpose, priceType);
}

private Limjang getByMemberAndIdWithLimjangPrice(Member member, long id) {
return limjangRepository.findByLimjangIdAndMemberIdWithLimjangPriceAndDeletedIsFalse(id, member)
.orElseThrow(() -> new LimjangHandler(ErrorStatus.LIMJANG_NOTFOUND_ERROR));
}
}
Loading

0 comments on commit 7550af2

Please sign in to comment.