Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.zenfulcode.commercify.api.auth;

import com.zenfulcode.commercify.api.auth.dto.request.LoginRequest;
import com.zenfulcode.commercify.api.auth.dto.request.RefreshTokenRequest;
import com.zenfulcode.commercify.api.auth.dto.request.RegisterRequest;
import com.zenfulcode.commercify.api.auth.dto.response.AuthResponse;
import com.zenfulcode.commercify.auth.application.service.AuthenticationApplicationService;
import com.zenfulcode.commercify.auth.application.service.AuthenticationResult;
import com.zenfulcode.commercify.shared.interfaces.ApiResponse;
import com.zenfulcode.commercify.user.application.service.UserApplicationService;
import lombok.RequiredArgsConstructor;
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;

@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
public class AuthController {

private final AuthenticationApplicationService authService;
private final UserApplicationService userService;

@PostMapping("/login")
public ResponseEntity<ApiResponse<AuthResponse>> login(
@RequestBody LoginRequest request) {

AuthenticationResult result = authService.authenticate(
request.username(),
request.password()
);

AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}

@PostMapping("/register")
public ResponseEntity<ApiResponse<AuthResponse>> register(
@RequestBody RegisterRequest request) {

userService.registerUser(
request.firstName(),
request.lastName(),
request.email(),
request.password(),
request.phone()
);

// Authenticate the newly registered user
AuthenticationResult result = authService.authenticate(
request.email(),
request.password()
);

AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}

@PostMapping("/refresh")
public ResponseEntity<ApiResponse<AuthResponse>> refreshToken(
@RequestBody RefreshTokenRequest request) {

AuthenticationResult result = authService.refreshToken(request.refreshToken());
AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zenfulcode.commercify.api.auth.dto.request;

public record LoginRequest(
String username,
String password
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.zenfulcode.commercify.api.auth.dto.request;

public record RefreshTokenRequest(
String refreshToken
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zenfulcode.commercify.api.auth.dto.request;

public record RegisterRequest(
String firstName,
String lastName,
String email,
String password,
String phone
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.zenfulcode.commercify.api.auth.dto.response;

import com.zenfulcode.commercify.auth.application.service.AuthenticationResult;
import com.zenfulcode.commercify.auth.domain.model.UserRole;

import java.util.Set;
import java.util.stream.Collectors;

public record AuthResponse(
String accessToken,
String refreshToken,
String tokenType,
String userId,
String username,
String email,
Set<String> roles
) {
public static AuthResponse from(AuthenticationResult result) {
Set<String> roles = result.user().getRoles().stream()
.map(UserRole::name)
.collect(Collectors.toSet());

return new AuthResponse(
result.accessToken(),
result.refreshToken(),
"Bearer",
result.user().getUserId(),
result.user().getUsername(),
result.user().getEmail(),
roles
);
}
}
108 changes: 108 additions & 0 deletions src/main/java/com/zenfulcode/commercify/api/order/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.zenfulcode.commercify.api.order;

import com.zenfulcode.commercify.api.order.dto.request.CreateOrderRequest;
import com.zenfulcode.commercify.api.order.dto.request.UpdateOrderStatusRequest;
import com.zenfulcode.commercify.api.order.dto.response.CreateOrderResponse;
import com.zenfulcode.commercify.api.order.dto.response.OrderDetailsResponse;
import com.zenfulcode.commercify.api.order.dto.response.PagedOrderResponse;
import com.zenfulcode.commercify.api.order.mapper.OrderDtoMapper;
import com.zenfulcode.commercify.order.application.command.CancelOrderCommand;
import com.zenfulcode.commercify.order.application.command.CreateOrderCommand;
import com.zenfulcode.commercify.order.application.command.UpdateOrderStatusCommand;
import com.zenfulcode.commercify.order.application.dto.OrderDetailsDTO;
import com.zenfulcode.commercify.order.application.query.FindAllOrdersQuery;
import com.zenfulcode.commercify.order.application.query.FindOrdersByUserIdQuery;
import com.zenfulcode.commercify.order.application.service.OrderApplicationService;
import com.zenfulcode.commercify.order.domain.model.Order;
import com.zenfulcode.commercify.order.domain.model.OrderStatus;
import com.zenfulcode.commercify.order.domain.valueobject.OrderId;
import com.zenfulcode.commercify.shared.interfaces.ApiResponse;
import com.zenfulcode.commercify.user.domain.valueobject.UserId;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/orders")
@RequiredArgsConstructor
public class OrderController {
private final OrderApplicationService orderApplicationService;
private final OrderDtoMapper orderDtoMapper;

@PostMapping
public ResponseEntity<ApiResponse<CreateOrderResponse>> createOrder(
@RequestBody CreateOrderRequest request) {
CreateOrderCommand command = orderDtoMapper.toCommand(request);
OrderId orderId = orderApplicationService.createOrder(command);

CreateOrderResponse response = new CreateOrderResponse(
orderId.toString(),
"Order created successfully"
);

return ResponseEntity.ok(ApiResponse.success(response));
}

@GetMapping("/{orderId}")
public ResponseEntity<ApiResponse<OrderDetailsResponse>> getOrder(
@PathVariable String orderId) {
OrderDetailsDTO order = orderApplicationService.getOrderById(OrderId.of(orderId));
OrderDetailsResponse response = orderDtoMapper.toResponse(order);
return ResponseEntity.ok(ApiResponse.success(response));
}

@GetMapping("/user/{userId}")
@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id or hasRole('ADMIN')")
public ResponseEntity<ApiResponse<PagedOrderResponse>> getOrdersByUserId(
@PathVariable String userId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {

FindOrdersByUserIdQuery query = new FindOrdersByUserIdQuery(
UserId.of(userId),
PageRequest.of(page, size)
);

Page<Order> orders = orderApplicationService.findOrdersByUserId(query);
PagedOrderResponse response = orderDtoMapper.toPagedResponse(orders);
return ResponseEntity.ok(ApiResponse.success(response));
}

@GetMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ApiResponse<PagedOrderResponse>> getAllOrders(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {

FindAllOrdersQuery query = new FindAllOrdersQuery(PageRequest.of(page, size));
Page<Order> orders = orderApplicationService.findAllOrders(query);
PagedOrderResponse response = orderDtoMapper.toPagedResponse(orders);
return ResponseEntity.ok(ApiResponse.success(response));
}

@PutMapping("/{orderId}/status")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ApiResponse<String>> updateOrderStatus(
@PathVariable String orderId,
@RequestBody UpdateOrderStatusRequest request) {

UpdateOrderStatusCommand command = new UpdateOrderStatusCommand(
OrderId.of(orderId),
OrderStatus.valueOf(request.status())
);

orderApplicationService.updateOrderStatus(command);
return ResponseEntity.ok(ApiResponse.success("Order status updated successfully"));
}

@DeleteMapping("/{orderId}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ApiResponse<String>> cancelOrder(@PathVariable String orderId) {
CancelOrderCommand command = new CancelOrderCommand(OrderId.of(orderId));
orderApplicationService.cancelOrder(command);
return ResponseEntity.ok(ApiResponse.success("Order cancelled successfully"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zenfulcode.commercify.api.order.dto.request;

public record AddressRequest(
String street,
String city,
String state,
String zipCode,
String country
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zenfulcode.commercify.api.order.dto.request;


public record CreateOrderLineRequest(
String productId,
String variantId,
int quantity
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.zenfulcode.commercify.api.order.dto.request;

import java.util.List;

public record CreateOrderRequest(
String userId,
String currency,
CustomerDetailsRequest customerDetails,
AddressRequest shippingAddress,
AddressRequest billingAddress,
List<CreateOrderLineRequest> orderLines
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zenfulcode.commercify.api.order.dto.request;

public record CustomerDetailsRequest(
String firstName,
String lastName,
String email,
String phone
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.zenfulcode.commercify.api.order.dto.request;

public record UpdateOrderStatusRequest(
String status
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zenfulcode.commercify.api.order.dto.response;

public record AddressResponse(
String street,
String city,
String state,
String zipCode,
String country
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zenfulcode.commercify.api.order.dto.response;

public record CreateOrderResponse(
String id,
String message
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zenfulcode.commercify.api.order.dto.response;

public record CustomerDetailsResponse(
String firstName,
String lastName,
String email,
String phone
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zenfulcode.commercify.api.order.dto.response;

public record MoneyResponse(
double amount,
String currency
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.zenfulcode.commercify.api.order.dto.response;

import java.time.Instant;
import java.util.List;

public record OrderDetailsResponse(
String id,
String userId,
String status,
String currency,
MoneyResponse totalAmount,
List<OrderLineResponse> orderLines,
CustomerDetailsResponse customerDetails,
AddressResponse shippingAddress,
AddressResponse billingAddress,
Instant createdAt
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.zenfulcode.commercify.api.order.dto.response;

public record OrderLineResponse(
String id,
String productId,
String variantId,
int quantity,
MoneyResponse unitPrice,
MoneyResponse total
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.zenfulcode.commercify.api.order.dto.response;

import java.time.Instant;

public record OrderSummaryResponse(
String id,
String userId,
String status,
MoneyResponse totalAmount,
Instant createdAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zenfulcode.commercify.api.order.dto.response;

import com.zenfulcode.commercify.api.product.dto.response.PageInfo;

import java.util.List;

public record PagedOrderResponse(
List<OrderSummaryResponse> items,
PageInfo pageInfo
) {}
Loading