Skip to content

Commit

Permalink
[ADD] reportResponse for modify
Browse files Browse the repository at this point in the history
  • Loading branch information
jhhong0509 committed Jun 5, 2021
1 parent 0324611 commit df072e6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dsmpear.main.user_backend_v2.controller;

import com.dsmpear.main.user_backend_v2.payload.response.ReportContentResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportModifyResponse;
import com.dsmpear.main.user_backend_v2.service.report.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand All @@ -17,6 +18,11 @@ public ReportContentResponse getReportContent(@PathVariable Long reportId) {
return reportService.getReport(reportId);
}

@GetMapping("/modify/{reportId}")
public ReportModifyResponse getReportModify(@PathVariable Long reportId) {
return reportService.getReportModify(reportId);
}

@DeleteMapping("/{reportId}")
public Long deleteReport(@PathVariable Long reportId) {
return reportService.deleteReport(reportId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.dsmpear.main.user_backend_v2.entity.report.Report;
import com.dsmpear.main.user_backend_v2.entity.user.User;
import com.dsmpear.main.user_backend_v2.payload.request.report.BaseReportRequest;
import com.dsmpear.main.user_backend_v2.payload.response.MemberResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportCommentsResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportContentResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportResponse;
import com.dsmpear.main.user_backend_v2.payload.response.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand Down Expand Up @@ -42,4 +39,14 @@ public interface ReportMapper {
ReportContentResponse entityToContentResponse(Report report, Boolean isMine,
List<ReportCommentsResponse> comments, List<MemberResponse> member);

@Mapping(source = "report.languages", target = "languages")
@Mapping(source = "report.reportType.type", target = "type")
@Mapping(source = "report.reportType.field", target = "field")
@Mapping(source = "report.reportType.access", target = "access")
@Mapping(source = "report.reportType.grade", target = "grade")
@Mapping(source = "report.reportFile.id", target = "fileId")
@Mapping(source = "member", target = "member")
@Mapping(source = "report.createdAt", target = "createdAt")
ReportModifyResponse entityToModifyResponse(Report report, Boolean isMine, List<MemberResponse> member);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dsmpear.main.user_backend_v2.payload.response;

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;
import com.dsmpear.main.user_backend_v2.entity.report.enums.Type;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReportModifyResponse {

private String title;

private String description;

private LocalDateTime createdAt;

private List<String> languages;

private Type type;

private Grade grade;

private Access access;

private Field field;

private String teamName;

private List<MemberResponse> member;

private Long fileId;

private String github;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.dsmpear.main.user_backend_v2.entity.report.enums.Type;
import com.dsmpear.main.user_backend_v2.payload.response.ReportContentResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportListResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportModifyResponse;
import org.springframework.data.domain.Pageable;


public interface ReportService {
ReportContentResponse getReport(Long reportId);

ReportModifyResponse getReportModify(Long reportId);

ReportListResponse getReportList(Pageable pageable, Type type, Field field, Grade grade);

Long deleteReport(Long reportId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ public class ReportServiceImpl implements ReportService {
public ReportContentResponse getReport(Long reportId) {
Report report = reportFacade.createReport(reportId);

List<MemberResponse> members = getMembers(report);

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)
.collect(Collectors.toList());

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

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

@Override
public ReportModifyResponse getReportModify(Long reportId) {
Report report = reportFacade.createReport(reportId);
List<MemberResponse> members = getMembers(report);
validateAccessible(report);
return reportMapper.entityToModifyResponse(report, userFacade.isMine(report), members);
}

@Override
public ReportListResponse getReportList(Pageable pageable, Type type, Field field, Grade grade) {
Page<Report> reportResponses = reportCustomRepository.findAllByAccessAndGradeAndFieldAndType(grade, field, type, pageable);
Expand All @@ -71,8 +74,15 @@ public Long deleteReport(Long reportId) {
return reportId;
}

private boolean isAccessible(Report report) {
return report.getReportType().getAccess().equals(Access.EVERY) || userFacade.isMine(report);
private List<MemberResponse> getMembers(Report report) {
return report.getMembers()
.stream().map(memberMapper::entityToResponse)
.collect(Collectors.toList());
}

private void validateAccessible(Report report) {
if(!(report.getReportType().getAccess().equals(Access.EVERY) || userFacade.isMine(report)))
throw new InvalidAccessException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.dsmpear.main.user_backend_v2.payload.request.report.SoleReportRequest;
import com.dsmpear.main.user_backend_v2.payload.request.report.TeamReportRequest;
import com.dsmpear.main.user_backend_v2.payload.response.ReportContentResponse;
import com.dsmpear.main.user_backend_v2.payload.response.ReportModifyResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -31,6 +32,7 @@
import java.util.Arrays;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -156,6 +158,21 @@ void cleanUp() {
Assertions.assertTrue(response.getIsSubmitted());
}


@Test
@WithMockUser(value = "email", password = "pwd")
void 보고서_변경_보기_성공() throws Exception {
MvcResult result = mvc.perform(get("/report/modify/"+successReport.getId()))
.andExpect(status().isOk())
.andReturn();

ReportModifyResponse response = new ObjectMapper()
.registerModule(new JavaTimeModule())
.readValue(result.getResponse().getContentAsString(), ReportModifyResponse.class);

Assertions.assertEquals("title_for_every", response.getTitle());
}

@Test
void 보고서_보기_실패_유저_NOTFOUND() throws Exception {
mvc.perform(get("/report/"+1))
Expand Down

0 comments on commit df072e6

Please sign in to comment.