Coverage Summary for Class: PartServiceImpl (org.softuni.cardealer.service)

Class Class, % Method, % Line, %
PartServiceImpl 100% (1/ 1) 100% (5/ 5) 100% (17/ 17)


1 package org.softuni.cardealer.service; 2  3 import org.modelmapper.ModelMapper; 4 import org.softuni.cardealer.domain.entities.Part; 5 import org.softuni.cardealer.domain.models.service.PartServiceModel; 6 import org.softuni.cardealer.repository.PartRepository; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9  10 @Service 11 public class PartServiceImpl implements PartService { 12  13  private final PartRepository partRepository; 14  private final ModelMapper modelMapper; 15  16  @Autowired 17  public PartServiceImpl(PartRepository partRepository, ModelMapper modelMapper) { 18  this.partRepository = partRepository; 19  this.modelMapper = modelMapper; 20  } 21  22  @Override 23  public PartServiceModel savePart(PartServiceModel partServiceModel) { 24  25  Part part = this.modelMapper.map(partServiceModel, Part.class); 26  this.partRepository.saveAndFlush(part); 27  28  return this.modelMapper.map(part, PartServiceModel.class); 29  30  } 31  32  @Override 33  public PartServiceModel editPart(PartServiceModel partServiceModel) { 34  Part part = this.partRepository.findById(partServiceModel.getId()).orElse(null); 35  part.setName(partServiceModel.getName()); 36  part.setPrice(partServiceModel.getPrice()); 37  38  Part edited = this.partRepository.saveAndFlush(part); 39  40  return this.modelMapper.map(edited, PartServiceModel.class); 41  42  } 43  44  @Override 45  public PartServiceModel deletePart(String id) { 46  Part part = this.partRepository.findById(id).orElseThrow(); 47  48  this.partRepository.delete(part); 49  return this.modelMapper.map(part, PartServiceModel.class); 50  51  } 52  53  @Override 54  public PartServiceModel findPartById(String id) { 55  Part part = this.partRepository.findById(id).orElse(null); 56  57  return this.modelMapper.map(part, PartServiceModel.class); 58  59  } 60 }