Skip to content

Commit

Permalink
[UPDATE] service to use isMine()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhhong0509 committed May 5, 2021
1 parent be708b4 commit 0371f7c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ProfilePageResponse getProfile(String email) {
public ProfileReportsResponse getReport(String userEmail, Pageable page) {
User user = userFacade.createUser(userEmail);

Page<Report> reportPage = reportCustomRepository.findAllByMembersContainsAndIsAcceptedAndIsSubmittedTrueAndReportTypeAccessOrderByReportIdDesc(user, Access.EVERY, page);
Page<Report> reportPage = reportCustomRepository.findAllByMembersContainsAndIsAcceptedAndIsSubmittedTrueAndReportTypeAccessOrderByReportIdDesc(user, page);
List<ProfileReportResponse> reportResponses = new ArrayList<>();

for(Report report : reportPage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dsmpear.main.user_backend_v2.service.report;

import com.dsmpear.main.user_backend_v2.entity.report.Report;
import com.dsmpear.main.user_backend_v2.entity.report.ReportRepository;
import com.dsmpear.main.user_backend_v2.entity.report.repository.ReportRepository;
import com.dsmpear.main.user_backend_v2.entity.report.enums.Access;
import com.dsmpear.main.user_backend_v2.entity.report.enums.Field;
import com.dsmpear.main.user_backend_v2.entity.report.enums.Grade;
Expand Down Expand Up @@ -37,19 +37,20 @@ public class ReportServiceImpl implements ReportService {
public ReportContentResponse getReport(Long reportId) {
Report report = reportFacade.createReport(reportId);

List<ReportCommentsResponse> comments = report.getComments().stream().map(comment ->
List<ReportCommentsResponse> comments = report.getComments()
.stream().map(comment ->
commentMapper.entityToResponse(comment, comment.getUser().equals(userFacade.createAuthUser())))
.collect(Collectors.toList());

List<MemberResponse> members = report.getMembers().stream()
.map(memberMapper::entityToResponse)
List<MemberResponse> members = report.getMembers()
.stream().map(memberMapper::entityToResponse)
.collect(Collectors.toList());

if(!isAccessable(report)) {
throw new InvalidAccessException();
}

return reportMapper.entityToContentResponse(report, isMine(report), comments, members);
return reportMapper.entityToContentResponse(report, userFacade.isMine(report), comments, members);
}

@Override
Expand All @@ -66,18 +67,13 @@ public ReportListResponse getReportList(Pageable pageable, Type type, Field fiel
@Override
public Long deleteReport(Long reportId) {
Report report = reportFacade.createReport(reportId);
if(!isMine(report)) throw new InvalidAccessException();
if(!userFacade.isMine(report)) throw new InvalidAccessException();
reportRepository.delete(report);
return reportId;
}

private boolean isAccessable(Report report) {
return report.getReportType().getAccess().equals(Access.EVERY) || isMine(report);
}

private boolean isMine(Report report) {
return report.getMembers().stream()
.anyMatch(member -> member.getUser().equals(userFacade.createAuthUser()));
return report.getReportType().getAccess().equals(Access.EVERY) || userFacade.isMine(report);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.dsmpear.main.user_backend_v2.entity.member.Member;
import com.dsmpear.main.user_backend_v2.entity.report.Report;
import com.dsmpear.main.user_backend_v2.entity.report.ReportRepository;
import com.dsmpear.main.user_backend_v2.entity.report.repository.ReportRepository;
import com.dsmpear.main.user_backend_v2.exception.InvalidAccessException;
import com.dsmpear.main.user_backend_v2.facade.report.ReportFacade;
import com.dsmpear.main.user_backend_v2.facade.user.UserFacade;
Expand Down Expand Up @@ -84,7 +84,7 @@ private <R extends BaseReportRequest> Report saveReport(R request) {
private <R extends BaseReportRequest>Long updateReportContent(R request, Long reportId) {
Report report = reportFacade.createReport(reportId);

if(!isMine(report)) throw new InvalidAccessException();
if(!userFacade.isMine(report)) throw new InvalidAccessException();

report.update(request);

Expand Down Expand Up @@ -120,10 +120,4 @@ private Member buildMember(String email, Report report) {
.build();
}

private boolean isMine(Report report) {
return report.getMembers().stream()
.map(Member::getUser)
.anyMatch(member -> member.equals(userFacade.createAuthUser()));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dsmpear.main.user_backend_v2.service.search;

import com.dsmpear.main.user_backend_v2.entity.report.Report;
import com.dsmpear.main.user_backend_v2.entity.report.ReportRepository;
import com.dsmpear.main.user_backend_v2.entity.report.repository.ReportRepository;
import com.dsmpear.main.user_backend_v2.entity.report.enums.Access;
import com.dsmpear.main.user_backend_v2.entity.user.User;
import com.dsmpear.main.user_backend_v2.entity.user.UserRepository;
Expand Down

0 comments on commit 0371f7c

Please sign in to comment.