Coverage Summary for Class: CustomerServiceImpl (org.softuni.cardealer.service)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
CustomerServiceImpl | 100% (1/ 1) | 100% (5/ 5) | 100% (18/ 18) |
1 package org.softuni.cardealer.service;
2
3 import org.modelmapper.ModelMapper;
4 import org.softuni.cardealer.domain.entities.Customer;
5 import org.softuni.cardealer.domain.models.service.CustomerServiceModel;
6 import org.softuni.cardealer.repository.CustomerRepository;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service;
9
10 @Service
11 public class CustomerServiceImpl implements CustomerService {
12
13 private final CustomerRepository customerRepository;
14 private final ModelMapper modelMapper;
15
16 @Autowired
17 public CustomerServiceImpl(CustomerRepository customerRepository, ModelMapper modelMapper) {
18 this.customerRepository = customerRepository;
19 this.modelMapper = modelMapper;
20 }
21
22 @Override
23 public CustomerServiceModel saveCustomer(CustomerServiceModel customerServiceModel) {
24 Customer customer = this.modelMapper.map(customerServiceModel, Customer.class);
25 customer = this.customerRepository.saveAndFlush(customer);
26
27 return this.modelMapper.map(customer, CustomerServiceModel.class);
28
29 }
30
31 @Override
32 public CustomerServiceModel editCustomer(CustomerServiceModel customerServiceModel) {
33 Customer customer = this.customerRepository.findById(customerServiceModel.getId()).orElse(null);
34 customer.setName(customerServiceModel.getName());
35 customer.setBirthDate(customerServiceModel.getBirthDate());
36 customer.setYoungDriver(customerServiceModel.isYoungDriver());
37
38 customer = this.customerRepository.saveAndFlush(customer);
39
40 return this.modelMapper.map(customer, CustomerServiceModel.class);
41
42 }
43
44 @Override
45 public CustomerServiceModel deleteCustomer(String id) {
46 Customer customer = this.customerRepository.findById(id).orElse(null);
47 this.customerRepository.delete(customer);
48
49 return this.modelMapper.map(customer, CustomerServiceModel.class);
50 }
51
52 @Override
53 public CustomerServiceModel findCustomerById(String id) {
54 Customer customer = this.customerRepository.findById(id).orElse(null);
55
56 return this.modelMapper.map(customer, CustomerServiceModel.class);
57 }
58 }