olexander_dan

DataService

May 11th, 2020
1,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package com.alexd.AlexPharmacy.service;
  2.  
  3. import com.alexd.AlexPharmacy.domain.PharmacyDomain;
  4. import com.alexd.AlexPharmacy.exception.RecordExistException;
  5. import com.alexd.AlexPharmacy.exception.RecordNotFoundException;
  6. import org.apache.commons.lang3.math.NumberUtils;
  7. import org.jetbrains.annotations.NotNull;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.dao.DataIntegrityViolationException;
  10. import org.springframework.data.jpa.repository.JpaRepository;
  11. import org.springframework.stereotype.Service;
  12.  
  13. import java.util.List;
  14.  
  15. @Service
  16. public class DataService {
  17.  
  18.     /**
  19.      * Service method, which returns all presented records of certain repository.
  20.      *
  21.      * @param repository Repository with all needed records
  22.      * @return List of specified repository records presented in table
  23.      */
  24.     public List<? extends PharmacyDomain> getAllRecords(
  25.             @NotNull final JpaRepository<? extends PharmacyDomain, Long> repository) {
  26.         return repository.findAll();
  27.     }
  28.  
  29.     /**
  30.      * Service method, which finds record in repository specified by id basket record.
  31.      *
  32.      * @param repository Repository with all needed records
  33.      * @param id         ID of desired record
  34.      * @return Record founded by ID
  35.      */
  36.     public PharmacyDomain getRecordById(@NotNull final JpaRepository<? extends PharmacyDomain, Long> repository,
  37.                                         final String id) {
  38.         try {
  39.             var longId = NumberUtils.createLong(id);
  40.             var foundedRecord = repository.findById(longId);
  41.             return foundedRecord.orElseThrow(IllegalArgumentException::new);
  42.         } catch (IllegalArgumentException ex) {
  43.             throw new RecordNotFoundException(id);
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Service method, which saves given record into DB.
  49.      *
  50.      * @param repository Adding repository
  51.      * @param record     Saving record
  52.      */
  53.     public void saveRecord(@NotNull final JpaRepository repository, final PharmacyDomain record) {
  54.         try {
  55.             repository.save(record);
  56.         } catch (IllegalArgumentException | DataIntegrityViolationException ignored) {
  57.             throw new RecordExistException("Запис уже присутній у базі даних");
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Service method, which updates given manufacturer record.
  63.      *
  64.      * @param repository Updating repository
  65.      * @param updRecord  Updating record
  66.      */
  67.     public void updateRecord(@NotNull final JpaRepository repository, @NotNull final PharmacyDomain updRecord) {
  68.         var stringId = String.valueOf(updRecord.getId());
  69.         var recordFromDB = getRecordById(repository, stringId);
  70.         BeanUtils.copyProperties(updRecord, recordFromDB, "id");
  71.         repository.save(recordFromDB);
  72.     }
  73.  
  74.     /**
  75.      * Service method, which deletes record by ID.
  76.      *
  77.      * @param repository Deleting repository
  78.      * @param id         ID of deleting record
  79.      */
  80.     public void deleteRecord(final JpaRepository repository, final String id) {
  81.         var recordFromDB = getRecordById(repository, id);
  82.         repository.delete(recordFromDB);
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment