Skip to content

Commit

Permalink
feat : adds endpoint to create order
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jun 17, 2024
1 parent d6379c4 commit d351233
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.retailstore.webapp.clients.order;

import jakarta.validation.constraints.NotBlank;
import java.io.Serializable;

public record Address(
@NotBlank(message = "AddressLine1 is required") String addressLine1,
String addressLine2,
@NotBlank(message = "City is required") String city,
@NotBlank(message = "State is required") String state,
@NotBlank(message = "ZipCode is required") String zipCode,
@NotBlank(message = "Country is required") String country)
implements Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.retailstore.webapp.clients.order;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import java.io.Serializable;
import java.util.List;

public record CreateOrderRequest(
@NotEmpty(message = "Items cannot be empty.") List<OrderItemRequest> items,
@Positive Long customerId,
@Valid Address deliveryAddress)
implements Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.retailstore.webapp.clients.order;

public record OrderConfirmationDTO(String orderNumber, OrderStatus status) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.retailstore.webapp.clients.order;

import java.math.BigDecimal;

public record OrderItemRequest(String productCode, int quantity, BigDecimal productPrice) {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.retailstore.webapp.clients.order;

import com.example.retailstore.webapp.clients.PagedResult;
import jakarta.validation.Valid;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.PostExchange;

public interface OrderServiceClient {

Expand All @@ -13,4 +15,7 @@ public interface OrderServiceClient {

@GetExchange("/api/orders/{id}")
OrderResponse getOrder(@RequestHeader Map<String, ?> headers, @PathVariable String id);

@PostExchange("/api/orders")
OrderConfirmationDTO createOrder(Map<String, ?> headers, @Valid CreateOrderRequest orderRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.retailstore.webapp.clients.order;

public enum OrderStatus {
NEW,
IN_PROCESS,
DELIVERED,
CANCELLED,
ERROR
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.example.retailstore.webapp.web.controller;

import com.example.retailstore.webapp.clients.PagedResult;
import com.example.retailstore.webapp.clients.order.CreateOrderRequest;
import com.example.retailstore.webapp.clients.order.OrderConfirmationDTO;
import com.example.retailstore.webapp.clients.order.OrderResponse;
import com.example.retailstore.webapp.clients.order.OrderServiceClient;
import com.example.retailstore.webapp.services.SecurityHelper;
import jakarta.validation.Valid;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
Expand Down Expand Up @@ -60,4 +65,11 @@ PagedResult<OrderResponse> getOrders() {
String accessToken = securityHelper.getAccessToken();
return Map.of("Authorization", "Bearer " + accessToken);
}

@PostMapping("/api/orders")
@ResponseBody
OrderConfirmationDTO createOrder(@Valid @RequestBody CreateOrderRequest orderRequest) {
log.info("Creating order: {}", orderRequest);
return orderServiceClient.createOrder(getHeaders(), orderRequest);
}
}

0 comments on commit d351233

Please sign in to comment.