Skip to content

Commit

Permalink
Api-Release-v0.0.2-22
Browse files Browse the repository at this point in the history
Api-Release-v0.0.2-22
  • Loading branch information
imenuuu authored Dec 18, 2023
2 parents 708231e + 4665229 commit a1de764
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,23 +30,32 @@ public class AdminBannerController {
@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@Operation(summary = "ADMIN-08-01 배너 업로드")
public CommonResponse<List<BannerRes.BannerList>> 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<PageResponse<List<BannerRes.BannerAdminListDto>>> 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<String> deleteBanner(@PathVariable Long bannerId){
adminBannerService.deleteBanner(bannerId);
return CommonResponse.onSuccess("삭제 성공");
}

@PatchMapping("/{bannerId}")
@Operation(summary = "ADMIN-03 배너 수정")
@Operation(summary = "ADMIN-08-04 배너 수정")
public CommonResponse<String> patchBanner(
@PathVariable Long bannerId,
BannerReq.BannerPatchDto bannerPatchDto){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -51,4 +53,9 @@ public void deleteBanner(Long bannerId) {
public void patchBanner(Long bannerId) {

}

public PageResponse<List<BannerRes.BannerAdminListDto>> getBannerLists(int page, int size) {
Page<Banner> banners = bannerAdaptor.getBannerLists(page, size);
return new PageResponse<>(banners.isLast(), banners.getSize(), bannerConverter.convertToBannerLists(banners.getContent()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Banner convertToBannerUpload(BannerType bannerType, String bannerImg, Ban
.builder()
.bannerImg(bannerImg)
.bannerType(bannerType)
.name(bannerUploadDto.getName())
.eventId(bannerUploadDto.getEventId())
.build();
}
Expand All @@ -26,6 +27,7 @@ public Banner convertToBannerUpload(BannerType bannerType, String bannerImg, Ban
.builder()
.bannerImg(bannerImg)
.bannerType(bannerType)
.name(bannerUploadDto.getName())
.contentsUrl(bannerUploadDto.getContentsUrl())
.build();
}
Expand Down Expand Up @@ -54,4 +56,30 @@ private BannerRes.BannerList convertToBannerInfo(Banner result) {
.contentsUrl(result.getContentsUrl())
.build();
}

public List<BannerRes.BannerAdminListDto> convertToBannerLists(List<Banner> content) {
List<BannerRes.BannerAdminListDto> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class BannerUpload {

private String contentsUrl;

private String name;

private LocalDateTime startDate;

private LocalDateTime endDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class QBanner extends EntityPathBase<Banner> {

public final NumberPath<Long> id = createNumber("id", Long.class);

public final StringPath name = createString("name");

public final DateTimePath<java.time.LocalDateTime> startDate = createDateTime("startDate", java.time.LocalDateTime.class);

//inherited
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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;
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;
Expand All @@ -26,10 +32,16 @@ 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) {
bannerRepository.deleteById(bannerId);
}

public Page<Banner> getBannerLists(int page, int size) {
Pageable pageable = PageRequest.of(page, size);
return bannerRepository.findByOrderByCreatedAtDesc(pageable);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,4 +13,6 @@ public interface BannerRepository extends JpaRepository<Banner, Long> {
List<Banner> findAllByOrderByCreatedAtDesc();

List<Banner> findByStartDateLessThanAndEndDateGreaterThanOrderByCreatedAtDesc(LocalDateTime now, LocalDateTime now1);

Page<Banner> findByOrderByCreatedAtDesc(Pageable pageable);
}

0 comments on commit a1de764

Please sign in to comment.