From 17b8d02ca221a8c71a1f395b7e22a3f0fd834f26 Mon Sep 17 00:00:00 2001 From: daolove0323 Date: Sat, 9 Nov 2024 00:30:06 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20=EC=B5=9C=EB=8C=80=EC=82=AC?= =?UTF-8?q?=EC=9D=B4=EC=A6=88=20=EC=A0=9C=ED=95=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wouldyouin/Image/application/ImageStorage.java | 13 +++++++------ .../Image/application/MemberImageService.java | 5 +---- .../_common/config/ObjectMapperConfig.java | 14 ++++++++++++++ src/main/resources/application.yml | 5 +++++ .../curation/CurationControllerUnitTest.java | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java index 046aea29..569c92a2 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java @@ -4,10 +4,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.UUID; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageRequest; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToDeleteImageException; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToUploadImageException; import org.springframework.beans.factory.annotation.Value; @@ -36,19 +36,19 @@ public String save(MultipartFile image, String subPath) { } // Todo: payment랑 이부분 restclient 유틸로 빼기 - public String save(String imageUrl, String subPath) { + public ImageRequest save(String imageUrl, String subPath) { RestClient client = RestClient.builder().build(); String fileName = ""; + Long size = 0L; try { ResponseEntity response = client.get() .uri(imageUrl) .retrieve() .toEntity(byte[].class); - if (Optional.ofNullable(response).isPresent() && response.getStatusCode().is2xxSuccessful()) { - byte[] imageBytes = response.getBody(); - + if (Optional.ofNullable(response.getBody()).isPresent() && response.getStatusCode().is2xxSuccessful()) { fileName = generateUuidName() + "." + getExtension(imageUrl); + size = (long) response.getBody().length; Path path = Paths.get(commonPath, subPath, fileName); Files.createDirectories(path.getParent()); Files.write(path, response.getBody()); @@ -56,7 +56,8 @@ public String save(String imageUrl, String subPath) { } catch (IOException ex) { throw new FailedToUploadImageException(); } - return subPath + "/" + fileName; + String url = subPath + "/" + fileName; + return ImageRequest.of(url, size, getExtension(imageUrl)); } public void delete(String imagePath) { diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java index 621679b3..ba009567 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java @@ -60,10 +60,7 @@ public void setBaseMember(MemberImage image, BaseMember member) { image.setBaseMember(member); } - // TODO: imageUrl을 MemberImage로 변환하는 로직 추가 필요 - // Todo: extension과 size 불러오기 public MemberImage convert(String imageUrl) { - var request = ImageRequest.of(imageStorage.save(imageUrl, subPath), 123123L, "jpg"); - return memberImageRepository.save(toEntity(request)); + return memberImageRepository.save(toEntity(imageStorage.save(imageUrl, subPath))); } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java new file mode 100644 index 00000000..861847a4 --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java @@ -0,0 +1,14 @@ +package org.ktc2.cokaen.wouldyouin._common.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ObjectMapperConfig { + + @Bean + ObjectMapper objectMapper() { + return new ObjectMapper(); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e76ba79b..446aa5a9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,11 @@ spring: application.name: WouldYouIn + servlet: + multipart: + max-file-size: 10MB + max-request-size: 10MB + # 임시 프로젝트 url wouldyouin-domain-name: http://52.78.71.136/ profiles: diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index d6fcb308..d7055555 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -48,6 +48,7 @@ @WebMvcTest(CurationController.class) class CurationControllerUnitTest { + private static ObjectMapper objectMapper; @MockBean @@ -66,7 +67,6 @@ class CurationControllerUnitTest { @BeforeAll public static void init() { - objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); } From a2879486df3e08662e2331c96afc67ed7128cf67 Mon Sep 17 00:00:00 2001 From: daolove0323 Date: Sat, 9 Nov 2024 00:38:28 +0900 Subject: [PATCH 2/6] feat: objectMapper autowired --- .../curation/CurationControllerUnitTest.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index d7055555..7ee0bf05 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.util.List; import java.util.Random; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -31,7 +30,6 @@ import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.ktc2.cokaen.wouldyouin.curation.api.CurationController; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest; import org.ktc2.cokaen.wouldyouin.curation.application.CurationService; @@ -48,8 +46,8 @@ @WebMvcTest(CurationController.class) class CurationControllerUnitTest { - - private static ObjectMapper objectMapper; + @Autowired + private ObjectMapper objectMapper; @MockBean private CurationService curationService; @@ -64,14 +62,9 @@ class CurationControllerUnitTest { private WebApplicationContext context; private static final long randomId = abs(new Random().nextLong()); - - @BeforeAll - public static void init() { - objectMapper.registerModule(new JavaTimeModule()); - } - @BeforeEach public void setup() throws Exception { + objectMapper.registerModule(new JavaTimeModule()); mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) From 03863bc7be71d9f94827cbc63a47bbe1cfa9b13a Mon Sep 17 00:00:00 2001 From: ariimo Date: Sat, 9 Nov 2024 00:57:29 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[Weekly/11/Test/like]=20Like=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=84=B0=EB=A7=81=20=EB=B0=8F=20Event=20Unit=20Test?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20(#97)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: Event Unit Test 리팩터링 * feat: Like 리팩터링 --- .../wouldyouin/like/api/LikeController.java | 37 +++++++------- .../like/application/LikeService.java | 51 +++++++++---------- .../application/{ => dto}/LikeResponse.java | 2 +- .../application/dto/LikeToggleResponse.java | 20 ++++++++ .../like/persist/LikeRepository.java | 14 ++++- .../event/EventControllerUnitTest.java | 12 +++-- .../event/EventServiceUnitTest.java | 14 ++--- .../like/LikeControllerUnitTest.java | 6 +++ 8 files changed, 98 insertions(+), 58 deletions(-) rename src/main/java/org/ktc2/cokaen/wouldyouin/like/application/{ => dto}/LikeResponse.java (93%) create mode 100644 src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeToggleResponse.java create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/like/LikeControllerUnitTest.java diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java index 02a434bc..75792e55 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java @@ -1,17 +1,18 @@ package org.ktc2.cokaen.wouldyouin.like.api; -import java.util.List; import lombok.RequiredArgsConstructor; import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse; import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody; +import org.ktc2.cokaen.wouldyouin._common.config.ParamDefaults; import org.ktc2.cokaen.wouldyouin.auth.Authorize; import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier; -import org.ktc2.cokaen.wouldyouin.like.application.LikeResponse; import org.ktc2.cokaen.wouldyouin.like.application.LikeServiceFactory; +import org.ktc2.cokaen.wouldyouin.like.application.dto.LikeResponse; +import org.ktc2.cokaen.wouldyouin.like.application.dto.LikeToggleResponse; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; -import org.springframework.http.HttpStatus; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -26,22 +27,24 @@ public class LikeController { private final LikeServiceFactory likeServiceFactory; - // Todo: 와일드카드 수정 - // Todo: api 요청시 create, delete를 분리하지말고 토글방식으로 하면 어떨지 - @GetMapping - public ResponseEntity>> getLikes(@Authorize(MemberType.normal) MemberIdentifier identifier, @RequestParam("type") MemberType memberType) { - return ApiResponse.ok(likeServiceFactory.getLikeServiceFrom(memberType).getLikes(identifier.id())); + public ResponseEntity>> getLikes( + @Authorize(MemberType.normal) MemberIdentifier identifier, + @RequestParam("type") MemberType memberType, + @RequestParam(defaultValue = ParamDefaults.PAGE) Integer page, + @RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size, + @RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId + ) { + return ApiResponse.ok( + likeServiceFactory.getLikeServiceFrom(memberType) + .getLikes(identifier.id(), PageRequest.of(page, size), lastId)); } @PostMapping("/{targetMemberId}") - public ResponseEntity> createLike(@Authorize(MemberType.normal) MemberIdentifier identifier, @PathVariable("targetMemberId") Long targetId) { - return ApiResponse.created(likeServiceFactory.getLikeServiceFrom(targetId).create(identifier.id(), targetId)); - } - - @DeleteMapping("/{targetMemberId}") - public ResponseEntity> deleteLike(@Authorize(MemberType.normal) MemberIdentifier identifier, @PathVariable("targetMemberId") Long targetId) { - likeServiceFactory.getLikeServiceFrom(targetId).delete(identifier.id(), targetId); - return ApiResponse.noContent(); + public ResponseEntity> createOrDeleteLike( + @Authorize(MemberType.normal) MemberIdentifier identifier, + @PathVariable("targetMemberId") Long targetId) { + return ApiResponse.created( + likeServiceFactory.getLikeServiceFrom(targetId).toggleLike(identifier.id(), targetId)); } } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java index 01e2b32d..7b2393ab 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java @@ -1,7 +1,8 @@ package org.ktc2.cokaen.wouldyouin.like.application; -import java.util.List; import lombok.RequiredArgsConstructor; +import org.ktc2.cokaen.wouldyouin.like.application.dto.LikeResponse; +import org.ktc2.cokaen.wouldyouin.like.application.dto.LikeToggleResponse; import org.ktc2.cokaen.wouldyouin.like.persist.Like; import org.ktc2.cokaen.wouldyouin.like.persist.LikeRepository; import org.ktc2.cokaen.wouldyouin.member.application.LikeableMemberGetterFactory; @@ -9,6 +10,8 @@ import org.ktc2.cokaen.wouldyouin.member.persist.LikeableMember; import org.ktc2.cokaen.wouldyouin.member.persist.Member; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,44 +23,38 @@ public abstract class LikeService getLikeRepository(); + protected abstract LikeType toEntity(Member member, LikeableMember targetLikableMember); + public abstract MemberType getTargetLikeableMemberType(); @Transactional(readOnly = true) - public List getLikes(Long memberId) { - return getLikeRepository().findAllByMember(memberService.getByIdOrThrow(memberId)) - .stream() - .map(Like::getLikeableMember) - .map(LikeResponse::from) - .toList(); + public Slice getLikes(Long memberId, Pageable pageable, Long lastId) { + return getLikeRepository().findAllByMember( + memberService.getByIdOrThrow(memberId), lastId, pageable) + .map(like -> LikeResponse.from(like.getLikeableMember())); } @Transactional - public LikeResponse create(Long memberId, Long targetMemberId) { + public LikeToggleResponse toggleLike(Long memberId, Long targetMemberId) { Member member = memberService.getByIdOrThrow(memberId); LikeableMember targetLikeableMember = getLikeableMemberByIdOrThrow(targetMemberId); - getLikeRepository().findByMemberAndLikeableMember(member, targetLikeableMember) - .ifPresent(x -> { throw new RuntimeException("이미 좋아요한 사용자입니다."); }); - - targetLikeableMember.increaseLikes(); - return LikeResponse.from(getLikeRepository() - .save(toEntity(member, targetLikeableMember)) - .getLikeableMember()); - } - - @Transactional - public void delete(Long memberId, Long targetMemberId) { - Member member = memberService.getByIdOrThrow(memberId); - LikeableMember targetLikeableMember = getLikeableMemberByIdOrThrow(targetMemberId); - LikeType like = getLikeRepository().findByMemberAndLikeableMember(member, targetLikeableMember) - .orElseThrow(() -> new RuntimeException("해당 사용자를 좋아요하지 않았습니다.")); - - targetLikeableMember.decreaseLikes(); - getLikeRepository().delete(like); + return getLikeRepository().findByMemberAndLikeableMember(member, targetLikeableMember) + .map(like -> { + targetLikeableMember.decreaseLikes(); + getLikeRepository().delete(like); + return LikeToggleResponse.from(false); + }) + .orElseGet(() -> { + targetLikeableMember.increaseLikes(); + getLikeRepository().save(toEntity(member, targetLikeableMember)); + return LikeToggleResponse.from(true); + }); } @Transactional(readOnly = true) protected LikeableMember getLikeableMemberByIdOrThrow(Long likeableMemberId) { - return likeableMemberGetterFactory.get(getTargetLikeableMemberType()).getByIdOrThrow(likeableMemberId); + return likeableMemberGetterFactory.get(getTargetLikeableMemberType()) + .getByIdOrThrow(likeableMemberId); } } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeResponse.java similarity index 93% rename from src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeResponse.java rename to src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeResponse.java index c4150bb6..cc749208 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeResponse.java @@ -1,4 +1,4 @@ -package org.ktc2.cokaen.wouldyouin.like.application; +package org.ktc2.cokaen.wouldyouin.like.application.dto; import java.util.List; import lombok.AccessLevel; diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeToggleResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeToggleResponse.java new file mode 100644 index 00000000..d5181d72 --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/dto/LikeToggleResponse.java @@ -0,0 +1,20 @@ +package org.ktc2.cokaen.wouldyouin.like.application.dto; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.ktc2.cokaen.wouldyouin.member.persist.LikeableMember; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PROTECTED) +@Builder +public class LikeToggleResponse { + private final boolean isLiked; + + public static LikeToggleResponse from(boolean state) { + return LikeToggleResponse.builder() + .isLiked(state) + .build(); + } +} diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/persist/LikeRepository.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/persist/LikeRepository.java index 3bb61ce8..97936c97 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/persist/LikeRepository.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/persist/LikeRepository.java @@ -1,15 +1,25 @@ package org.ktc2.cokaen.wouldyouin.like.persist; + import java.util.List; import java.util.Optional; import org.ktc2.cokaen.wouldyouin.member.persist.LikeableMember; import org.ktc2.cokaen.wouldyouin.member.persist.Member; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.NoRepositoryBean; @NoRepositoryBean public interface LikeRepository > extends JpaRepository { - Optional findByMemberAndLikeableMember(Member member, LikeableMember likeableMember); - List findAllByMember(Member member); + + @Query("SELECT l FROM #{#entityName} l " + + "JOIN FETCH l.member m " + + "JOIN FETCH l.likeableMember lm " + + "WHERE l.member.id = :member " + + "AND l.id < :lastId " + + "ORDER BY l.id DESC") + Slice findAllByMember(Member member, Long lastId, Pageable pageable); } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java index e9267358..5d14ba40 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java @@ -91,16 +91,18 @@ void getEventsByFilterOrderByDistanceAsc() throws Exception { // given LocationFilter locationFilter = new LocationFilter(0.0, 0.0, 10.0, 10.0); Location currentLocation = new Location(3.0, 2.0); + String title = "testTitle"; Category category = Category.공예; Area area = Area.광주; - int pageNumber = 1; // 교수님 - int pageSize = 10; // + int pageNumber = 1; + int pageSize = 10; Long lastId = 1L; Pageable pageable = PageRequest.of(pageNumber, pageSize); given(eventService.getAllByFilterOrderByDistanceAsc( locationFilter, currentLocation, + title, category, area, pageable, @@ -115,8 +117,9 @@ void getEventsByFilterOrderByDistanceAsc() throws Exception { .param("endLongitude", locationFilter.getEndLongitude().toString()) .param("latitude", currentLocation.getLatitude().toString()) .param("longitude", currentLocation.getLongitude().toString()) - .param("category", category.toString()) // enum을 문자열로 변환하여 설정 - .param("area", area.toString()) // enum을 문자열로 변환하여 설정 + .param("title", title) + .param("category", category.toString()) + .param("area", area.toString()) .param("page", String.valueOf(pageNumber)) .param("size", String.valueOf(pageSize)) .param("lastId", String.valueOf(lastId)) @@ -127,6 +130,7 @@ void getEventsByFilterOrderByDistanceAsc() throws Exception { then(eventService).should(times(1)).getAllByFilterOrderByDistanceAsc( any(LocationFilter.class), any(Location.class), + eq(title), eq(category), eq(area), eq(pageable), diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java index a5e5eaa7..7a9cea4d 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java @@ -19,15 +19,14 @@ import org.ktc2.cokaen.wouldyouin._common.vo.Area; import org.ktc2.cokaen.wouldyouin._common.vo.Category; import org.ktc2.cokaen.wouldyouin._common.vo.Location; +import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; import org.ktc2.cokaen.wouldyouin.curation.api.dto.LocationFilter; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventCreateRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventEditRequest; import org.ktc2.cokaen.wouldyouin.event.application.EventService; import org.ktc2.cokaen.wouldyouin.event.persist.Event; import org.ktc2.cokaen.wouldyouin.event.persist.EventRepository; -import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; import org.ktc2.cokaen.wouldyouin.member.application.HostService; -import org.ktc2.cokaen.wouldyouin.member.application.MemberService; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.PageRequest; @@ -62,6 +61,7 @@ void getAllByFilterOrderByDistanceAsc() { // given LocationFilter location = new LocationFilter(); Location currentLocation = new Location(3.0, 2.0); + String title = "testTitle"; Category category = Category.공예; Area area = Area.광주; int pageNumber = 1; @@ -71,18 +71,18 @@ void getAllByFilterOrderByDistanceAsc() { given(eventRepository.findAllByFilterOrderByDistance(location.getStartLatitude(), location.getStartLongitude(), location.getEndLatitude(), location.getEndLongitude(), currentLocation.getLatitude(), - currentLocation.getLongitude(), - category, area, pageable)).willReturn(new SliceImpl<>(List.of())); + currentLocation.getLongitude(), title, category, area, pageable)).willReturn( + new SliceImpl<>(List.of())); // when - eventService.getAllByFilterOrderByDistanceAsc(location, currentLocation, category, area, - pageable, lastId); + eventService.getAllByFilterOrderByDistanceAsc(location, currentLocation, title, category, + area, pageable, lastId); // then then(eventRepository).should(times(1)) .findAllByFilterOrderByDistance(any(Double.class), any(Double.class), any(Double.class), any(Double.class), any(Double.class), any(Double.class), - any(Category.class), any(Area.class), any(Pageable.class)); + any(String.class), any(Category.class), any(Area.class), any(Pageable.class)); } @Test diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/like/LikeControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/like/LikeControllerUnitTest.java new file mode 100644 index 00000000..f3777b3b --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/like/LikeControllerUnitTest.java @@ -0,0 +1,6 @@ +package org.ktc2.cokaen.wouldyouin.like; + +public class LikeControllerUnitTest +{ + +} From 30abf7b831883938ae313c104fe0a84929247e93 Mon Sep 17 00:00:00 2001 From: daolove0323 Date: Sat, 9 Nov 2024 00:30:06 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20=EC=B5=9C=EB=8C=80=EC=82=AC?= =?UTF-8?q?=EC=9D=B4=EC=A6=88=20=EC=A0=9C=ED=95=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wouldyouin/Image/application/ImageStorage.java | 13 +++++++------ .../Image/application/MemberImageService.java | 5 +---- .../_common/config/ObjectMapperConfig.java | 14 ++++++++++++++ src/main/resources/application.yml | 5 +++++ .../curation/CurationControllerUnitTest.java | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java index 046aea29..569c92a2 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java @@ -4,10 +4,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.UUID; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageRequest; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToDeleteImageException; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToUploadImageException; import org.springframework.beans.factory.annotation.Value; @@ -36,19 +36,19 @@ public String save(MultipartFile image, String subPath) { } // Todo: payment랑 이부분 restclient 유틸로 빼기 - public String save(String imageUrl, String subPath) { + public ImageRequest save(String imageUrl, String subPath) { RestClient client = RestClient.builder().build(); String fileName = ""; + Long size = 0L; try { ResponseEntity response = client.get() .uri(imageUrl) .retrieve() .toEntity(byte[].class); - if (Optional.ofNullable(response).isPresent() && response.getStatusCode().is2xxSuccessful()) { - byte[] imageBytes = response.getBody(); - + if (Optional.ofNullable(response.getBody()).isPresent() && response.getStatusCode().is2xxSuccessful()) { fileName = generateUuidName() + "." + getExtension(imageUrl); + size = (long) response.getBody().length; Path path = Paths.get(commonPath, subPath, fileName); Files.createDirectories(path.getParent()); Files.write(path, response.getBody()); @@ -56,7 +56,8 @@ public String save(String imageUrl, String subPath) { } catch (IOException ex) { throw new FailedToUploadImageException(); } - return subPath + "/" + fileName; + String url = subPath + "/" + fileName; + return ImageRequest.of(url, size, getExtension(imageUrl)); } public void delete(String imagePath) { diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java index 621679b3..ba009567 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java @@ -60,10 +60,7 @@ public void setBaseMember(MemberImage image, BaseMember member) { image.setBaseMember(member); } - // TODO: imageUrl을 MemberImage로 변환하는 로직 추가 필요 - // Todo: extension과 size 불러오기 public MemberImage convert(String imageUrl) { - var request = ImageRequest.of(imageStorage.save(imageUrl, subPath), 123123L, "jpg"); - return memberImageRepository.save(toEntity(request)); + return memberImageRepository.save(toEntity(imageStorage.save(imageUrl, subPath))); } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java new file mode 100644 index 00000000..861847a4 --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java @@ -0,0 +1,14 @@ +package org.ktc2.cokaen.wouldyouin._common.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ObjectMapperConfig { + + @Bean + ObjectMapper objectMapper() { + return new ObjectMapper(); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e76ba79b..446aa5a9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,11 @@ spring: application.name: WouldYouIn + servlet: + multipart: + max-file-size: 10MB + max-request-size: 10MB + # 임시 프로젝트 url wouldyouin-domain-name: http://52.78.71.136/ profiles: diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index d6fcb308..d7055555 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -48,6 +48,7 @@ @WebMvcTest(CurationController.class) class CurationControllerUnitTest { + private static ObjectMapper objectMapper; @MockBean @@ -66,7 +67,6 @@ class CurationControllerUnitTest { @BeforeAll public static void init() { - objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); } From b2157448c27b7bf9c995d73538e4851b6fbc6430 Mon Sep 17 00:00:00 2001 From: daolove0323 Date: Sat, 9 Nov 2024 00:38:28 +0900 Subject: [PATCH 5/6] feat: objectMapper autowired --- .../curation/CurationControllerUnitTest.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index d7055555..7ee0bf05 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.util.List; import java.util.Random; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -31,7 +30,6 @@ import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.ktc2.cokaen.wouldyouin.curation.api.CurationController; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest; import org.ktc2.cokaen.wouldyouin.curation.application.CurationService; @@ -48,8 +46,8 @@ @WebMvcTest(CurationController.class) class CurationControllerUnitTest { - - private static ObjectMapper objectMapper; + @Autowired + private ObjectMapper objectMapper; @MockBean private CurationService curationService; @@ -64,14 +62,9 @@ class CurationControllerUnitTest { private WebApplicationContext context; private static final long randomId = abs(new Random().nextLong()); - - @BeforeAll - public static void init() { - objectMapper.registerModule(new JavaTimeModule()); - } - @BeforeEach public void setup() throws Exception { + objectMapper.registerModule(new JavaTimeModule()); mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) From f531c791fefe12f031d89e17f67d05adb57a286b Mon Sep 17 00:00:00 2001 From: daolove0323 Date: Sat, 9 Nov 2024 01:00:24 +0900 Subject: [PATCH 6/6] Update AdvertisementController.java --- .../advertisement/api/AdvertisementController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java index e8beda9e..89c66f07 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java @@ -45,7 +45,7 @@ public ResponseEntity> getAdvertisementBy public ResponseEntity> createAdvertisement( @Valid @RequestPart AdvertisementRequest advertisementRequest, @RequestPart(required = false) MultipartFile image, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { return ApiResponse.created(advertisementService.create(advertisementRequest, image)); } @@ -54,14 +54,14 @@ public ResponseEntity> updateAdvertisemen @PathVariable Long adId, @Valid @RequestPart AdvertisementRequest advertisementRequest, @RequestPart(required = false) MultipartFile image, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { return ApiResponse.ok(advertisementService.update(adId, advertisementRequest, image)); } @DeleteMapping("/{adId}") public ResponseEntity> deleteAdvertisement( @PathVariable Long adId, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { advertisementService.delete(adId); return ApiResponse.noContent(); }