-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f21335
commit 1a25f1a
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/github/nhatoriginal/spring/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/nhatoriginal/spring/dto/order/OrderDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/github/nhatoriginal/spring/repository/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/nhatoriginal/spring/service/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |