Skip to content

Commit

Permalink
Merge pull request #197 from Team-HMH/feat/#196-get-banner
Browse files Browse the repository at this point in the history
feat - 배너 정보 불러오기
  • Loading branch information
jumining authored Nov 20, 2024
2 parents f19afda + bfd12cc commit da402e9
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/sopt/org/hmh/domain/banner/Banner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sopt.org.hmh.domain.banner;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Banner {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String title;
private String subTitle;
private String imageUrl;
private String linkUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sopt.org.hmh.domain.banner;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface BannerJpaRepository extends JpaRepository<Banner, Long> {
Optional<Banner> findTopByOrderByIdAsc(); // 디비에 배너 하나만 존재
}
20 changes: 20 additions & 0 deletions src/main/java/sopt/org/hmh/domain/banner/BannerResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sopt.org.hmh.domain.banner;

import lombok.Builder;

@Builder
public record BannerResponse(
String title,
String subTitle,
String imageUrl,
String linkUrl
) {
public static BannerResponse of(Banner banner) {
return BannerResponse.builder()
.title(banner.getTitle())
.subTitle(banner.getSubTitle())
.imageUrl(banner.getImageUrl())
.linkUrl(banner.getLinkUrl())
.build();
}
}
19 changes: 19 additions & 0 deletions src/main/java/sopt/org/hmh/domain/banner/BannerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sopt.org.hmh.domain.banner;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import sopt.org.hmh.domain.banner.exception.BannerError;
import sopt.org.hmh.domain.banner.exception.BannerException;

@Service
@RequiredArgsConstructor
public class BannerService {

private final BannerJpaRepository bannerRepository;

public BannerResponse getBanner() {
return bannerRepository.findTopByOrderByIdAsc()
.map(BannerResponse::of)
.orElseThrow(() -> new BannerException(BannerError.BANNER_NOT_FOUND));
}
}
29 changes: 29 additions & 0 deletions src/main/java/sopt/org/hmh/domain/banner/controller/BannerApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sopt.org.hmh.domain.banner.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import sopt.org.hmh.domain.banner.BannerResponse;
import sopt.org.hmh.global.common.response.BaseResponse;

@Tag(name = "배너 관련 API")
public interface BannerApi {

@Operation(
summary = "배너 정보를 불러오는 API",
responses = {
@ApiResponse(
responseCode = "200",
description = "배너 정보 불러오기에 성공하였습니다."),
@ApiResponse(
responseCode = "404",
description = "배너를 찾을 수 없습니다.",
content = @Content),
@ApiResponse(
responseCode = "500",
description = "서버 내부 오류입니다.",
content = @Content)})
ResponseEntity<BaseResponse<BannerResponse>> orderGetBanner();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sopt.org.hmh.domain.banner.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sopt.org.hmh.domain.banner.BannerResponse;
import sopt.org.hmh.domain.banner.BannerService;
import sopt.org.hmh.domain.banner.exception.BannerSuccess;
import sopt.org.hmh.global.common.response.BaseResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/banner")
public class BannerController implements BannerApi {

private final BannerService bannerService;

@Override
@GetMapping
public ResponseEntity<BaseResponse<BannerResponse>> orderGetBanner() {
return ResponseEntity
.status(BannerSuccess.GET_BANNER_SUCCESS.getHttpStatus())
.body(BaseResponse.success(BannerSuccess.GET_BANNER_SUCCESS, bannerService.getBanner()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sopt.org.hmh.domain.banner.exception;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import sopt.org.hmh.global.common.exception.base.ErrorBase;

@AllArgsConstructor
public enum BannerError implements ErrorBase {

BANNER_NOT_FOUND(HttpStatus.NOT_FOUND, "배너를 찾을 수 없습니다."),
;

private final HttpStatus status;
private final String errorMessage;

@Override
public int getHttpStatusCode() {
return status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getErrorMessage() {
return this.errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sopt.org.hmh.domain.banner.exception;

import sopt.org.hmh.global.common.exception.base.ExceptionBase;

public class BannerException extends ExceptionBase {
public BannerException(BannerError error) { super(error); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sopt.org.hmh.domain.banner.exception;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import sopt.org.hmh.global.common.exception.base.SuccessBase;

@AllArgsConstructor
public enum BannerSuccess implements SuccessBase {

GET_BANNER_SUCCESS(HttpStatus.OK, "배너 정보 불러오기에 성공하였습니다."),
;

private final HttpStatus status;
private final String successMessage;

@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getSuccessMessage() {
return this.successMessage;
}
}

0 comments on commit da402e9

Please sign in to comment.