Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
feat: add update function in controller
Browse files Browse the repository at this point in the history
add the update of status order function in OrderController

linked#27
  • Loading branch information
MeloLawson committed Jan 7, 2024
1 parent 0d9437f commit f13ba6e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import com.cashmanager.server.common.dto.CartDto;
import com.cashmanager.server.common.dto.OrderDto;
import com.cashmanager.server.common.dto.ProductDto;
import com.cashmanager.server.common.enumeration.OrderStatus;
import com.cashmanager.server.order.service.IOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("api/shop/orders")
@Tag(name = "API Orders")
public class OrderController {

private final IOrderService orderService;
Expand All @@ -25,6 +29,12 @@ public OrderController(IOrderService orderService) {
}

@PostMapping("")
@Operation(summary = "After the inventory check, create a new order ", description = "Add a new order")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully created"),
@ApiResponse(responseCode = "400", description = "One or several products are unavailable, or no order received"),
@ApiResponse(responseCode = "503", description = "Error accessing the database, please retry later")
})
public ResponseEntity<?> createOrder(@RequestBody CartDto cartDto){
Map<Integer, ProductDto> orderedProducts = cartDto.getListOrderedProducts();
if(orderedProducts != null && !orderedProducts.isEmpty()){
Expand All @@ -37,15 +47,31 @@ public ResponseEntity<?> createOrder(@RequestBody CartDto cartDto){
return new ResponseEntity<>(optOrder.get(), HttpStatus.CREATED);
}
else{
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE);
}
}
else {
return new ResponseEntity<>("This order cannot be completed because certain products are unavailable :\n"+optMessage.get(),HttpStatus.NO_CONTENT);
return new ResponseEntity<>("This order cannot be completed because certain products are unavailable :\n"+optMessage.get(),HttpStatus.BAD_REQUEST);
}
}
else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>("No order received",HttpStatus.BAD_REQUEST);
}
}

@PostMapping("/{orderId}")
@Operation(summary = "Update a pre-existing status order ", description = "Update an order")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully updated"),
@ApiResponse(responseCode = "404", description = "The order with this id was not found"),
})
public ResponseEntity<?> updateOrderStatus(@PathVariable UUID orderId, @RequestBody OrderStatus status ){
boolean isUpdated = this.orderService.updateOrderStatus(orderId,status);
if(isUpdated){
return new ResponseEntity<>(HttpStatus.OK);
}
else{
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.cashmanager.server.common.dto.CartDto;
import com.cashmanager.server.common.dto.OrderDto;
import com.cashmanager.server.common.dto.ProductDto;
import com.cashmanager.server.common.enumeration.OrderStatus;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public interface IOrderService {
/**
Expand All @@ -20,4 +22,12 @@ public interface IOrderService {
* @return the new order
*/
Optional<OrderDto> createOrder(CartDto cartDto);

/**
* Update the order status in the database
* @param orderId : identification of the order
* @param status : new status of the order
* @return true if the update is successful
*/
boolean updateOrderStatus(UUID orderId, OrderStatus status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

@Service
public class OrderService implements IOrderService {
Expand Down Expand Up @@ -91,4 +92,15 @@ public Optional<OrderDto> createOrder(CartDto cartDto) {
}
return Optional.empty();
}

@Override
public boolean updateOrderStatus(UUID orderId, OrderStatus status) {
Optional<Order> optOrder = orderRepository.findById(orderId);
if(optOrder.isPresent()){
optOrder.get().setStatus(status.toString());
orderRepository.save(optOrder.get());
return true;
}
return false;
}
}

0 comments on commit f13ba6e

Please sign in to comment.