Skip to content

Commit

Permalink
[DEV-000] 활동보고서 삭제 API 에러 수정 (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoSeonJe authored Oct 6, 2024
1 parent 3d19bdc commit a7335f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ddingdong.ddingdongBE.domain.activityreport.service;

import ddingdong.ddingdongBE.common.exception.PersistenceException.ResourceNotFound;
import ddingdong.ddingdongBE.domain.activityreport.domain.ActivityReport;
import ddingdong.ddingdongBE.domain.activityreport.repository.ActivityReportRepository;
import ddingdong.ddingdongBE.domain.club.entity.Club;
Expand All @@ -25,26 +26,22 @@ public List<ActivityReport> getActivityReportsByClub(final Club club) {
}

public List<ActivityReport> getActivityReport(
final String term,
final String clubName
final String clubName,
final String term
) {
return activityReportRepository.findByClubNameAndTerm(clubName, term);
}

@Transactional
public Long create(final ActivityReport activityReport) {
ActivityReport savedActivityReport = activityReportRepository.save(activityReport);
return savedActivityReport.getId();
public void create(final ActivityReport activityReport) {
activityReportRepository.save(activityReport);
}

@Transactional
public void update(
final String clubName,
final String term,
final List<ActivityReport> activityReports,
final List<ActivityReport> updateActivityReports
) {
List<ActivityReport> activityReports = getActivityReport(term, clubName);

IntStream.range(0, updateActivityReports.size())
.forEach(index -> {
ActivityReport activityReport = activityReports.get(index);
Expand All @@ -57,4 +54,13 @@ public void update(
public void deleteAll(List<ActivityReport> activityReports) {
activityReportRepository.deleteAll(activityReports);
}

public List<ActivityReport> getActivityReportOrThrow(String clubName, String term) {
List<ActivityReport> activityReports = getActivityReport(clubName, term);
if (activityReports.isEmpty()) {
throw new ResourceNotFound("해당 ActivityReports(clubName: " + clubName + ", term: " + term + ")"
+ "를 찾을 수 없습니다.");
}
return activityReports;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public List<ActivityReportQuery> getActivityReport(
String term,
String clubName
) {
List<ActivityReport> activityReports = activityReportService.getActivityReport(term,
clubName);
List<ActivityReport> activityReports = activityReportService.getActivityReport(
clubName,
term);
return activityReports.stream()
.map(this::parseToQuery)
.toList();
Expand Down Expand Up @@ -72,15 +73,14 @@ public void create(
List<MultipartFile> images
) {
Club club = clubService.getByUserId(user.getId());

String term = getRequestTerm(commands);
List<ActivityReport> activityReports = activityReportService.getActivityReport(club.getName(), term);
uploadImages(activityReports, images);

commands.forEach(command -> {
ActivityReport activityReport = command.toEntity(club);
activityReportService.create(activityReport);
});

List<ActivityReport> activityReports = activityReportService.getActivityReport(club.getName(), term);
uploadImages(activityReports, images);
}

@Transactional
Expand All @@ -92,19 +92,21 @@ public void update(
) {
Club club = clubService.getByUserId(user.getId());

List<ActivityReport> activityReports = activityReportService.getActivityReport(club.getName(), term);
List<ActivityReport> activityReports = activityReportService.getActivityReportOrThrow(
club.getName(),
term);
updateImages(activityReports, images);

List<ActivityReport> updateActivityReports = commands.stream()
.map(UpdateActivityReportCommand::toEntity)
.toList();
activityReportService.update(club.getName(), term, updateActivityReports);
activityReportService.update(activityReports, updateActivityReports);
}

@Transactional
public void delete(User user, String term) {
Club club = clubService.getByUserId(user.getId());
List<ActivityReport> activityReports = activityReportService.getActivityReport(
List<ActivityReport> activityReports = activityReportService.getActivityReportOrThrow(
club.getName(),
term);
deleteImages(activityReports);
Expand Down

0 comments on commit a7335f6

Please sign in to comment.