Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4] Controller 및 테스트 코드 작성 #6

Merged
merged 14 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
.gradle
build
out

### Java ###
# Compiled class file
Expand Down
24 changes: 21 additions & 3 deletions bp-app-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
plugins {
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
id 'io.freefair.lombok' version '8.11'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation project(':bp-domain-rdb')
implementation project(':bp-kafka-event-publisher')
implementation project(':bp-dto')
implementation project(':bp-utils')
implementation project(':bp-core-web')
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요건 뭐 .. 특별히 안된다고 보기는 어렵지만, 스프링에도 비슷한 기능을 하는 클래스들이 있지 않을까요? ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spring-core 에도 비슷한 역할을 클래스가 있기는 한데, apache의 라이브러리를 계속 사용해왔던지라 사용하게 되었습니다.

혹시 관련해서 비추천 하는 이유가 있으실까요?

implementation 'org.apache.commons:commons-lang3:3.14.0'
implementation 'org.springframework.boot:spring-boot-starter-validation'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api'

testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

test {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.beautify_project.bp_app_api.controller;

import com.beautify_project.bp_app_api.dto.common.ResponseMessage;
import com.beautify_project.bp_app_api.dto.shop.ImageFiles;
import com.beautify_project.bp_app_api.dto.shop.ShopFindListRequestParameters;
import com.beautify_project.bp_app_api.dto.shop.ShopRegistrationRequest;
import com.beautify_project.bp_app_api.enumeration.OrderType;
import com.beautify_project.bp_app_api.enumeration.ShopSearchType;
import com.beautify_project.bp_app_api.service.ShopService;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
public class ShopController {

private final ShopService shopService;

@PostMapping("/v1/shops")
@ResponseStatus(code = HttpStatus.OK)
ResponseMessage registerShop(
@RequestPart(value = "images", required = false) final List<MultipartFile> imageFiles,
@Valid @RequestPart(value = "shopRegistrationInfo") final ShopRegistrationRequest shopRegistrationRequest) {

return shopService.registerShop(new ImageFiles(imageFiles), shopRegistrationRequest);
}

@GetMapping("/v1/shops")
@ResponseStatus(code = HttpStatus.OK)
ResponseMessage findShopList(@RequestParam(name = "type") final String searchType,
@RequestParam(name = "page", required = false, defaultValue = "0") final Integer page,
@RequestParam(name = "count", required = false, defaultValue = "10") final Integer count,
@RequestParam(name = "order", required = false, defaultValue = "asc") final String order)
throws RuntimeException {

return shopService.findShopList(
new ShopFindListRequestParameters(ShopSearchType.from(searchType), page, count,
OrderType.from(order)));
}

// TODO: 샵 상세 조회 구현
// TODO: 샵 수정 구현
// TODO: 샵 삭제 구현
// TODO: 샵 좋아요 구현
// TODO: 샵 좋아요 취소 구현
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.beautify_project.bp_app_api.dto.common;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public enum ErrorCode {

BR001(HttpStatus.BAD_REQUEST, "BR001", "요청 파라미터가 잘못되었습니다."),
BR002(HttpStatus.BAD_REQUEST, "BR002", "본문 형식이 맞지 않습니다."),

UA001(HttpStatus.UNAUTHORIZED, "UA001", "접근 토큰이 존재하지 않습니다."),
UA002(HttpStatus.UNAUTHORIZED, "UA002", "접근 토큰이 만료되었습니다. "),

FB001(HttpStatus.FORBIDDEN, "FB001", "해당 API 사용 권한이 없습니다."),

NF001(HttpStatus.NOT_FOUND, "NF001", "요청 URL이 잘못되었습니다."),

SH001(HttpStatus.NOT_FOUND, "SH001", "등록되지 않은 미용 시술소입니다."),

IS001(HttpStatus.INTERNAL_SERVER_ERROR, "IS001", "시스템 에러가 발생하였습니다. 관리자에게 문읜해주세요.")
;

private final HttpStatus httpStatus;
private final String errorCode;
private final String errorMessage;

ErrorCode(final HttpStatus httpStatus, final String errorCode, final String errorMessage) {
this.httpStatus = httpStatus;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.beautify_project.bp_app_api.dto.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.http.HttpStatus;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@ToString
public class ErrorResponseMessage {

@JsonIgnore
private HttpStatus httpStatus;
@JsonInclude(Include.NON_NULL)
private String errorCode;
@JsonInclude(Include.NON_NULL)
private String errorMessage;

private ErrorResponseMessage(final HttpStatus httpStatus, final String errorCode,
final String errorMessage) {
this.httpStatus = httpStatus;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

public static ErrorResponseMessage createCustomErrorMessage(final ErrorCode errorCode, final String message) {
return new ErrorResponseMessage(errorCode.getHttpStatus(), errorCode.getErrorCode(), message);
}

public static ErrorResponseMessage createErrorMessage(final ErrorCode errorCode) {
return new ErrorResponseMessage(errorCode.getHttpStatus(), errorCode.getErrorCode(),
errorCode.getErrorMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.beautify_project.bp_app_api.dto.common;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@ToString
public class ResponseMessage {

private Object returnValue;

private ResponseMessage(final Object returnValue) {
this.returnValue = returnValue;
}

// private ResponseMessage(final HttpStatus httpStatus, final Object returnValue) {
// this.httpStatus = httpStatus;
// this.returnValue = returnValue;
// }

public static ResponseMessage createResponseMessage(final Object responseBody) {
return new ResponseMessage(responseBody);
}

// public static ResponseMessage createResponseMessage(final HttpStatus httpStatus,
// final Object returnValue) {
// return new ResponseMessage(httpStatus, returnValue);
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.beautify_project.bp_app_api.dto.shop;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

public record ImageFiles(List<MultipartFile> files) {

public ImageFiles {
createEmptyListIfNull(files);
}

private void createEmptyListIfNull(List<MultipartFile> files) {
if (files == null || files.isEmpty()) {
files = new ArrayList<>();
}
}

public boolean isEmpty() {
return files == null || files.isEmpty();
}

public int size() {
return files.size();
}

public MultipartFile get(final int index) {
return files().get(index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.beautify_project.bp_app_api.dto.shop;


import com.beautify_project.bp_app_api.enumeration.OrderType;
import com.beautify_project.bp_app_api.enumeration.ShopSearchType;

public record ShopFindListRequestParameters(ShopSearchType searchType, Integer page, Integer count,
OrderType orderType) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.beautify_project.bp_app_api.dto.shop;

import java.util.List;
import lombok.Builder;

@Builder
public record ShopFindListResponse(
String id,
String name,
List<String> operations,
List<String> supportFacilities,
String rate,
Integer likes,
Boolean likePushed,
String thumbnail
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.beautify_project.bp_app_api.dto.shop;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalTime;
import java.util.List;


public record ShopRegistrationRequest(
@NotNull @Size(max = 128) String name,
@NotNull @Size(max = 13) String contact,
@Size(max = 2048) String introduction,
List<IdName> operations,
List<IdName> categories,
List<IdName> supportFacilities,
BusinessTime businessTime,
Address address) {


public record IdName (
@Size(max = 64) String id,
@Size(max = 128) String name) { }

public record BusinessTime (
LocalTime openTime,
LocalTime closeTime,
LocalTime breakBeginTime,
LocalTime breakEndTime,
List<String> offDayOfWeek){

}

public record Address (
String dongCode,
String siDoName,
String siGoonGooName,
String eubMyunDongName,
String roadNameCode,
String roadName,
String underGround,
String roadMainNum,
String roadSubNum,
String siGoonGooBuildingName,
String zipCode,
String apartComplex,
String eubMyunDongSerialNumber,
String latitude,
String longitude) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.beautify_project.bp_app_api.enumeration;


import com.beautify_project.bp_app_api.exception.EnumMismatchException;

public enum OrderType {
ASC,
DESC
;

public static OrderType from(final String input) {
final String inputUpperCase = input.toUpperCase();
try {
return valueOf(inputUpperCase);
} catch (IllegalArgumentException e) {
throw new EnumMismatchException("OrderType", input);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.beautify_project.bp_app_api.enumeration;

import com.beautify_project.bp_app_api.exception.EnumMismatchException;
import org.apache.commons.lang3.StringUtils;

public enum ShopSearchType {
SHOP_NAME,
LOCATION,
LIKE,
RATE
;

public static ShopSearchType from(final String input) {
String inputUpperCase = input.toUpperCase();

if (StringUtils.equals("SHOPNAME", inputUpperCase)) {
inputUpperCase = "SHOP_NAME";
}

try {
return valueOf(inputUpperCase);
} catch (IllegalArgumentException e) {
throw new EnumMismatchException("ShopSearchType", input);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.beautify_project.bp_app_api.exception;

import lombok.Getter;

@Getter
public class EnumMismatchException extends RuntimeException {

private String enumName;
private String value;

public EnumMismatchException(String message) {
super(message);
}

public EnumMismatchException(final String enumName, final String value) {
super(enumName + "에 해당하는 값 " + "'" + value + "' 은 올바르지 않습니다.");
this.enumName = enumName;
this.value = value;
}

}
Loading
Loading