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

[#25] [Feature] 반찬가게 API 구현 #27

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package core.startup.mealtoktok.api.dishstore;

import java.util.List;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.api.dishstore.request.DishStoreRequest;
import core.startup.mealtoktok.api.dishstore.response.DishStoreResponse;
import core.startup.mealtoktok.common.dto.Response;
import core.startup.mealtoktok.domain.dishstore.DishStore;
import core.startup.mealtoktok.domain.dishstore.DishStoreService;
import core.startup.mealtoktok.domain.dishstore.TargetDishStore;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class DishStoreApi implements DishStoreApiDocs {

private final DishStoreService dishStoreService;

@PostMapping("/admin/dish-stores")
public Response<Void> createDishStore(
@RequestBody DishStoreRequest dishStoreRequest,
@AuthenticationPrincipal User currnetUser) {
dishStoreService.createDishStore(dishStoreRequest.toDishStoreInfo());
return Response.success("반찬 가게 생성 성공");
}

@DeleteMapping("/admin/dish-stores/{dishStoreId}")
public Response<Void> deleteDishStore(
@PathVariable("dishStoreId") Long dishStoreId,
@AuthenticationPrincipal User currentUser) {
dishStoreService.deleteDishStore(TargetDishStore.from(dishStoreId));
return Response.success("반찬 가게 삭제 성공");
}

@PutMapping("/admin/dish-stores/{dishStoreId}")
public Response<Void> updateDishStore(
@PathVariable("dishStoreId") Long dishStoreId,
@RequestBody DishStoreRequest dishStoreRequest,
@AuthenticationPrincipal User currentUser) {
dishStoreService.updateDishStore(
TargetDishStore.from(dishStoreId), dishStoreRequest.toDishStoreInfo());
return Response.success("반찬 가게 수정 성공");
}

@GetMapping("/dish-stores")
public Response<List<DishStoreResponse>> readDishStores(
@AuthenticationPrincipal User currentUser) {
List<DishStoreResponse> dishStoreResponse =
dishStoreService.readDishStores().stream().map(DishStoreResponse::from).toList();
return Response.success(dishStoreResponse);
}

@GetMapping("/dish-stores/{dishStoreId}")
public Response<DishStoreResponse> readDishStores(
@PathVariable("dishStoreId") Long dishStoreId,
@AuthenticationPrincipal User currentUser) {
DishStore dishStore = dishStoreService.readDishStore(TargetDishStore.from(dishStoreId));
return Response.success(DishStoreResponse.from(dishStore));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.startup.mealtoktok.api.dishstore;

import java.util.List;

import org.springframework.security.core.userdetails.User;

import core.startup.mealtoktok.api.dishstore.request.DishStoreRequest;
import core.startup.mealtoktok.api.dishstore.response.DishStoreResponse;
import core.startup.mealtoktok.common.dto.Response;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "반찬 가게 API")
public interface DishStoreApiDocs {

@Operation(summary = "반찬 가게 생성")
Response<Void> createDishStore(DishStoreRequest dishStoreRequest, User currentUser);

@Operation(summary = "반찬 가게 삭제")
Response<Void> deleteDishStore(Long dishStoreId, User currentUser);

@Operation(summary = "반찬 가게 수정")
Response<Void> updateDishStore(
Long dishStoreId, DishStoreRequest dishStoreRequest, User currentUser);

@Operation(summary = "반찬 가게 목록 조회")
Response<List<DishStoreResponse>> readDishStores(User currentUser);

@Operation(summary = "반찬 가게 상세 조회")
Response<DishStoreResponse> readDishStores(Long dishStoreId, User currentUser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.startup.mealtoktok.api.dishstore.request;

import java.time.LocalTime;

import core.startup.mealtoktok.domain.dishstore.DishStoreInfo;
import core.startup.mealtoktok.domain.user.Address;

public record DishStoreRequest(
String storeName,
String phoneNumber,
Address address,
Double latitude,
Double longitude,
LocalTime openTime,
LocalTime closeTime) {

public DishStoreInfo toDishStoreInfo() {
return DishStoreInfo.of(
storeName, phoneNumber, address, latitude, longitude, openTime, closeTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.startup.mealtoktok.api.dishstore.response;

import java.time.LocalTime;

import core.startup.mealtoktok.domain.dishstore.DishStore;
import core.startup.mealtoktok.domain.user.Address;

public record DishStoreResponse(
String storeName,
String phoneNumber,
Address address,
Double latitude,
Double longitude,
LocalTime openTime,
LocalTime closeTime) {
public static DishStoreResponse from(DishStore dishStore) {
return new DishStoreResponse(
dishStore.getDishStoreInfo().storeName(),
dishStore.getDishStoreInfo().phoneNumber(),
dishStore.getDishStoreInfo().addressWithCoordinate().address(),
dishStore.getDishStoreInfo().addressWithCoordinate().coordinate().latitude(),
dishStore.getDishStoreInfo().addressWithCoordinate().coordinate().longitude(),
dishStore.getDishStoreInfo().operatingHour().openTime(),
dishStore.getDishStoreInfo().operatingHour().closeTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public DishCategory read(TargetDishCategory targetDishCategory) {
}

public List<DishCategory> readAll() {
return dishRepository.readAllCategories();
return dishRepository.findAllCategories();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface DishRepository {

void deleteDishCategory(DishCategory dishCategory);

List<DishCategory> readAllCategories();
List<DishCategory> findAllCategories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import lombok.Builder;
import lombok.Getter;

import core.startup.mealtoktok.domain.user.AddressWithCoordinate;

@Getter
@Builder
public class DishStore {

private Long storeId;
private String storeName;
private String phoneNumber;
private AddressWithCoordinate addressWithCoordinate;
private OperatingHour operatingHour;
private DishStoreInfo dishStoreInfo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.startup.mealtoktok.domain.dishstore;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class DishStoreAppender {

private final DishStoreValidator dishStoreValidator;
private final DishStoreRepository dishStoreRepository;

public void append(DishStoreInfo dishStoreInfo) {
dishStoreValidator.validate(dishStoreInfo.storeName());
dishStoreRepository.save(dishStoreInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package core.startup.mealtoktok.domain.dishstore;

import java.time.LocalTime;

import core.startup.mealtoktok.domain.user.Address;
import core.startup.mealtoktok.domain.user.AddressWithCoordinate;

public record DishStoreInfo(
Copy link
Contributor

@JiwonKKang JiwonKKang Aug 9, 2024

Choose a reason for hiding this comment

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

DishStore의 내용들을 DishStoreInfo로 래핑하셔서 DishStore에서 이 DTO를 사용하셔도 될꺼같아요
이거만 고치시고 merge하시면 될거같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 감사합니다~~

String storeName,
String phoneNumber,
AddressWithCoordinate addressWithCoordinate,
OperatingHour operatingHour) {
public static DishStoreInfo of(
String storeName,
String phoneNumber,
Address address,
Double latitude,
Double longitude,
LocalTime openTime,
LocalTime closeTime) {
return new DishStoreInfo(
storeName,
phoneNumber,
AddressWithCoordinate.of(address, latitude, longitude),
OperatingHour.of(openTime, closeTime));
}

public static DishStoreInfo of(
String storeName,
String phoneNumber,
AddressWithCoordinate addressWithCoordinate,
OperatingHour operatingHour) {
return new DishStoreInfo(storeName, phoneNumber, addressWithCoordinate, operatingHour);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core.startup.mealtoktok.domain.dishstore;

import java.util.List;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -13,4 +15,8 @@ public class DishStoreReader {
public DishStore read(TargetDishStore targetStore) {
return dishStoreRepository.findById(targetStore);
}

public List<DishStore> readAll() {
return dishStoreRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.startup.mealtoktok.domain.dishstore;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class DishStoreRemover {

private final DishStoreRepository dishStoreRepository;

public void remove(TargetDishStore targetDishStore) {
dishStoreRepository.delete(targetDishStore);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package core.startup.mealtoktok.domain.dishstore;

import java.util.List;

public interface DishStoreRepository {

DishStore findById(TargetDishStore targetStore);

void save(DishStoreInfo dishStoreInfo);

boolean existsByDishStoreName(String dishStoreName);

void delete(TargetDishStore targetDishStore);

void update(TargetDishStore targetDishStore, DishStoreInfo dishStoreInfo);

List<DishStore> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core.startup.mealtoktok.domain.dishstore;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class DishStoreService {

private final DishStoreAppender dishStoreAppender;
private final DishStoreRemover dishStoreRemover;
private final DishStoreReader dishStoreReader;
private final DishStoreUpdater dishStoreUpdater;

public void createDishStore(DishStoreInfo dishStoreInfo) {
dishStoreAppender.append(dishStoreInfo);
}

public void deleteDishStore(TargetDishStore targetDishStore) {
dishStoreReader.read(targetDishStore);
dishStoreRemover.remove(targetDishStore);
}

public void updateDishStore(TargetDishStore targetDishStore, DishStoreInfo dishStoreInfo) {
dishStoreReader.read(targetDishStore);
dishStoreUpdater.update(targetDishStore, dishStoreInfo);
}

public DishStore readDishStore(TargetDishStore targetDishStore) {
return dishStoreReader.read(targetDishStore);
}

public List<DishStore> readDishStores() {
return dishStoreReader.readAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.startup.mealtoktok.domain.dishstore;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class DishStoreUpdater {

private final DishStoreValidator dishStoreValidator;
private final DishStoreRepository dishStoreRepository;

public void update(TargetDishStore targetDishStore, DishStoreInfo dishStoreInfo) {
dishStoreValidator.validate(dishStoreInfo.storeName());
dishStoreRepository.update(targetDishStore, dishStoreInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.startup.mealtoktok.domain.dishstore;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.domain.dishstore.exception.DishStoreNameAlreadyExitsException;

@Component
@RequiredArgsConstructor
public class DishStoreValidator {

private final DishStoreRepository dishStoreRepository;

public void validate(String DishStoreName) {
if (dishStoreRepository.existsByDishStoreName(DishStoreName)) {
throw DishStoreNameAlreadyExitsException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum DishStoreErrorCode implements BaseErrorCode {
DISH_NAME_ALREADY_EXISTS(CONFLICT, "DISH_409_1", "이미 존재하는 반찬 이름입니다."),
DISH_STORE_NOT_FOUND(NOT_FOUND, "DISH_STORE_404_1", "반찬 가게의 정보를 찾지 못했습니다."),
DISH_CATEGORY_NOT_FOUND(NOT_FOUND, "DISH_CATEGORY_404_1", "반찬 카테고리의 정보를 찾지 못했습니다."),
DISH_CATEGORY_NAME_ALREADY_EXISTS(CONFLICT, "DISH_STORE_409_1", "이미 존재하는 반찬 카테고리 이름입니다.");
DISH_CATEGORY_NAME_ALREADY_EXISTS(CONFLICT, "DISH_CATEGORY_409_1", "이미 존재하는 반찬 카테고리 이름입니다."),
DISH_STORE_NAME_ALREADY_EXISTS(CONFLICT, "DISH_STORE_409_1", "이미 존재하는 반찬 가게 이름입니다.");

private final Integer status;
private final String errorCode;
Expand Down
Loading