Advertisement
Guest User

PicsImpl

a guest
Mar 31st, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. @Service
  2. public class PictureServiceImpl implements PictureService {
  3.  
  4.  
  5. private final PictureRepository pictureRepository;
  6. private final ModelMapper modelMapper;
  7. private final Gson gson;
  8. private final ValidationUtil validationUtil;
  9. private final CarService carService;
  10.  
  11. public PictureServiceImpl(PictureRepository pictureRepository, ModelMapper modelMapper, Gson gson, ValidationUtil validationUtil, CarService carService) {
  12. this.pictureRepository = pictureRepository;
  13. this.modelMapper = modelMapper;
  14. this.gson = gson;
  15. this.validationUtil = validationUtil;
  16. this.carService = carService;
  17. }
  18.  
  19.  
  20. @Override
  21. public boolean areImported() {
  22. return this.pictureRepository.count()>0;
  23. }
  24.  
  25. @Override
  26. public String readPicturesFromFile() throws IOException {
  27. return Files.readString(Path.of(PICTURES_FILE_PATH));
  28. }
  29.  
  30. @Override
  31. public String importPictures() throws IOException {
  32. StringBuilder sb = new StringBuilder();
  33.  
  34.  
  35. PictureSeedDto[] pictureSeedDtos = this.gson
  36. .fromJson(new FileReader(PICTURES_FILE_PATH), PictureSeedDto[].class);
  37.  
  38. Arrays.stream(pictureSeedDtos)
  39. .forEach(pictureSeedDto -> {
  40. if (this.validationUtil.isValid(pictureSeedDto)){
  41. if (this.pictureRepository.findByName(pictureSeedDto.getName())==null){
  42. Picture picture = this.modelMapper
  43. .map(pictureSeedDto,Picture.class);
  44.  
  45. Car car =this.carService.findById(pictureSeedDto.getCar());
  46.  
  47. picture.setCar(car);
  48.  
  49. this.pictureRepository.saveAndFlush(picture);
  50.  
  51. sb.append(String.format("Successfully imported picture - %s\n",
  52. picture.getName()));
  53.  
  54. }else {
  55. sb.append("Already in DB");
  56. }
  57. }else {
  58. sb.append("Invalid picture");
  59. }
  60. sb.append(System.lineSeparator());
  61. });
  62.  
  63.  
  64. return sb.toString();
  65. }
  66.  
  67.  
  68.  
  69. @Override
  70. public Set<Picture> getAllPicturesByCarId(int id) {
  71.  
  72. return this.pictureRepository.getAllByCarId(id);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement