Skip to content

Commit

Permalink
Merge pull request #85 from Gachon-Table/GTB-74
Browse files Browse the repository at this point in the history
GTB-74 [refactor] 주점 관리 API 변경
  • Loading branch information
jwnnoh authored Sep 11, 2024
2 parents 5df861c + f117746 commit 026ff26
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import site.gachontable.gachontablebe.domain.pub.domain.Pub;
import site.gachontable.gachontablebe.domain.pub.domain.repository.PubRepository;
import site.gachontable.gachontablebe.domain.pub.exception.PubNotFoundException;
import site.gachontable.gachontablebe.domain.admin.presentation.dto.request.PubManageRequest;
import site.gachontable.gachontablebe.domain.shared.dto.request.RefreshRequest;
import site.gachontable.gachontablebe.domain.shared.dto.response.RegisterResponse;
import site.gachontable.gachontablebe.domain.waiting.usecase.GetWaitings;
Expand All @@ -38,6 +39,7 @@ public class AdminController {
private final EnterUser enterUser;
private final CallUser callUser;
private final ExitUser exitUser;
private final ManagePub managePub;
private final UpdateStatus updateStatus;
private final GetSeatings getSeatings;

Expand Down Expand Up @@ -172,4 +174,17 @@ public ResponseEntity<RegisterResponse> updateOpenStatus(@AuthenticationPrincipa
public ResponseEntity<RegisterResponse> updateWaitingStatus(@AuthenticationPrincipal AuthDetails authDetails, @RequestBody UpdateStatusRequest request) {
return ResponseEntity.ok(updateStatus.executeForWaitingStatus(authDetails, request));
}

@Operation(summary = "주점 관리", description = "메뉴 등록, 대표 사진 및 학생증 필수 여부를 수정할 수 있습니다.")
@ApiResponses({
@ApiResponse(responseCode = "201"),
@ApiResponse(responseCode = "400", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/manage")
public ResponseEntity<String> managePub(@AuthenticationPrincipal AuthDetails authDetails, @RequestBody PubManageRequest request){
return ResponseEntity.ok(managePub.execute(authDetails, request));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package site.gachontable.gachontablebe.domain.pub.presentation.dto.request;
package site.gachontable.gachontablebe.domain.admin.presentation.dto.request;

import java.util.List;

public record PubManageRequest(List<String> thumbnails,
Boolean studentCard,
List<MenuRequest> menuRequests) {
public record MenuRequest(String menuName,
public record MenuRequest(Integer menuId,
String thumbnail,
String menuName,
String price,
String oneLiner) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ public class EnterUser {

@RedissonLock(key = "#lockKey")
public String execute(AuthDetails authDetails, EnterUserRequest request, String lockKey) {
Admin admin = adminRepository.findById(authDetails.getUuid()).
orElseThrow(AdminNotFoundException::new);
Waiting waiting = waitingRepository.findById(request.waitingId()).
orElseThrow(WaitingNotFoundException::new);
Pub pub = waiting.getPub();

if (!pub.equals(admin.getPub())) {
throw new PubMismatchException();
}
checkPubMatches(authDetails, pub);

waiting.enter();
pub.decreaseWaitingCount();
Expand All @@ -48,9 +44,18 @@ public String execute(AuthDetails authDetails, EnterUserRequest request, String
return SuccessCode.ENTERED_SUCCESS.getMessage();
}

private void checkPubMatches(AuthDetails authDetails, Pub pub) {
Admin admin = adminRepository.findById(authDetails.getUuid()).
orElseThrow(AdminNotFoundException::new);

if (!pub.equals(admin.getPub())) {
throw new PubMismatchException();
}
}

private void createSeating(Pub pub, Waiting waiting, Integer seatingNum) {
Seating seating = Seating.create(seatingNum,
true,
Seating seating = Seating.create(
seatingNum,
LocalDateTime.now().plusHours(pub.getHours()),
pub,
waiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package site.gachontable.gachontablebe.domain.pub.usecase;
package site.gachontable.gachontablebe.domain.admin.usecase;

import site.gachontable.gachontablebe.domain.auth.domain.AuthDetails;
import site.gachontable.gachontablebe.domain.pub.presentation.dto.request.PubManageRequest;
import site.gachontable.gachontablebe.domain.admin.presentation.dto.request.PubManageRequest;

public interface ManagePub {
String execute(AuthDetails authDetails, PubManageRequest request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package site.gachontable.gachontablebe.domain.admin.usecase;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.gachontable.gachontablebe.domain.admin.domain.repository.AdminRepository;
import site.gachontable.gachontablebe.domain.admin.exception.AdminNotFoundException;
import site.gachontable.gachontablebe.domain.auth.domain.AuthDetails;
import site.gachontable.gachontablebe.domain.menu.domain.Menu;
import site.gachontable.gachontablebe.domain.menu.domain.repository.MenuRepository;
import site.gachontable.gachontablebe.domain.pub.domain.Pub;
import site.gachontable.gachontablebe.domain.pub.domain.repository.PubRepository;
import site.gachontable.gachontablebe.domain.admin.presentation.dto.request.PubManageRequest;
import site.gachontable.gachontablebe.global.success.SuccessCode;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ManagePubImpl implements ManagePub {
private final AdminRepository adminRepository;
private final MenuRepository menuRepository;
private final PubRepository pubRepository;

@Override
@Transactional
public String execute(AuthDetails authDetails, PubManageRequest request) {
Pub pub = adminRepository.findById(authDetails.getUuid())
.orElseThrow(AdminNotFoundException::new)
.getPub();

List<Menu> menus = manageMenus(request, pub);

pub.updatePubInfo(request.thumbnails(), menus);
pubRepository.save(pub);

return SuccessCode.MANAGE_PUB_SUCCESS.getMessage();
}

private List<Menu> manageMenus(PubManageRequest request, Pub pub) {
Map<Integer, Menu> existingMenus = menuRepository.findAllByPub(pub).stream()
.collect(Collectors.toMap(Menu::getMenuId, menu -> menu));

List<Menu> updatedMenus = request.menuRequests().stream()
.map(menuRequest -> {
Menu menu = existingMenus.get(menuRequest.menuId());
if (menu != null) {
menu.update(
menuRequest.menuName(),
menuRequest.price(),
menuRequest.oneLiner(),
menuRequest.thumbnail());
return menu;
}
return Menu.create(
pub,
menuRequest.menuName(),
menuRequest.price(),
menuRequest.oneLiner(),
menuRequest.thumbnail());
}).toList();

return menuRepository.saveAll(updatedMenus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class UpdateStatusImpl implements UpdateStatus{
@Transactional
@Override
public RegisterResponse executeForOpenStatus(AuthDetails authDetails, UpdateStatusRequest request) {
Pub pub = adminRepository.findByUsername(authDetails.getUsername())
Pub pub = adminRepository.findById(authDetails.getUuid())
.orElseThrow(AdminNotFoundException::new)
.getPub();

Expand All @@ -39,7 +39,7 @@ public RegisterResponse executeForOpenStatus(AuthDetails authDetails, UpdateStat
@Transactional
@Override
public RegisterResponse executeForWaitingStatus(AuthDetails authDetails, UpdateStatusRequest request) {
Pub pub = adminRepository.findByUsername(authDetails.getUsername())
Pub pub = adminRepository.findById(authDetails.getUuid())
.orElseThrow(AdminNotFoundException::new)
.getPub();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Menu {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer menuID;
private Integer menuId;

@ManyToOne(fetch = FetchType.LAZY)
private Pub pub;
Expand All @@ -22,27 +22,34 @@ public class Menu {
@Column(nullable = false)
private String price;

@Column
@Column(nullable = false)
private String oneLiner;

public static Menu create(Pub pub, String menuName, String price, String oneLiner) {
@Column
private String thumbnail;

public static Menu create(Pub pub, String menuName, String price, String oneLiner, String thumbnail) {
return Menu.builder()
.pub(pub)
.menuName(menuName)
.price(price)
.oneLiner(oneLiner)
.thumbnail(thumbnail)
.build();
}

@Builder
public Menu(Pub pub, String menuName, String price, String oneLiner) {
public Menu(Pub pub, String menuName, String price, String oneLiner, String thumbnail) {
this.pub = pub;
this.menuName = menuName;
this.price = price;
this.oneLiner = oneLiner;
this.thumbnail = thumbnail;
}

public void update(String price, String oneLiner) {
public void update(String menuName, String price, String oneLiner, String thumbnail) {
this.thumbnail = thumbnail;
this.menuName = menuName;
this.price = price;
this.oneLiner = oneLiner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
import java.util.List;

public interface MenuRepository extends JpaRepository<Menu, Integer> {
List<Menu> findByPub(Pub pub);
List<Menu> findAllByPub(Pub pub);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Pub {
@Column(nullable = false)
private String instagramUrl;

@Column(nullable = false)
@Column
private Boolean studentCard;

@Column(nullable = false)
Expand Down Expand Up @@ -125,9 +125,8 @@ private void validateWaitingCount() {
}
}

public void updatePubInfo(List<String> thumbnails, Boolean studentCard, List<Menu> menus) {
public void updatePubInfo(List<String> thumbnails, List<Menu> menus) {
this.thumbnails = thumbnails;
this.studentCard = studentCard;
this.menus = menus;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import site.gachontable.gachontablebe.domain.auth.domain.AuthDetails;
import site.gachontable.gachontablebe.domain.pub.presentation.dto.request.PubManageRequest;
import site.gachontable.gachontablebe.domain.pub.presentation.dto.request.PubRegisterRequest;
import site.gachontable.gachontablebe.domain.pub.presentation.dto.response.GetPubDetailsResponse;
import site.gachontable.gachontablebe.domain.pub.presentation.dto.response.GetPubsResponse;
import site.gachontable.gachontablebe.domain.pub.usecase.ManagePub;
import site.gachontable.gachontablebe.domain.pub.usecase.PubService;
import site.gachontable.gachontablebe.domain.pub.usecase.SearchPub;
import site.gachontable.gachontablebe.domain.shared.dto.response.RegisterResponse;
Expand All @@ -27,7 +23,6 @@
@RequiredArgsConstructor
public class PubController {
private final PubService pubService;
private final ManagePub managePub;
private final SearchPub searchPub;

@Operation(summary = "주점 목록", description = "전체 주점 목록을 가져옵니다")
Expand Down Expand Up @@ -69,19 +64,6 @@ public ResponseEntity<RegisterResponse> register(@RequestBody PubRegisterRequest
return ResponseEntity.ok(pubService.register(request));
}

@Operation(summary = "주점 관리", description = "메뉴 등록, 대표 사진 및 학생증 필수 여부를 수정할 수 있습니다.")
@ApiResponses({
@ApiResponse(responseCode = "201"),
@ApiResponse(responseCode = "400", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/manage")
public ResponseEntity<String> managePub(@AuthenticationPrincipal AuthDetails authDetails, @RequestBody PubManageRequest request){
return ResponseEntity.ok(managePub.execute(authDetails, request));
}

@Operation(summary = "주점 검색", description = "랜딩페이지에서 검색을 통해 원하는 주점을 조회합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public static PubInfo from(Pub pub) {


public record MenuInfo(
Integer menuId,
String menuName,
String price,
String oneLiner
) {
public static MenuInfo from(Menu menu) {
return new MenuInfo(
menu.getMenuId(),
menu.getMenuName(),
menu.getPrice(),
menu.getOneLiner()
Expand Down

This file was deleted.

Loading

0 comments on commit 026ff26

Please sign in to comment.