source: src/main/java/com/example/autopartz/service/impl/OrderServiceImpl.java

main
Last change on this file was 2a552fe, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 16 months ago

Added transactional methods to secure the database integrity and refactoring

  • Property mode set to 100644
File size: 1.5 KB
Line 
1package com.example.autopartz.service.impl;
2
3import com.example.autopartz.model.Client;
4import com.example.autopartz.model.Order;
5import com.example.autopartz.model.manytomany.OrderContainsPart;
6import com.example.autopartz.repository.OrderContainsPartRepository;
7import com.example.autopartz.repository.OrderRepository;
8import com.example.autopartz.service.OrderService;
9import org.springframework.stereotype.Service;
10
11import javax.transaction.Transactional;
12import java.util.List;
13@Service
14public 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}
Note: See TracBrowser for help on using the repository browser.