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

Swagger PR #11

Merged
merged 9 commits into from
Jan 16, 2025
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
2 changes: 1 addition & 1 deletion BackEnd/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
HELP.md
.gradle
build/
.idea/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
Expand All @@ -19,7 +20,6 @@ bin/

### IntelliJ IDEA ###
application.yml
.idea
*.iws
*.iml
*.ipr
Expand Down
1 change: 1 addition & 0 deletions BackEnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0' //Swagger
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling // 스케줄링 활성화
@EnableJpaAuditing
public class Nbe341Team05Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.team5.nbe341team05.common.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@OpenAPIDefinition(info = @Info(title = "API 서버", version = "v1"))
public class SwaggerConfig {
@Bean
public GroupedOpenApi openAPI() {
return GroupedOpenApi.builder()
.group("order")
.pathsToMatch("/order/**")
.build();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public class Menu extends BaseTime {

private int stock; // 재고 수량

private String image; // 이미지 URL 또는 경로

public void decreaseStock(int diff) {
this.stock -= diff;
}

public void increaseStock(int diff) {
this.stock += diff;
}

private String image; // 이미지 URL 또는 경로

private long views; // 메뉴 조회수
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
import com.team5.nbe341team05.domain.order.dto.OrderUpdateRequestDto;
import com.team5.nbe341team05.domain.order.entity.Order;
import com.team5.nbe341team05.domain.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Order", description = "Order API")
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class OrderController {
private final OrderService orderService;

@PostMapping
@Operation(summary = "주문 생성", description = "새로운 주문을 생성합니다")
@ApiResponse(responseCode = "201", description = "성공")
@ApiResponse(responseCode = "400", description = "실패")
public ResponseMessage<OrderResponseDto> createOrder(@RequestBody OrderDto orderDto) {
Order order = orderService.createOrder(orderDto);

Expand All @@ -30,6 +37,9 @@ public ResponseMessage<OrderResponseDto> createOrder(@RequestBody OrderDto order
}

@GetMapping("/{email}")
@Operation(summary = "주문 리스트 조회", description = "주문 리스트를 조회합니다")
@ApiResponse(responseCode = "200", description = "성공")
@ApiResponse(responseCode = "404", description = "실패")
public ResponseMessage<List<OrderResponseDto>> getOrdersByEmail(@PathVariable String email) {
List<OrderResponseDto> orders = orderService.getOrdersByEmail(email);
return new ResponseMessage<>(
Expand All @@ -40,6 +50,9 @@ public ResponseMessage<List<OrderResponseDto>> getOrdersByEmail(@PathVariable St
}

@GetMapping("/{email}/{id}")
@Operation(summary = "주문 상세 조회", description = "주문 상세 내역을 조회합니다")
@ApiResponse(responseCode = "200", description = "성공")
@ApiResponse(responseCode = "404", description = "실패")
public ResponseMessage<OrderResponseDto> getOrderDetails(@PathVariable String email, @PathVariable Long id) {
OrderResponseDto order = orderService.getOrderDetails(email, id);
return new ResponseMessage<>(
Expand All @@ -50,6 +63,9 @@ public ResponseMessage<OrderResponseDto> getOrderDetails(@PathVariable String em
}

@PutMapping("/{email}/{id}")
@Operation(summary = "주문 수정", description = "주문 내역을 수정합니다")
@ApiResponse(responseCode = "200", description = "성공")
@ApiResponse(responseCode = "404", description = "실패")
public ResponseMessage<OrderResponseDto> updateOrder(@PathVariable String email, @PathVariable Long id, @RequestBody OrderUpdateRequestDto updateRequestDto) {
OrderResponseDto updatedOrder = orderService.updateOrder(email, id, updateRequestDto);
return new ResponseMessage<>(
Expand All @@ -60,6 +76,9 @@ public ResponseMessage<OrderResponseDto> updateOrder(@PathVariable String email,
}

@DeleteMapping("/{email}/{id}")
@Operation(summary = "주문 취소", description = "주문을 취소합니다")
@ApiResponse(responseCode = "200", description = "성공")
@ApiResponse(responseCode = "404", description = "실패")
public ResponseMessage<Void> cancelOrder(@PathVariable String email, @PathVariable Long id) {
orderService.cancelOrder(email, id);
return new ResponseMessage<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public OrderResponseDto(Order order) {
this.id = order.getId();
this.email = order.getEmail();
this.address = order.getAddress();
this.order_time = order.getOrderTime();
this.order_time = order.getCreateDate();
this.totalPrice = order.getTotalPrice();
this.deliveryStatus = order.isDeliveryStatus();
this.omlist = order.getOrderMenus().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team5.nbe341team05.domain.order.entity;


import com.team5.nbe341team05.common.jpa.entity.BaseTime;
import com.team5.nbe341team05.domain.orderMenu.entity.OrderMenu;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -19,7 +20,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "\"order\"")
public class Order {
public class Order extends BaseTime {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,8 +30,6 @@ public class Order {
@NotBlank(message = "이메일은 필수 항목입니다.")
private String email; // 고객 이메일

private LocalDateTime orderTime; // 주문 시간

@NotBlank(message = "주소는 필수 항목입니다.")
private String address; // 고객 주소

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ public Order createOrder(OrderDto orderDto) {
for (CartMenuDto cartMenuDto : orderDto.getProducts()) {
Menu menu = menuRepository.findById(cartMenuDto.getMenuId()).orElseThrow(() -> new ServiceException("404","상품을 찾을 수 없습니다."));

if (menu.getStock() < cartMenuDto.getQuantity()) {
throw new ServiceException("400", "상품 재고가 부족합니다.");
}
menu.decreaseStock(cartMenuDto.getQuantity());
CartMenu cartMenu = new CartMenu(menu, cartMenuDto.getQuantity());
cart.addCartMenu(cartMenu);
}

LocalDateTime now = LocalDateTime.now();
boolean orderStatus = now.getHour() < 14;
boolean orderStatus = checkTime();

int totalPrice = 0;

Order order = Order.builder()
.email(orderDto.getEmail())
.address(orderDto.getAddress())
.orderTime(LocalDateTime.now())
.deliveryStatus(orderStatus)
.totalPrice(0)
.build();
Expand Down Expand Up @@ -98,17 +100,30 @@ public OrderResponseDto updateOrder(String email, Long id, OrderUpdateRequestDto
Order order = orderRepository.findByEmailAndId(email, id)
.orElseThrow(() -> new ServiceException("404", "해당 주문을 찾을 수 없습니다."));


int totalPrice = 0;

for (CartMenuDto cartMenuDto : updateRequestDto.getOmlist()) {
Menu menu = menuRepository.findById(cartMenuDto.getMenuId()).orElseThrow(() -> new ServiceException("404","상품을 찾을 수 없습니다."));
for (OrderMenu orderMenu : order.getOrderMenus()) {
Menu menu = orderMenu.getMenu();
int prevQuantity = orderMenu.getQuantity();
int newQuantity = updateRequestDto.getOmlist().stream()
.filter(cartMenuDto -> cartMenuDto.getMenuId().equals(menu.getId()))
.map(CartMenuDto::getQuantity)
.findFirst()
.orElse(prevQuantity);

int diff = newQuantity - prevQuantity;

if (diff > 0) {
if (menu.getStock() < diff) {
throw new ServiceException("400", "상품 재고가 부족합니다.");
}
menu.decreaseStock(diff);
} else {
menu.increaseStock(-diff);
}

int price = menu.getPrice();
int tPrice = price * cartMenuDto.getQuantity();

OrderMenu orderMenu = new OrderMenu(menu, cartMenuDto.getQuantity(), menu.getPrice());
order.addOrderMenu(orderMenu);
int tPrice = price * newQuantity;
totalPrice += tPrice;
}

Expand All @@ -123,6 +138,12 @@ public void cancelOrder(String email, Long id) {
Order order = orderRepository.findByEmailAndId(email, id)
.orElseThrow(() -> new ServiceException("404", "해당 주문을 찾을 수 없습니다."));

for (OrderMenu orderMenu : order.getOrderMenus()) {
Menu menu = orderMenu.getMenu();
int prevQuantity = orderMenu.getQuantity();
menu.increaseStock(prevQuantity);
}

orderRepository.delete(order);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void t1() throws Exception {
.andExpect(jsonPath("$.data.id").value(order.getId()))
.andExpect(jsonPath("$.data.email").value(order.getEmail()))
.andExpect(jsonPath("$.data.address").value(order.getAddress()))
.andExpect(jsonPath("$.data.order_time").value(order.getOrderTime()))
.andExpect(jsonPath("$.data.order_time").value(order.getCreateDate()))
.andExpect(jsonPath("$.data.totalPrice").value(order.getTotalPrice()))
.andExpect(jsonPath("$.data.deliveryStatus").value(order.isDeliveryStatus()))
.andExpect(jsonPath("$.data.omlist").value(order.getOrderMenus().stream()
Expand Down Expand Up @@ -249,7 +249,7 @@ void t8() throws Exception {
.andExpect(jsonPath("$.data.id").value(order.getId()))
.andExpect(jsonPath("$.data.email").value(order.getEmail()))
.andExpect(jsonPath("$.data.address").value(order.getAddress()))
.andExpect(jsonPath("$.data.order_time").value(order.getOrderTime()))
.andExpect(jsonPath("$.data.order_time").value(order.getCreateDate()))
.andExpect(jsonPath("$.data.totalPrice").value(order.getTotalPrice()))
.andExpect(jsonPath("$.data.deliveryStatus").value(order.isDeliveryStatus()))
.andExpect(jsonPath("$.data.omlist").value(order.getOrderMenus().stream()
Expand Down