From 6ebd887373799986a655bf076e77986daa2b8f01 Mon Sep 17 00:00:00 2001 From: LimHyunwoo <81962309+imenuuu@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:51:26 +0900 Subject: [PATCH 1/2] =?UTF-8?q?:zap:=20:=20=EA=B4=80=EB=A6=AC=EC=9E=90=20?= =?UTF-8?q?=EB=B0=B0=EB=84=88=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?(#225)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AdminBannerController.java | 18 +++++++++--- .../banner/service/AdminBannerService.java | 7 +++++ .../banner/converter/BannerConverter.java | 28 +++++++++++++++++++ .../matchapi/banner/dto/BannerReq.java | 2 ++ .../matchapi/banner/dto/BannerRes.java | 26 +++++++++++++++++ .../matchdomain/banner/entity/QBanner.java | 2 ++ .../banner/adaptor/BannerAdaptor.java | 9 ++++++ .../matchdomain/banner/entity/Banner.java | 2 ++ .../banner/repository/BannerRepository.java | 5 ++++ 9 files changed, 95 insertions(+), 4 deletions(-) diff --git a/Match-Api/src/main/java/com/example/matchapi/admin/banner/controller/AdminBannerController.java b/Match-Api/src/main/java/com/example/matchapi/admin/banner/controller/AdminBannerController.java index 0e29b02b..8f465490 100644 --- a/Match-Api/src/main/java/com/example/matchapi/admin/banner/controller/AdminBannerController.java +++ b/Match-Api/src/main/java/com/example/matchapi/admin/banner/controller/AdminBannerController.java @@ -4,6 +4,7 @@ import com.example.matchapi.banner.dto.BannerReq; import com.example.matchapi.banner.dto.BannerRes; import com.example.matchcommon.reponse.CommonResponse; +import com.example.matchcommon.reponse.PageResponse; import com.example.matchdomain.banner.enums.BannerType; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -29,23 +30,32 @@ public class AdminBannerController { @PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) @Operation(summary = "ADMIN-08-01 배너 업로드") public CommonResponse> uploadBanner( - //@RequestPart BannerType bannerType, + @RequestPart BannerType bannerType, @RequestPart MultipartFile bannerImage, @RequestPart BannerReq.BannerUpload bannerUploadDto ){ - System.out.println(bannerUploadDto.getContentsUrl()); return CommonResponse.onSuccess(adminBannerService.uploadBanner(BannerType.EVENT, bannerImage, bannerUploadDto)); } + @GetMapping("") + @Operation(summary = "ADMIN-08-02 배너 조회") + public CommonResponse>> getBannerLists( + @Parameter(description = "페이지", example = "0") @RequestParam(required = false, defaultValue = "0") int page, + @Parameter(description = "페이지 사이즈", example = "10") @RequestParam(required = false, defaultValue = "10") int size + + ){ + return CommonResponse.onSuccess(adminBannerService.getBannerLists(page, size)); + } + @DeleteMapping("/{bannerId}") - @Operation(summary = "ADMIN-08-02 배너 삭제") + @Operation(summary = "ADMIN-08-03 배너 삭제") public CommonResponse deleteBanner(@PathVariable Long bannerId){ adminBannerService.deleteBanner(bannerId); return CommonResponse.onSuccess("삭제 성공"); } @PatchMapping("/{bannerId}") - @Operation(summary = "ADMIN-03 배너 수정") + @Operation(summary = "ADMIN-08-04 배너 수정") public CommonResponse patchBanner( @PathVariable Long bannerId, BannerReq.BannerPatchDto bannerPatchDto){ diff --git a/Match-Api/src/main/java/com/example/matchapi/admin/banner/service/AdminBannerService.java b/Match-Api/src/main/java/com/example/matchapi/admin/banner/service/AdminBannerService.java index ebf7b46d..3410bbc8 100644 --- a/Match-Api/src/main/java/com/example/matchapi/admin/banner/service/AdminBannerService.java +++ b/Match-Api/src/main/java/com/example/matchapi/admin/banner/service/AdminBannerService.java @@ -3,6 +3,7 @@ import com.example.matchapi.banner.converter.BannerConverter; import com.example.matchapi.banner.dto.BannerReq; import com.example.matchapi.banner.dto.BannerRes; +import com.example.matchcommon.reponse.PageResponse; import com.example.matchdomain.banner.adaptor.BannerAdaptor; import com.example.matchdomain.banner.entity.Banner; import com.example.matchdomain.banner.enums.BannerType; @@ -11,6 +12,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -51,4 +53,9 @@ public void deleteBanner(Long bannerId) { public void patchBanner(Long bannerId) { } + + public PageResponse> getBannerLists(int page, int size) { + Page banners = bannerAdaptor.getBannerLists(page, size); + return new PageResponse<>(banners.isLast(), banners.getSize(), bannerConverter.convertToBannerLists(banners.getContent())); + } } diff --git a/Match-Api/src/main/java/com/example/matchapi/banner/converter/BannerConverter.java b/Match-Api/src/main/java/com/example/matchapi/banner/converter/BannerConverter.java index b3735aa6..519f236b 100644 --- a/Match-Api/src/main/java/com/example/matchapi/banner/converter/BannerConverter.java +++ b/Match-Api/src/main/java/com/example/matchapi/banner/converter/BannerConverter.java @@ -18,6 +18,7 @@ public Banner convertToBannerUpload(BannerType bannerType, String bannerImg, Ban .builder() .bannerImg(bannerImg) .bannerType(bannerType) + .name(bannerUploadDto.getName()) .eventId(bannerUploadDto.getEventId()) .build(); } @@ -26,6 +27,7 @@ public Banner convertToBannerUpload(BannerType bannerType, String bannerImg, Ban .builder() .bannerImg(bannerImg) .bannerType(bannerType) + .name(bannerUploadDto.getName()) .contentsUrl(bannerUploadDto.getContentsUrl()) .build(); } @@ -54,4 +56,30 @@ private BannerRes.BannerList convertToBannerInfo(Banner result) { .contentsUrl(result.getContentsUrl()) .build(); } + + public List convertToBannerLists(List content) { + List bannerLists = new ArrayList<>(); + + content.forEach( + result -> + bannerLists.add( + convertToBannerListDto(result) + ) + ); + + return bannerLists; + } + + private BannerRes.BannerAdminListDto convertToBannerListDto(Banner result) { + return BannerRes.BannerAdminListDto + .builder() + .bannerId(result.getId()) + .bannerType(result.getBannerType()) + .eventId(result.getEventId()) + .name(result.getName()) + .bannerImg(result.getBannerImg()) + .startDate(result.getStartDate()) + .endDate(result.getEndDate()) + .build(); + } } diff --git a/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerReq.java b/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerReq.java index 5097a7e7..26a5b72e 100644 --- a/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerReq.java +++ b/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerReq.java @@ -17,6 +17,8 @@ public static class BannerUpload { private String contentsUrl; + private String name; + private LocalDateTime startDate; private LocalDateTime endDate; diff --git a/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerRes.java b/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerRes.java index 1cc454da..a71754c9 100644 --- a/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerRes.java +++ b/Match-Api/src/main/java/com/example/matchapi/banner/dto/BannerRes.java @@ -1,9 +1,12 @@ package com.example.matchapi.banner.dto; import com.example.matchdomain.banner.enums.BannerType; +import com.fasterxml.jackson.annotation.JsonFormat; + import lombok.*; import java.io.Serializable; +import java.time.LocalDateTime; public class BannerRes { @Getter @@ -24,4 +27,27 @@ public static class BannerList implements Serializable { } + + @Getter + @Setter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class BannerAdminListDto { + private Long bannerId; + + private BannerType bannerType; + + private String name; + + private String bannerImg; + + private Long eventId; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime startDate; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime endDate; + } } diff --git a/Match-Domain/src/main/generated/com/example/matchdomain/banner/entity/QBanner.java b/Match-Domain/src/main/generated/com/example/matchdomain/banner/entity/QBanner.java index 1d36e76c..bb427f07 100644 --- a/Match-Domain/src/main/generated/com/example/matchdomain/banner/entity/QBanner.java +++ b/Match-Domain/src/main/generated/com/example/matchdomain/banner/entity/QBanner.java @@ -41,6 +41,8 @@ public class QBanner extends EntityPathBase { public final NumberPath id = createNumber("id", Long.class); + public final StringPath name = createString("name"); + public final DateTimePath startDate = createDateTime("startDate", java.time.LocalDateTime.class); //inherited diff --git a/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java b/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java index 2774e61f..63cdd392 100644 --- a/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java +++ b/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java @@ -6,6 +6,9 @@ import com.example.matchdomain.keyword.entity.SearchKeyword; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import java.time.LocalDateTime; import java.util.ArrayList; @@ -32,4 +35,10 @@ public Banner findById(Long bannerId) { public void deleteById(Long bannerId) { bannerRepository.deleteById(bannerId); } + + public Page getBannerLists(int page, int size) { + Pageable pageable = PageRequest.of(page, size); + return bannerRepository.findByOrderByCreatedAtDesc(pageable); + + } } diff --git a/Match-Domain/src/main/java/com/example/matchdomain/banner/entity/Banner.java b/Match-Domain/src/main/java/com/example/matchdomain/banner/entity/Banner.java index 22ca647c..dd261855 100644 --- a/Match-Domain/src/main/java/com/example/matchdomain/banner/entity/Banner.java +++ b/Match-Domain/src/main/java/com/example/matchdomain/banner/entity/Banner.java @@ -31,6 +31,8 @@ public class Banner extends BaseEntity { @Enumerated(EnumType.STRING) private BannerType bannerType; + private String name; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "eventId",nullable = false, insertable=false, updatable=false) private Event event; diff --git a/Match-Domain/src/main/java/com/example/matchdomain/banner/repository/BannerRepository.java b/Match-Domain/src/main/java/com/example/matchdomain/banner/repository/BannerRepository.java index d33b0dac..685f0ad5 100644 --- a/Match-Domain/src/main/java/com/example/matchdomain/banner/repository/BannerRepository.java +++ b/Match-Domain/src/main/java/com/example/matchdomain/banner/repository/BannerRepository.java @@ -1,6 +1,9 @@ package com.example.matchdomain.banner.repository; import com.example.matchdomain.banner.entity.Banner; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import java.time.LocalDateTime; @@ -10,4 +13,6 @@ public interface BannerRepository extends JpaRepository { List findAllByOrderByCreatedAtDesc(); List findByStartDateLessThanAndEndDateGreaterThanOrderByCreatedAtDesc(LocalDateTime now, LocalDateTime now1); + + Page findByOrderByCreatedAtDesc(Pageable pageable); } From 4665229fab595a4b57c6b204fbe074615dc6b02e Mon Sep 17 00:00:00 2001 From: LimHyunwoo <81962309+imenuuu@users.noreply.github.com> Date: Mon, 18 Dec 2023 16:51:34 +0900 Subject: [PATCH 2/2] =?UTF-8?q?:zap:=20:=20=EA=B4=80=EB=A6=AC=EC=9E=90=20?= =?UTF-8?q?=EB=B0=B0=EB=84=88=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?(#225)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../banner/adaptor/BannerAdaptor.java | 5 ++- .../banner/exception/BannerGerErrorCode.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Match-Domain/src/main/java/com/example/matchdomain/banner/exception/BannerGerErrorCode.java diff --git a/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java b/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java index 63cdd392..99699594 100644 --- a/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java +++ b/Match-Domain/src/main/java/com/example/matchdomain/banner/adaptor/BannerAdaptor.java @@ -1,6 +1,9 @@ package com.example.matchdomain.banner.adaptor; +import static com.example.matchdomain.banner.exception.BannerGerErrorCode.*; + import com.example.matchcommon.annotation.Adaptor; +import com.example.matchcommon.exception.NotFoundException; import com.example.matchdomain.banner.entity.Banner; import com.example.matchdomain.banner.repository.BannerRepository; import com.example.matchdomain.keyword.entity.SearchKeyword; @@ -29,7 +32,7 @@ public Banner save(Banner banner) { } public Banner findById(Long bannerId) { - return bannerRepository.findById(bannerId).orElseThrow(); + return bannerRepository.findById(bannerId).orElseThrow(() -> new NotFoundException(NOT_EXISTS_BANNER)); } public void deleteById(Long bannerId) { diff --git a/Match-Domain/src/main/java/com/example/matchdomain/banner/exception/BannerGerErrorCode.java b/Match-Domain/src/main/java/com/example/matchdomain/banner/exception/BannerGerErrorCode.java new file mode 100644 index 00000000..667de9a7 --- /dev/null +++ b/Match-Domain/src/main/java/com/example/matchdomain/banner/exception/BannerGerErrorCode.java @@ -0,0 +1,44 @@ +package com.example.matchdomain.banner.exception; + +import static org.springframework.http.HttpStatus.*; + +import java.lang.reflect.Field; +import java.util.Objects; + +import org.springframework.http.HttpStatus; + +import com.example.matchcommon.annotation.ExplainError; +import com.example.matchcommon.dto.ErrorReason; +import com.example.matchcommon.exception.errorcode.BaseErrorCode; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum BannerGerErrorCode implements BaseErrorCode { + @ExplainError("해당 도네이션이 존재하지 않습니다.") + NOT_EXISTS_BANNER(NOT_FOUND,"DONATION001", "해당 배너가 존재하지 않습니다."); + + + private final HttpStatus httpStatus; + private final String code; + private final String message; + + @Override + public ErrorReason getErrorReason() { + return ErrorReason.builder().message(message).code(code).isSuccess(false).build(); + } + + @Override + public String getExplainError() throws NoSuchFieldException { + Field field = this.getClass().getField(this.name()); + ExplainError annotation = field.getAnnotation(ExplainError.class); + return Objects.nonNull(annotation) ? annotation.value() : this.getMessage(); + } + + @Override + public ErrorReason getErrorReasonHttpStatus(){ + return ErrorReason.builder().message(message).code(code).isSuccess(false).httpStatus(httpStatus).build(); + } +} \ No newline at end of file