Skip to content

Commit

Permalink
feat: 소속 삭제 api를 구현한다. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarltj authored Jul 21, 2024
1 parent 4e52120 commit 348cec7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ public void blockMember(
public List<AgencyResponse> getMyAgency(@AuthenticationPrincipal JwtAuthentication user) {
return agencyService.getMyAgency(user.getId());
}

@Operation(summary = "소속 삭제")
@DeleteMapping("/{agencyId}")
public void delete(
@AuthenticationPrincipal JwtAuthentication user,
@PathVariable("agencyId") Long agencyId
) {
agencyService.delete(user.getId(), agencyId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.moneymong.domain.agency.api.response.*;
import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.AgencyUser;
import com.moneymong.domain.agency.entity.enums.AgencyType;
import com.moneymong.domain.agency.entity.enums.AgencyUserRole;
import com.moneymong.domain.agency.exception.BlockedAgencyUserException;
import com.moneymong.domain.agency.repository.AgencyRepository;
Expand All @@ -17,6 +16,7 @@
import com.moneymong.domain.invitationcode.repository.InvitationCodeRepository;
import com.moneymong.domain.ledger.entity.Ledger;
import com.moneymong.domain.ledger.repository.LedgerRepository;
import com.moneymong.domain.ledger.service.manager.LedgerService;
import com.moneymong.domain.user.entity.UserUniversity;
import com.moneymong.domain.user.repository.UserUniversityRepository;
import com.moneymong.global.exception.custom.NotFoundException;
Expand Down Expand Up @@ -45,6 +45,7 @@ public class AgencyService {
private final LedgerRepository ledgerRepository;
private final InvitationCodeRepository invitationCodeRepository;
private final InvitationCodeCertificationRepository invitationCodeCertificationRepository;
private final LedgerService ledgerService;

public SearchAgencyResponse getAgencyList(Long userId, Pageable pageable) {
String universityName = getUniversityName(userId);
Expand Down Expand Up @@ -133,4 +134,16 @@ public List<AgencyResponse> getMyAgency(Long userId) {
.map(AgencyResponse::from)
.toList();
}

@Transactional
public void delete(Long userId, Long agencyId) {
validateAgencyUserRole(userId, agencyId);

Agency agency = agencyRepository.findById(agencyId)
.orElseThrow(() -> new NotFoundException(ErrorCode.AGENCY_NOT_FOUND));

ledgerService.deleteLedger(agencyId);

agencyRepository.delete(agency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ private LedgerDetail getLedgerDetail(Long ledgerDetailId) {
.findById(ledgerDetailId)
.orElseThrow(() -> new NotFoundException(ErrorCode.LEDGER_DETAIL_NOT_FOUND));
}

public void deleteLedger(Long agencyId) {
Ledger ledger = ledgerRepository.findByAgencyId(agencyId)
.orElseThrow(() -> new NotFoundException(ErrorCode.LEDGER_NOT_FOUND));

List<LedgerDetail> ledgerDetails = ledgerDetailRepository.findAllByLedger(ledger);

ledgerDetailRepository.deleteAll(ledgerDetails);
ledgerRepository.delete(ledger);
}
}

0 comments on commit 348cec7

Please sign in to comment.