1 | package com.example.autopartz.service.impl;
|
---|
2 |
|
---|
3 | import com.example.autopartz.model.Client;
|
---|
4 | import com.example.autopartz.model.Order;
|
---|
5 | import com.example.autopartz.model.manytomany.OrderContainsPart;
|
---|
6 | import com.example.autopartz.repository.OrderContainsPartRepository;
|
---|
7 | import com.example.autopartz.repository.OrderRepository;
|
---|
8 | import com.example.autopartz.service.OrderService;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import javax.transaction.Transactional;
|
---|
12 | import java.util.List;
|
---|
13 | @Service
|
---|
14 | public class OrderServiceImpl implements OrderService {
|
---|
15 | private final OrderRepository orderRepository;
|
---|
16 | private final OrderContainsPartRepository orderContainsPartRepository;
|
---|
17 |
|
---|
18 | public OrderServiceImpl(OrderRepository orderRepository, OrderContainsPartRepository orderContainsPartRepository) {
|
---|
19 | this.orderRepository = orderRepository;
|
---|
20 | this.orderContainsPartRepository = orderContainsPartRepository;
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public List<Order> findAll() {
|
---|
25 | return orderRepository.findAll();
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | @Transactional
|
---|
30 | public Order createOrderAndAddPartToOrder(Client user, Integer partId, Integer quantity) {
|
---|
31 | Order order = orderRepository.save(new Order(user));
|
---|
32 | orderContainsPartRepository.save(new OrderContainsPart(partId, order.getOrderid(), quantity));
|
---|
33 | return order;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public Order findById(Integer id) {
|
---|
38 | return orderRepository.findById(id).get();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void save(Order o) {
|
---|
43 | orderRepository.save(o);
|
---|
44 | }
|
---|
45 | }
|
---|