-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat - 배너 정보 불러오기
- Loading branch information
Showing
9 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/sopt/org/hmh/domain/banner/BannerJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/java/sopt/org/hmh/domain/banner/BannerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/sopt/org/hmh/domain/banner/BannerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/sopt/org/hmh/domain/banner/controller/BannerApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/sopt/org/hmh/domain/banner/controller/BannerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/sopt/org/hmh/domain/banner/exception/BannerError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/sopt/org/hmh/domain/banner/exception/BannerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/sopt/org/hmh/domain/banner/exception/BannerSuccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |