Skip to content

Commit

Permalink
feat: added order module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhat-Original committed May 13, 2024
1 parent 8f21335 commit 1a25f1a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static final class User {
public static final String BASE = "/user";
public static final String GET_ALL = "";
public static final String GET_ONE = "/{id}";

}

public static final class Cart {
Expand All @@ -38,4 +37,11 @@ public static final class Review {
public static final class Payment {
public static final String BASE = "/payment/user/{id}";
}

public static final class Order {
public static final String BASE = "/order";
public static final String CREATE = "";
public static final String GET_ALL = "/user/{userId}";
public static final String GET_ONE = "/{id}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.nhatoriginal.spring.controller;

import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
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;

import com.github.nhatoriginal.spring.constant.Endpoint;
import com.github.nhatoriginal.spring.dto.order.OrderDto;
import com.github.nhatoriginal.spring.service.OrderService;

@RestController
@RequestMapping(Endpoint.Order.BASE)
public class OrderController {
@Autowired
private OrderService orderService;

@GetMapping(Endpoint.Order.GET_ALL)
public List<OrderDto> findByUserId(@PathVariable UUID userId) {
return orderService.findByUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.nhatoriginal.spring.dto.order;

import java.time.LocalDate;
import java.util.UUID;

import com.github.nhatoriginal.spring.model.DeliveryStatus;
import com.github.nhatoriginal.spring.model.PaymentMethod;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class OrderDto {
public UUID id;
public LocalDate orderDate;
public long totalPrice;
public String note;
public PaymentMethod paymentMethod;
public DeliveryStatus deliveryStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.nhatoriginal.spring.repository;

import org.springframework.stereotype.Repository;
import com.github.nhatoriginal.spring.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

@Repository
public interface OrderRepository extends JpaRepository<Order, UUID> {
List<Order> findByUserId(UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.nhatoriginal.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.github.nhatoriginal.spring.dto.order.OrderDto;
import com.github.nhatoriginal.spring.repository.OrderRepository;
import com.github.nhatoriginal.spring.repository.UserRepository;

import java.util.List;
import java.util.UUID;
import org.springframework.http.HttpStatus;

@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;

@Autowired
private UserRepository userRepository;

public List<OrderDto> findByUserId(UUID userId) {
userRepository.findById(userId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));

return orderRepository.findByUserId(userId).stream().map(
order -> new OrderDto(order.getId(), order.getOrderDate(), order.getTotalPrice(), order.getNote(),
order.getPaymentMethod(), order.getDeliveryStatus()))
.toList();
}
}

0 comments on commit 1a25f1a

Please sign in to comment.