Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : 활동보고서 API 파일 null 체킹 안 되는 에러 해결 #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -36,8 +37,6 @@
@RequestMapping("/server/club")
public class ClubActivityReportApiController {

private static final int IMAGE_COUNT = 2;

private final ActivityReportService activityReportService;
private final FileService fileService;

Expand All @@ -48,83 +47,100 @@ public CurrentTermResponse getCurrentTerm() {

@GetMapping("/my/activity-reports")
public List<AllActivityReportResponse> getMyActivityReports(
@AuthenticationPrincipal PrincipalDetails principalDetails
@AuthenticationPrincipal PrincipalDetails principalDetails
) {
User user = principalDetails.getUser();
return activityReportService.getMyActivityReports(user);
}

@GetMapping("/activity-reports")
public List<DetailActivityReportResponse> getActivityReport(
@RequestParam("term") String term,
@RequestParam("club_name") String clubName
@RequestParam("term") String term,
@RequestParam("club_name") String clubName
) {
return activityReportService.getActivityReport(term, clubName);
}

@PostMapping("/my/activity-reports")
public void registerReport(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestPart(value = "reportData", required = false) List<RegisterActivityReportRequest> requests,
@RequestPart(value = "uploadFiles1", required = false) List<MultipartFile> imageA,
@RequestPart(value = "uploadFiles2", required = false) List<MultipartFile> imageB
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestPart(value = "reportData", required = false) List<RegisterActivityReportRequest> requests,
@RequestPart(value = "uploadFiles1", required = false) MultipartFile firstImage,
@RequestPart(value = "uploadFiles2", required = false) MultipartFile secondImage
) {
User user = principalDetails.getUser();

IntStream.range(0, requests.size())
.forEach(index -> {
RegisterActivityReportRequest request = requests.get(index);
Long registeredActivityReportId = activityReportService.register(user, request);
if (index == 0 && imageA != null) {
fileService.uploadFile(registeredActivityReportId, Collections.singletonList(imageA.get(0)),
IMAGE, ACTIVITY_REPORT);
}
if (index == 1 && imageB != null) {
fileService.uploadFile(registeredActivityReportId, Collections.singletonList(imageB.get(0)),
IMAGE, ACTIVITY_REPORT);
}
});
.forEach(index -> {

RegisterActivityReportRequest request = requests.get(index);
Long registeredActivityReportId = activityReportService.register(user, request);

if (index == 0 && !firstImage.isEmpty()) {
fileService.uploadFile(registeredActivityReportId,
Collections.singletonList(firstImage),
IMAGE, ACTIVITY_REPORT);
}

if (index == 1 && !secondImage.isEmpty()) {
fileService.uploadFile(registeredActivityReportId,
Collections.singletonList(secondImage),
IMAGE, ACTIVITY_REPORT);
}
});
}

@PatchMapping("my/activity-reports")
public void updateReport(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestParam("term") String term,
@RequestPart(value = "reportData", required = false) List<UpdateActivityReportRequest> requests,
@RequestPart(value = "uploadFiles1", required = false) List<MultipartFile> imageA,
@RequestPart(value = "uploadFiles2", required = false) List<MultipartFile> imageB
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestParam("term") String term,
@RequestPart(value = "reportData", required = false) List<UpdateActivityReportRequest> requests,
@RequestPart(value = "uploadFiles1", required = false) MultipartFile firstImage,
@RequestPart(value = "uploadFiles2", required = false) MultipartFile secondImage
) {
User user = principalDetails.getUser();

List<ActivityReportDto> updateActivityReportDtos = activityReportService.update(user, term, requests);
List<ActivityReportDto> updateActivityReportDtos = activityReportService.update(user, term,
requests);

IntStream.range(0, updateActivityReportDtos.size())
.forEach(index -> {
if (index == 0) {
fileService.deleteFile(updateActivityReportDtos.get(index).getId(), IMAGE, ACTIVITY_REPORT);
fileService.uploadFile(updateActivityReportDtos.get(index).getId(), imageA, IMAGE,
ACTIVITY_REPORT);
}
if (index == 1) {
fileService.deleteFile(updateActivityReportDtos.get(index).getId(), IMAGE, ACTIVITY_REPORT);
fileService.uploadFile(updateActivityReportDtos.get(index).getId(), imageB, IMAGE,
ACTIVITY_REPORT);
}
.forEach(index -> {
if (index == 0) {
fileService.deleteFile(updateActivityReportDtos.get(index).getId(), IMAGE,
ACTIVITY_REPORT);

if (!firstImage.isEmpty()) {
fileService.uploadFile(updateActivityReportDtos.get(index).getId(), Collections.singletonList(firstImage),
IMAGE,
ACTIVITY_REPORT);
}
);
}
if (index == 1) {
fileService.deleteFile(updateActivityReportDtos.get(index).getId(), IMAGE,
ACTIVITY_REPORT);

if (!secondImage.isEmpty()) {
fileService.uploadFile(updateActivityReportDtos.get(index).getId(), Collections.singletonList(secondImage),
IMAGE,
ACTIVITY_REPORT);
}
}
}
);
}

@DeleteMapping("my/activity-reports")
public void deleteReport(
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestParam("term") String term
@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestParam("term") String term
) {
User user = principalDetails.getUser();
List<ActivityReportDto> deleteActivityReportDtos = activityReportService.delete(user, term);

deleteActivityReportDtos
.forEach(
activityReportDto -> fileService.deleteFile(activityReportDto.getId(), IMAGE, ACTIVITY_REPORT)
);
.forEach(
activityReportDto -> fileService.deleteFile(activityReportDto.getId(), IMAGE,
ACTIVITY_REPORT)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,27 @@ public List<AllActivityReportResponse> getAll() {
public List<AllActivityReportResponse> getMyActivityReports(final User user) {
Club club = clubService.findClubByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubName(club.getName());
List<ActivityReport> activityReports = activityReportRepository.findByClubName(
club.getName());

return parseToActivityReportResponse(activityReports);
}

@Transactional(readOnly = true)
public List<DetailActivityReportResponse> getActivityReport(final String term, final String clubName) {
List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(clubName, term);
public List<DetailActivityReportResponse> getActivityReport(final String term,
final String clubName) {
List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(
clubName, term);

return activityReports.stream().map(activityReport -> {
List<String> imageUrls = fileInformationService.getImageUrls(IMAGE.getFileType() + ACTIVITY_REPORT.getFileDomain() + activityReport.getId());
List<String> imageUrls = fileInformationService.getImageUrls(
IMAGE.getFileType() + ACTIVITY_REPORT.getFileDomain() + activityReport.getId());
return DetailActivityReportResponse.from(activityReport, imageUrls);
}).collect(Collectors.toList());
}

public Long register(final User user, final RegisterActivityReportRequest registerActivityReportRequest) {
public Long register(final User user,
final RegisterActivityReportRequest registerActivityReportRequest) {

Club club = clubService.findClubByUserId(user.getId());
ActivityReport activityReport = registerActivityReportRequest.toEntity(club);
Expand All @@ -84,25 +89,29 @@ public Long register(final User user, final RegisterActivityReportRequest regist
}

public List<ActivityReportDto> update(final User user, final String term,
final List<UpdateActivityReportRequest> requests) {
final List<UpdateActivityReportRequest> requests) {
Club club = clubService.findClubByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(club.getName(), term);
List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(
club.getName(), term);

return IntStream.range(0, activityReports.size()).mapToObj(index -> {
activityReports.get(index).update(requests.get(index));
return ActivityReportDto.from(activityReports.get(index));
}).collect(Collectors.toList());
return IntStream.range(0, activityReports.size())
.mapToObj(index -> {
activityReports.get(index).update(requests.get(index));
return ActivityReportDto.from(activityReports.get(index));
}).collect(Collectors.toList());
}

public List<ActivityReportDto> delete(final User user, final String term) {
Club club = clubService.findClubByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(club.getName(), term);
List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(
club.getName(), term);

return activityReports.stream()
.peek(activityReport -> activityReport.getParticipants().clear())
.peek(activityReportRepository::delete).map(ActivityReportDto::from).collect(Collectors.toList());
.peek(activityReport -> activityReport.getParticipants().clear())
.peek(activityReportRepository::delete).map(ActivityReportDto::from)
.collect(Collectors.toList());
}

public CurrentTermResponse getCurrentTerm() {
Expand All @@ -114,7 +123,8 @@ public CurrentTermResponse getCurrentTerm() {
}

private int calculateGapOfDays(final LocalDate startDate, final LocalDate currentDate) {
return (int) Duration.between(startDate.atStartOfDay(), currentDate.atStartOfDay()).toDays();
return (int) Duration.between(startDate.atStartOfDay(), currentDate.atStartOfDay())
.toDays();
}

private String calculateCurrentTerm(final int days) {
Expand All @@ -127,20 +137,22 @@ private String calculateCurrentTerm(final int days) {
return String.valueOf(result);
}

private List<AllActivityReportResponse> parseToActivityReportResponse(final List<ActivityReport> activityReports) {
private List<AllActivityReportResponse> parseToActivityReportResponse(
final List<ActivityReport> activityReports) {
Map<String, Map<String, List<Long>>> groupedData = activityReports.stream().collect(
Collectors.groupingBy(activityReport -> activityReport.getClub().getName(),
Collectors.groupingBy(ActivityReport::getTerm,
Collectors.mapping(ActivityReport::getId, Collectors.toList()))));
Collectors.groupingBy(activityReport -> activityReport.getClub().getName(),
Collectors.groupingBy(ActivityReport::getTerm,
Collectors.mapping(ActivityReport::getId, Collectors.toList()))));

return groupedData.entrySet().stream().flatMap(entry -> {
String clubName = entry.getKey();
Map<String, List<Long>> termMap = entry.getValue();

return termMap.entrySet().stream().map(termEntry -> {
String term = termEntry.getKey();
List<ActivityReportDto> activityReportDtos = termEntry.getValue().stream().map(ActivityReportDto::new)
.collect(Collectors.toList());
List<ActivityReportDto> activityReportDtos = termEntry.getValue().stream()
.map(ActivityReportDto::new)
.collect(Collectors.toList());
return AllActivityReportResponse.of(clubName, term, activityReportDtos);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import ddingdong.ddingdongBE.domain.club.entity.ClubMember;
import ddingdong.ddingdongBE.domain.club.repository.ClubMemberRepository;
import ddingdong.ddingdongBE.domain.club.repository.ClubRepository;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -19,6 +20,7 @@ public class FileService {

private final FileInformationService fileInformationService;
private final FileStore fileStore;

public void uploadFile(Long parentId, List<MultipartFile> files, FileTypeCategory fileTypeCategory, FileDomainCategory fileDomainCategory) {
if (files != null && !files.isEmpty()) {
List<UploadFileDto> uploadFileDtos = fileStore.storeFile(files, fileTypeCategory.getFileType(), fileDomainCategory.getFileDomain());
Expand Down
Loading