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

7주차 미션 / 서버 2조 권태완 #12

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
Binary file added .DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions src/main/java/kuit/server/common/exception/OrderException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kuit.server.common.exception;

import kuit.server.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class OrderException extends RuntimeException {
private final ResponseStatus exceptionStatus;

public OrderException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

public OrderException(ResponseStatus exceptionStatus, String message) {
super(message);
this.exceptionStatus = exceptionStatus;
}
}
14 changes: 14 additions & 0 deletions src/main/java/kuit/server/common/exception/StoreException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kuit.server.common.exception;

import kuit.server.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class StoreException extends RuntimeException{
private final ResponseStatus exceptionStatus;

public StoreException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import jakarta.annotation.Priority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static kuit.server.common.response.status.BaseExceptionResponseStatus.BAD_DATABASE_ID_REQUEST;
import static kuit.server.common.response.status.BaseExceptionResponseStatus.BAD_SQL_GRAMMAR;
import static kuit.server.common.response.status.BaseExceptionResponseStatus.DATABASE_ERROR;

Expand All @@ -32,4 +34,10 @@ public BaseErrorResponse handle_DataAccessException(DataAccessException e) {
return new BaseErrorResponse(DATABASE_ERROR);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(EmptyResultDataAccessException.class)
public BaseErrorResponse handle_EmptyResultDataAccessException(EmptyResultDataAccessException e) {
log.error("[handle_EmptyResultDataAccessException]", e);
return new BaseErrorResponse(BAD_DATABASE_ID_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kuit.server.common.exception_handler;

import static kuit.server.common.response.status.BaseExceptionResponseStatus.ORDER_ERROR;

import jakarta.annotation.Priority;
import kuit.server.common.exception.OrderException;
import kuit.server.common.response.BaseErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class OrderExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(OrderException.class)
public BaseErrorResponse handle_UserException(OrderException e) {
log.error("[handle_OrderException]", e);
return new BaseErrorResponse(ORDER_ERROR, e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
BAD_REQUEST(2000, HttpStatus.BAD_REQUEST.value(), "유효하지 않은 요청입니다."),
URL_NOT_FOUND(2001, HttpStatus.BAD_REQUEST.value(), "유효하지 않은 URL 입니다."),
METHOD_NOT_ALLOWED(2002, HttpStatus.METHOD_NOT_ALLOWED.value(), "해당 URL에서는 지원하지 않는 HTTP Method 입니다."),
BAD_DATABASE_ID_REQUEST(2003, HttpStatus.BAD_REQUEST.value(), "잘못된 데이터id에 대한 접근입니다."),

/**
* 3000: Server, Database 오류 (INTERNAL_SERVER_ERROR)
Expand All @@ -42,10 +43,22 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
INVALID_USER_VALUE(5000, HttpStatus.BAD_REQUEST.value(), "회원가입 요청에서 잘못된 값이 존재합니다."),
DUPLICATE_EMAIL(5001, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 이메일입니다."),
DUPLICATE_NICKNAME(5002, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 닉네임입니다."),
USER_NOT_FOUND(4003, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 회원입니다."),
PASSWORD_NO_MATCH(4004, HttpStatus.BAD_REQUEST.value(), "비밀번호가 일치하지 않습니다."),
INVALID_USER_STATUS(4005, HttpStatus.BAD_REQUEST.value(), "잘못된 회원 status 값입니다."),
EMAIL_NOT_FOUND(4006, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 이메일입니다.");
USER_NOT_FOUND(5003, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 회원입니다."),
PASSWORD_NO_MATCH(5004, HttpStatus.BAD_REQUEST.value(), "비밀번호가 일치하지 않습니다."),
INVALID_USER_STATUS(5005, HttpStatus.BAD_REQUEST.value(), "잘못된 회원 status 값입니다."),
EMAIL_NOT_FOUND(5006, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 이메일입니다."),

/**
* 6000: Store 오류
*/
STORE_ERROR(6000, HttpStatus.BAD_REQUEST.value(), "가게 정보에 오류가 있습니다."),
EMPTY_MENU(6001, HttpStatus.BAD_REQUEST.value(), "메뉴가 비어있습니다."),

/**
* 7000: Order 오류ㅓ
*/
ORDER_ERROR(7000, HttpStatus.BAD_REQUEST.value(), "주문 정보에 오류가 있습니다."),
WRONG_ORDER_ID(7001, HttpStatus.BAD_REQUEST.value(), "잘못된 주문ID의 접근입니다.");

private final int code;
private final int status;
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/kuit/server/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kuit.server.controller;

import kuit.server.common.response.BaseResponse;
import kuit.server.dto.order.GetOrderResponse;
import kuit.server.dto.order.PostOrderRequest;
import kuit.server.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/orders")
public class OrderController {

private final OrderService orderService;
@PostMapping("/test")
public String test(@RequestBody PostOrderRequest postOrderRequest) {
log.info(postOrderRequest.toString());
return "Success?";
}

// public String registerOrder(@RequestBody PostOrderRequest postOrderRequest) {
//
// }

@PatchMapping("/{orderId}/cancel")
public BaseResponse<String> cancelOrder(@PathVariable long orderId) {
orderService.cancelOrder(orderId);

return new BaseResponse<>("orderId=" + orderId + " 삭제처리 되었습니다.");
}

@GetMapping("/{orderId}")
public GetOrderResponse getOrder(@PathVariable long orderId) {
return orderService.getOrder(orderId);
}
}
22 changes: 22 additions & 0 deletions src/main/java/kuit/server/controller/StoreController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kuit.server.controller;

import java.util.List;
import kuit.server.dto.store.GetStoreMenuResponse;
import kuit.server.service.StoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stores")
@RequiredArgsConstructor
public class StoreController {
private final StoreService storeService;

@GetMapping("/{storeId}/menus")
public List<GetStoreMenuResponse> getStoreMenus(@PathVariable long storeId) {
return storeService.getMenu(storeId);
}
}
13 changes: 11 additions & 2 deletions src/main/java/kuit/server/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class UserController {
/**
* 회원 가입
*/
@PostMapping("")
@PostMapping("/signup")
public BaseResponse<PostUserResponse> signUp(@Validated @RequestBody PostUserRequest postUserRequest, BindingResult bindingResult) {
log.info("[UserController.signUp]");
if (bindingResult.hasErrors()) {
Expand All @@ -49,7 +49,7 @@ public BaseResponse<Object> modifyUserStatus_dormant(@PathVariable long userId)
/**
* 회원 탈퇴
*/
@PatchMapping("/{userId}/deleted")
@PatchMapping("/{userId}/cancel")
public BaseResponse<Object> modifyUserStatus_deleted(@PathVariable long userId) {
log.info("[UserController.modifyUserStatus_delete]");
userService.modifyUserStatus_deleted(userId);
Expand Down Expand Up @@ -85,4 +85,13 @@ public BaseResponse<List<GetUserResponse>> getUsers(
return new BaseResponse<>(userService.getUsers(nickname, email, status));
}

@GetMapping("/{userId}")
public GetUserResponse getUserInfo(@PathVariable long userId) {
return userService.getUserInfoByUserId(userId);
}

@GetMapping("/{userId}/order-history")
public List<GetUserOrderHistoryResponse> getUserOrderHistory (@PathVariable long userId) {
return userService.getOrderByUserId(userId);
}
}
38 changes: 38 additions & 0 deletions src/main/java/kuit/server/dao/OrderDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kuit.server.dao;

import java.util.Map;
import kuit.server.dto.order.GetOrderResponse;
import kuit.server.dto.order.PostOrderRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
@RequiredArgsConstructor
public class OrderDao {
private final NamedParameterJdbcTemplate jdbcTemplate;

public void registerOrder(PostOrderRequest postOrderRequest) { // TODO: 동적 쿼리;;;
String sql = "insert all";
}

public int cancelOrder(long orderId) {
String sql = "update `order` set status=:status where order_id=:order_id";
Map<String, Object> param = Map.of(
"status", "deleted",
"order_id", orderId);
return jdbcTemplate.update(sql, param); // returns affected row
}

public GetOrderResponse getOrder(long orderId) {
String sql = "select order_id, status, total from `order` "
+ "where order_id=:order_id";

Map<String, Object> param = Map.of("order_id", orderId);

return jdbcTemplate.queryForObject(sql, param, new BeanPropertyRowMapper<>(GetOrderResponse.class));
}
}
31 changes: 31 additions & 0 deletions src/main/java/kuit/server/dao/StoreDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kuit.server.dao;

import java.util.List;
import java.util.Map;
import kuit.server.dto.store.GetStoreMenuResponse;
import kuit.server.dto.user.GetUserResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
@RequiredArgsConstructor
public class StoreDao {
private final NamedParameterJdbcTemplate jdbcTemplate;

public List<GetStoreMenuResponse> getMenus(long storeId) {
String sql = "select menu.store_id, menu.name, menu.price from store "
+ "join menu on store.store_id = menu.store_id "
+ "where menu.store_id=:storeId";

Map<String, Object> param = Map.of("storeId", storeId);

return jdbcTemplate.query(sql, param,
(rs, rowNum) -> new GetStoreMenuResponse(
rs.getString("menu.name"),
rs.getString("menu.price"))
);
}
}
27 changes: 23 additions & 4 deletions src/main/java/kuit/server/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package kuit.server.dao;

import kuit.server.dto.user.GetUserOrderHistoryResponse;
import kuit.server.dto.user.GetUserResponse;
import kuit.server.dto.user.PostUserRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
Expand Down Expand Up @@ -38,8 +40,8 @@ public boolean hasDuplicateNickName(String nickname) {
}

public long createUser(PostUserRequest postUserRequest) {
String sql = "insert into user(email, password, phone_number, nickname, profile_image) " +
"values(:email, :password, :phoneNumber, :nickname, :profileImage)";
String sql = "insert into user(email, password, phone_number, nickname) " +
"values(:email, :password, :phoneNumber, :nickname)";

SqlParameterSource param = new BeanPropertySqlParameterSource(postUserRequest);
KeyHolder keyHolder = new GeneratedKeyHolder();
Expand Down Expand Up @@ -73,7 +75,7 @@ public int modifyNickname(long userId, String nickname) {
}

public List<GetUserResponse> getUsers(String nickname, String email, String status) {
String sql = "select email, phone_number, nickname, profile_image, status from user " +
String sql = "select email, phone_number, nickname, status from user " +
"where nickname like :nickname and email like :email and status=:status";

Map<String, Object> param = Map.of(
Expand All @@ -86,7 +88,6 @@ public List<GetUserResponse> getUsers(String nickname, String email, String stat
rs.getString("email"),
rs.getString("phone_number"),
rs.getString("nickname"),
rs.getString("profile_image"),
rs.getString("status"))
);
}
Expand All @@ -103,4 +104,22 @@ public String getPasswordByUserId(long userId) {
return jdbcTemplate.queryForObject(sql, param, String.class);
}

public GetUserResponse getUserByUserId(long userId) {
String sql = "select email, phone_number, nickname, status from user " +
"where user_id=:user_id";
Map<String, Object> param = Map.of("user_id", userId);

return jdbcTemplate.queryForObject(sql, param, new BeanPropertyRowMapper<>(GetUserResponse.class));

}

public List<GetUserOrderHistoryResponse> getOrderByUserId(long userId) {
String sql = "select user.user_id, `order`.order_id, `order`.status, `order`.total from user "
+ "join `order` on user.user_id = `order`.user_id "
+ "where user.user_id=:user_id";

Map<String, Object> param = Map.of("user_id", userId);

return jdbcTemplate.query(sql, param, new BeanPropertyRowMapper<>(GetUserOrderHistoryResponse.class));
}
}
9 changes: 9 additions & 0 deletions src/main/java/kuit/server/dto/order/Custom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kuit.server.dto.order;

import lombok.Data;

@Data
public class Custom {
private String option;
private String[] userChoice;
}
14 changes: 14 additions & 0 deletions src/main/java/kuit/server/dto/order/GetOrderResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kuit.server.dto.order;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class GetOrderResponse {
private long orderId;
private String status;
private float total;
}
10 changes: 10 additions & 0 deletions src/main/java/kuit/server/dto/order/OrderDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kuit.server.dto.order;

import lombok.Data;

@Data
public class OrderDetail {
private long menuId;
private long count;
private Custom[] custom;
}
Loading