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

refactor: API 주소 변경 #67

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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 @@ -47,6 +47,7 @@ public TrashPhotoDTO.TrashPhotoPageResponse findPage(Long userId, Pageable pagea
.applyPagination(pageable, jpaQuery)
.fetch();

return new TrashPhotoDTO.TrashPhotoPageResponse(paginationUtils.getTotalPageCount(jpaQuery.fetchCount(), pageable.getPageSize()), trashPhotoResponses);
long totalCount = jpaQuery.fetchCount();
return new TrashPhotoDTO.TrashPhotoPageResponse(totalCount, paginationUtils.getTotalPageCount(totalCount, pageable.getPageSize()), trashPhotoResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.ddd.moodof.adapter.presentation.api.StoragePhotoAPI;
import com.ddd.moodof.application.StoragePhotoService;
import com.ddd.moodof.application.TrashPhotoService;
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import com.ddd.moodof.application.dto.TrashPhotoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -18,6 +20,7 @@ public class StoragePhotoController implements StoragePhotoAPI {
public static final String API_STORAGE_PHOTO = "/api/storage-photos";

private final StoragePhotoService storagePhotoService;
private final TrashPhotoService trashPhotoService;

@Override
@PostMapping
Expand Down Expand Up @@ -57,8 +60,8 @@ public ResponseEntity<StoragePhotoDTO.StoragePhotoDetailResponse> findDetail(

@Override
@DeleteMapping
public ResponseEntity<Void> delete(@LoginUserId Long userId, @RequestBody StoragePhotoDTO.DeleteStoragePhotos request) {
storagePhotoService.delete(userId, request);
return ResponseEntity.noContent().build();
public ResponseEntity<List<TrashPhotoDTO.TrashPhotoCreatedResponse>> addToTrash(@LoginUserId Long userId, @RequestBody TrashPhotoDTO.CreateTrashPhotos request) {
List<TrashPhotoDTO.TrashPhotoCreatedResponse> response = trashPhotoService.add(userId, request);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ddd.moodof.adapter.presentation.api.TrashPhotoAPI;
import com.ddd.moodof.application.TrashPhotoService;
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import com.ddd.moodof.application.dto.TrashPhotoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -24,6 +25,12 @@ public ResponseEntity<List<TrashPhotoDTO.TrashPhotoCreatedResponse>> add(@LoginU
return ResponseEntity.ok(responses);
}

@PostMapping("/restore")
public ResponseEntity<List<StoragePhotoDTO.StoragePhotoResponse>> restore(@LoginUserId Long userId, @RequestBody TrashPhotoDTO.TrashPhotosRequest request) {
List<StoragePhotoDTO.StoragePhotoResponse> responses = trashPhotoService.restore(request, userId);
return ResponseEntity.ok(responses);
}

@Override
@GetMapping
public ResponseEntity<TrashPhotoDTO.TrashPhotoPageResponse> findPage(
Expand All @@ -38,8 +45,8 @@ public ResponseEntity<TrashPhotoDTO.TrashPhotoPageResponse> findPage(

@Override
@DeleteMapping
public ResponseEntity<Void> cancel(@LoginUserId Long userId, @RequestBody TrashPhotoDTO.CancelTrashPhotos request) {
trashPhotoService.cancel(request, userId);
public ResponseEntity<Void> delete(@LoginUserId Long userId, @RequestBody TrashPhotoDTO.TrashPhotosRequest request) {
trashPhotoService.deletePhoto(request, userId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ddd.moodof.adapter.presentation.LoginUserId;
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import com.ddd.moodof.application.dto.TrashPhotoDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -32,5 +33,5 @@ ResponseEntity<StoragePhotoDTO.StoragePhotoPageResponse> findPage(

@ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token")
@DeleteMapping
ResponseEntity<Void> delete(@ApiIgnore @LoginUserId Long userId, @RequestBody StoragePhotoDTO.DeleteStoragePhotos request);
ResponseEntity<List<TrashPhotoDTO.TrashPhotoCreatedResponse>> addToTrash(@ApiIgnore @LoginUserId Long userId, @RequestBody TrashPhotoDTO.CreateTrashPhotos request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ ResponseEntity<TrashPhotoDTO.TrashPhotoPageResponse> findPage(

@ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token")
@DeleteMapping
ResponseEntity<Void> cancel(@ApiIgnore @LoginUserId Long userId, @RequestBody TrashPhotoDTO.CancelTrashPhotos request);
ResponseEntity<Void> delete(@ApiIgnore @LoginUserId Long userId, @RequestBody TrashPhotoDTO.TrashPhotosRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public void delete(Long userId, StoragePhotoDTO.DeleteStoragePhotos request) {
public StoragePhotoDTO.StoragePhotoDetailResponse findDetail(Long userId, Long id, List<Long> tagIds) {
return storagePhotoQueryRepository.findDetail(userId, id, tagIds);
}

public List<StoragePhotoDTO.StoragePhotoResponse> findAllById(List<Long> storagePhotoIds) {
List<StoragePhoto> storagePhotos = storagePhotoRepository.findAllById(storagePhotoIds);
return StoragePhotoDTO.StoragePhotoResponse.listFrom(storagePhotos);
}
}
29 changes: 26 additions & 3 deletions src/main/java/com/ddd/moodof/application/TrashPhotoService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ddd.moodof.application;

import com.ddd.moodof.adapter.infrastructure.persistence.PaginationUtils;
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import com.ddd.moodof.application.dto.TrashPhotoDTO;
import com.ddd.moodof.domain.model.trash.photo.TrashPhoto;
import com.ddd.moodof.domain.model.trash.photo.TrashPhotoQueryRepository;
Expand All @@ -11,6 +12,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -39,10 +41,31 @@ public TrashPhotoDTO.TrashPhotoPageResponse findPage(Long userId, int page, int
}

@Transactional
public void cancel(TrashPhotoDTO.CancelTrashPhotos request, Long userId) {
if (!trashPhotoRepository.existsByIdInAndUserId(request.getTrashPhotoIds(), userId)) {
throw new IllegalArgumentException("요청과 일치하는 휴지통 사진이 없습니다.");
public void deletePhoto(TrashPhotoDTO.TrashPhotosRequest request, Long userId) {
List<TrashPhoto> trashPhotos = trashPhotoRepository.findAllById(request.getTrashPhotoIds());

List<Long> storagePhotoIds = trashPhotos.stream()
.map(TrashPhoto::getStoragePhotoId)
.collect(Collectors.toList());

storagePhotoService.delete(userId, new StoragePhotoDTO.DeleteStoragePhotos(storagePhotoIds));
trashPhotoRepository.deleteAllByIdIn(request.getTrashPhotoIds());
}
Comment on lines +44 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요청과 일치하는 휴지통 사진이 없습니다.로 체크하는 로직이 사라진 이유가 혹시 있을까요? 만약 request로 들어온 List<Long> trashPhotoIds id리스트가 없는 경우요!


@Transactional
public List<StoragePhotoDTO.StoragePhotoResponse> restore(TrashPhotoDTO.TrashPhotosRequest request, Long userId) {
List<TrashPhoto> trashPhotos = trashPhotoRepository.findAllById(request.getTrashPhotoIds());

if (trashPhotos.stream().anyMatch(trashPhoto -> trashPhoto.isUserNotEqual(userId))) {
throw new IllegalArgumentException("휴지통 사진의 userId가 요청과 다릅니다.");
}

trashPhotoRepository.deleteAllByIdIn(request.getTrashPhotoIds());

List<Long> storagePhotoIds = trashPhotos.stream()
.map(TrashPhoto::getStoragePhotoId)
.collect(Collectors.toList());

return storagePhotoService.findAllById(storagePhotoIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static List<StoragePhotoResponse> listFrom(List<StoragePhoto> storagePhot
public static class StoragePhotoPageResponse {
private long totalStoragePhotoCount;
private long totalPageCount;
private List<StoragePhotoResponse> storagePhotos;
private List<StoragePhotoResponse> data;
}

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ public static TrashPhotoCreatedResponse of(TrashPhoto trashPhoto) {
@Getter
@AllArgsConstructor
public static class TrashPhotoPageResponse {
private long totalTrashPhotoCount;
private long totalPageCount;
private List<TrashPhotoResponse> responses;
private List<TrashPhotoResponse> data;
}

@NoArgsConstructor
@Getter
@AllArgsConstructor
public static class CancelTrashPhotos {
public static class TrashPhotosRequest {
private List<Long> trashPhotoIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public class TrashPhoto {

@LastModifiedDate
private LocalDateTime lastModifiedDate;

public boolean isUserNotEqual(Long userId) {
return !this.userId.equals(userId);
}
}
5 changes: 3 additions & 2 deletions src/test/java/com/ddd/moodof/acceptance/AcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand Down Expand Up @@ -258,7 +259,7 @@ protected void deleteWithLogin(String uri, Long resourceId, Long userId) {
}
}

protected <T> void deleteListWithLogin(String uri, T request, Long userId) {
protected <T> void deleteListWithLogin(String uri, T request, Long userId, ResultMatcher expectResult) {
try {
String token = tokenProvider.createToken(userId);

Expand All @@ -267,7 +268,7 @@ protected <T> void deleteListWithLogin(String uri, T request, Long userId) {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
.header(AUTHORIZATION, BEARER + token))
.andExpect(MockMvcResultMatchers.status().isNoContent())
.andExpect(expectResult)
.andReturn();

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,6 +68,6 @@ void setUp() {
List<Long> boardPhotoIds = responses.stream()
.map(BoardPhotoDTO.BoardPhotoResponse::getId)
.collect(Collectors.toList());
deleteListWithLogin(API_BOARD_PHOTO, new BoardPhotoDTO.RemoveBoardPhotos(boardPhotoIds), userId);
deleteListWithLogin(API_BOARD_PHOTO, new BoardPhotoDTO.RemoveBoardPhotos(boardPhotoIds), userId, MockMvcResultMatchers.status().isNoContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ddd.moodof.application.dto.TagDTO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Collections;
Expand Down Expand Up @@ -84,9 +85,9 @@ public class StoragePhotoAcceptanceTest extends AcceptanceTest {

// then
assertAll(
() -> assertThat(response.getStoragePhotos().size()).isEqualTo(3),
() -> assertThat(response.getStoragePhotos().get(0)).usingRecursiveComparison().isEqualTo(top),
() -> assertThat(response.getStoragePhotos().get(1)).usingRecursiveComparison().isEqualTo(second),
() -> assertThat(response.getData().size()).isEqualTo(3),
() -> assertThat(response.getData().get(0)).usingRecursiveComparison().isEqualTo(top),
() -> assertThat(response.getData().get(1)).usingRecursiveComparison().isEqualTo(second),
() -> assertThat(response.getTotalPageCount()).isEqualTo(2),
() -> assertThat(response.getTotalStoragePhotoCount()).isEqualTo(4)
);
Expand Down Expand Up @@ -121,9 +122,9 @@ public class StoragePhotoAcceptanceTest extends AcceptanceTest {

// then
assertAll(
() -> assertThat(response.getStoragePhotos().size()).isEqualTo(2),
() -> assertThat(response.getStoragePhotos().get(0)).usingRecursiveComparison().isEqualTo(top),
() -> assertThat(response.getStoragePhotos().get(1)).usingRecursiveComparison().isEqualTo(second),
() -> assertThat(response.getData().size()).isEqualTo(2),
() -> assertThat(response.getData().get(0)).usingRecursiveComparison().isEqualTo(top),
() -> assertThat(response.getData().get(1)).usingRecursiveComparison().isEqualTo(second),
() -> assertThat(response.getTotalPageCount()).isEqualTo(2),
() -> assertThat(response.getTotalStoragePhotoCount()).isEqualTo(4)
);
Expand All @@ -135,7 +136,7 @@ public class StoragePhotoAcceptanceTest extends AcceptanceTest {
StoragePhotoDTO.StoragePhotoResponse storagePhotoResponse = 보관함사진_생성(userId, "uri", "color");

// when then
deleteListWithLogin(API_STORAGE_PHOTO, new StoragePhotoDTO.DeleteStoragePhotos(List.of(storagePhotoResponse.getId())), userId);
deleteListWithLogin(API_STORAGE_PHOTO, new StoragePhotoDTO.DeleteStoragePhotos(List.of(storagePhotoResponse.getId())), userId, MockMvcResultMatchers.status().isOk());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ddd.moodof.application.dto.StoragePhotoDTO;
import com.ddd.moodof.application.dto.TrashPhotoDTO;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
Expand Down Expand Up @@ -58,14 +59,15 @@ public class TrashPhotoAcceptanceTest extends AcceptanceTest {

// then
assertAll(
() -> assertThat(response.getTotalTrashPhotoCount()).isEqualTo(3),
() -> assertThat(response.getTotalPageCount()).isEqualTo(2),
() -> assertThat(response.getResponses().get(0).getStoragePhoto()).usingRecursiveComparison().isEqualTo(storagePhoto3),
() -> assertThat(response.getResponses().get(0).getId()).isEqualTo(top.get(0).getId())
() -> assertThat(response.getData().get(0).getStoragePhoto()).usingRecursiveComparison().isEqualTo(storagePhoto3),
() -> assertThat(response.getData().get(0).getId()).isEqualTo(top.get(0).getId())
);
}

@Test
void 휴지통의_사진을_복구한다() {
void 휴지통_사진을_삭제한다() {
// given
StoragePhotoDTO.StoragePhotoResponse storagePhoto = 보관함사진_생성(userId, "uri", "representativeColor");
List<TrashPhotoDTO.TrashPhotoCreatedResponse> responses = 보관함사진_휴지통_이동(List.of(storagePhoto.getId()), userId);
Expand All @@ -75,6 +77,23 @@ public class TrashPhotoAcceptanceTest extends AcceptanceTest {
.collect(Collectors.toList());

// when then
deleteListWithLogin(API_TRASH_PHOTO, new TrashPhotoDTO.CancelTrashPhotos(trashPhotoIds), userId);
deleteListWithLogin(API_TRASH_PHOTO, new TrashPhotoDTO.TrashPhotosRequest(trashPhotoIds), userId, MockMvcResultMatchers.status().isNoContent());
}

@Test
void 휴지통_사진을_복구한다() {
// given
StoragePhotoDTO.StoragePhotoResponse storagePhoto = 보관함사진_생성(userId, "uri", "representativeColor");
List<TrashPhotoDTO.TrashPhotoCreatedResponse> trashPhotos = 보관함사진_휴지통_이동(List.of(storagePhoto.getId()), userId);

List<Long> trashPhotoIds = trashPhotos.stream()
.map(TrashPhotoDTO.TrashPhotoCreatedResponse::getId)
.collect(Collectors.toList());

// when
List<StoragePhotoDTO.StoragePhotoResponse> responses = postListWithLogin(new TrashPhotoDTO.TrashPhotosRequest(trashPhotoIds), API_TRASH_PHOTO + "/restore", StoragePhotoDTO.StoragePhotoResponse.class, userId);

// then
assertThat(responses.get(0)).usingRecursiveComparison().isEqualTo(storagePhoto);
}
}