Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class PictureServiceImpl implements PictureService {
- private final PictureRepository pictureRepository;
- private final ModelMapper modelMapper;
- private final Gson gson;
- private final ValidationUtil validationUtil;
- private final CarService carService;
- public PictureServiceImpl(PictureRepository pictureRepository, ModelMapper modelMapper, Gson gson, ValidationUtil validationUtil, CarService carService) {
- this.pictureRepository = pictureRepository;
- this.modelMapper = modelMapper;
- this.gson = gson;
- this.validationUtil = validationUtil;
- this.carService = carService;
- }
- @Override
- public boolean areImported() {
- return this.pictureRepository.count()>0;
- }
- @Override
- public String readPicturesFromFile() throws IOException {
- return Files.readString(Path.of(PICTURES_FILE_PATH));
- }
- @Override
- public String importPictures() throws IOException {
- StringBuilder sb = new StringBuilder();
- PictureSeedDto[] pictureSeedDtos = this.gson
- .fromJson(new FileReader(PICTURES_FILE_PATH), PictureSeedDto[].class);
- Arrays.stream(pictureSeedDtos)
- .forEach(pictureSeedDto -> {
- if (this.validationUtil.isValid(pictureSeedDto)){
- if (this.pictureRepository.findByName(pictureSeedDto.getName())==null){
- Picture picture = this.modelMapper
- .map(pictureSeedDto,Picture.class);
- Car car =this.carService.findById(pictureSeedDto.getCar());
- picture.setCar(car);
- this.pictureRepository.saveAndFlush(picture);
- sb.append(String.format("Successfully imported picture - %s\n",
- picture.getName()));
- }else {
- sb.append("Already in DB");
- }
- }else {
- sb.append("Invalid picture");
- }
- sb.append(System.lineSeparator());
- });
- return sb.toString();
- }
- @Override
- public Set<Picture> getAllPicturesByCarId(int id) {
- return this.pictureRepository.getAllByCarId(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement