Skip to content

Commit

Permalink
Merge pull request #231 from Juinjang/refactor/#230
Browse files Browse the repository at this point in the history
[refactor/#230] 임장 일괄삭제 리팩토링
  • Loading branch information
PicturePark1101 authored Sep 21, 2024
2 parents a78094d + ff43093 commit 3cc0821
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 53 deletions.
12 changes: 4 additions & 8 deletions src/main/java/umc/th/juinjang/controller/LimjangController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
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.LimjangDeleteRequestDTO;
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.LimjangTotalListResponseDTO;
Expand Down Expand Up @@ -66,13 +66,9 @@ public ApiResponse<LimjangsMainGetResponse> getRecentUpdateList(

@CrossOrigin
@Operation(summary = "임장 선택 삭제", description = "임장 게시글을 여러 개 선택해서 삭제하는 api입니다.")
@DeleteMapping("")
public ApiResponse deleteLimjang(@RequestBody @Valid LimjangDeleteRequestDTO.DeleteDto deleteIds,
@AuthenticationPrincipal Member member
// public ApiResponse deleteLimjang(@PathVariable(name = "limjangIds") @Valid List<Long> deleteIds
){
System.out.println("임장 선택 삭제 controller 입니다");
limjangCommandService.deleteLimjangs(deleteIds);
@DeleteMapping
public ApiResponse deleteLimjang(@RequestBody @Valid LimjangsDeleteRequest deleteIds, @AuthenticationPrincipal Member member){
limjangCommandService.deleteLimjangs(deleteIds, member);
return ApiResponse.onSuccess(SuccessStatus.LIMJANG_DELETE);
}

Expand Down

This file was deleted.

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

import jakarta.validation.constraints.NotEmpty;
import java.util.List;

public record LimjangsDeleteRequest(
@NotEmpty List<Long> limjangIdList
) {
}
2 changes: 1 addition & 1 deletion src/main/java/umc/th/juinjang/model/entity/Limjang.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class Limjang extends BaseEntity {
@Column(nullable = false, name = "deleted")
private boolean deleted = Boolean.FALSE;

public void postLimjang(Member member, LimjangPrice limjangPrice){
public void saveMemberAndPrice(Member member, LimjangPrice limjangPrice){
this.limjangPrice = limjangPrice;
this.memberId = member;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import javax.swing.text.html.Option;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -20,9 +21,13 @@ public interface LimjangRepository extends JpaRepository<Limjang, Long>, Limjang
@Query(value = "SELECT * FROM limjang l WHERE l.member_id = :memberId", nativeQuery = true)
List<Limjang> findLimjangByMemberIdIgnoreDeleted(@Param("memberId") Long memberId);

Optional<Limjang> findByLimjangIdAndMemberIdAndDeletedIsFalse(Long id, Member member);

List<Limjang> findAllByLimjangIdInAndMemberIdAndDeletedIsFalse(List<Long> id, Member member);

@Modifying
@Query("UPDATE Limjang l SET l.deleted = true, l.updatedAt = CURRENT_TIMESTAMP WHERE l.limjangId = :limjangId")
void softDeleteById(@Param("limjangId") Long limjangId);
@Query("UPDATE Limjang l SET l.deleted = true WHERE l.limjangId in :ids")
void softDeleteByIds(@Param("ids") List<Long> ids);

@Modifying
@Query(value ="DELETE FROM limjang l WHERE l.deleted = true AND l.updated_at < :dateTime", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package umc.th.juinjang.service.LimjangService;

import umc.th.juinjang.model.dto.limjang.request.LimjangDeleteRequestDTO;
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.Member;

public interface LimjangCommandService {

Limjang postLimjang(LimjangPostRequest request, Member member);

void deleteLimjangs(LimjangDeleteRequestDTO.DeleteDto deleteIds);
void deleteLimjangs(LimjangsDeleteRequest deleteIds, Member member);

void updateLimjang(long memberId, long limjangId, LimjangUpdateRequestDTO.UpdateDto requestUpdateInfo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import static umc.th.juinjang.service.LimjangService.LimjangPriceBridge.determineLimjangPrice;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.LimjangDeleteRequestDTO;
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;
Expand All @@ -37,7 +34,7 @@ public class LimjangCommandServiceImpl implements LimjangCommandService {
@Transactional
public Limjang postLimjang(LimjangPostRequest postDto, Member member) {
Limjang limjang = LimjangPostRequestConverter.toLimjang(postDto);
limjang.postLimjang(findMemberById(member), findLimajngPrice(postDto));
limjang.saveMemberAndPrice(findMemberById(member), findLimajngPrice(postDto));
return limjangRepository.save(limjang);
}

Expand All @@ -48,31 +45,26 @@ private Member findMemberById(Member member) {

@Override
@Transactional
public void deleteLimjangs(LimjangDeleteRequestDTO.DeleteDto deleteIds) {

System.out.println("임장 선택 삭제 service 입니다");
List<Long> findIdList = new ArrayList<>();
public void deleteLimjangs(LimjangsDeleteRequest requestIds, Member member) {
List<Long> ids = requestIds.limjangIdList();
checkLimjangsExistence(ids, member);
limjangRepository.softDeleteByIds(ids);
}

for (Long id : deleteIds.getLimjangIdList()){
findIdList.add(limjangRetriever.findById(id).getLimjangId());
System.out.println("삭제할 임장 id : : "+id);
private void checkLimjangsExistence(List<Long> ids, Member member) {
if (isRequestSizeMismatch(ids, limjangRetriever.findAllByIdsInAndMemberAndDeletedIsFalse(ids, member))) {
throw new LimjangHandler(ErrorStatus.LIMJANG_NOTFOUND_ERROR);
}
}

try {
for (Long id : findIdList){
System.out.println("try문 안 -- 삭제할 임장 id : : "+id);
limjangRepository.softDeleteById(id);
}
} catch (DataIntegrityViolationException e) {
throw new LimjangHandler(ErrorStatus.LIMJANG_DELETE_NOT_COMPLETE);
} catch (EmptyResultDataAccessException e) {
throw new LimjangHandler(ErrorStatus.LIMJANG_DELETE_NOT_FOUND);
}
private boolean isRequestSizeMismatch(List<Long> ids, List<Limjang> findLimjangs) {
return ids.size() != findLimjangs.size();
}

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

List<String> newPriceList = requestUpdateInfo.getPriceList();
// 임장 찾기
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package umc.th.juinjang.service.LimjangService;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import umc.th.juinjang.apiPayload.code.status.ErrorStatus;
import umc.th.juinjang.apiPayload.exception.handler.LimjangHandler;
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.repository.limjang.LimjangRepository;

@Component
Expand All @@ -15,8 +16,11 @@ public class LimjangRetriever {
private final LimjangRepository limjangRepository;

public Limjang findById(long limjangId) {

return limjangRepository.findById(limjangId)
.orElseThrow(() -> new LimjangHandler(ErrorStatus.LIMJANG_NOTFOUND_ERROR));
}
}

public List<Limjang> findAllByIdsInAndMemberAndDeletedIsFalse(List<Long> ids, Member member) {
return limjangRepository.findAllByLimjangIdInAndMemberIdAndDeletedIsFalse(ids, member);
}
}

0 comments on commit 3cc0821

Please sign in to comment.