This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package ru.soune.nocopy.service.tariff;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffDTO;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
import ru.soune.nocopy.entity.tarif.TariffType;
|
||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||
import ru.soune.nocopy.repository.TariffRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class TariffService {
|
||||
|
||||
private final TariffRepository tariffRepository;
|
||||
|
||||
public Tariff addTariff(String tariffName, double price, String type, int tokens, long diskSize, long maxUsers,
|
||||
int maxFilesCount) {
|
||||
Tariff tariff = new Tariff();
|
||||
tariff.setName(tariffName);
|
||||
tariff.setPrice(price);
|
||||
tariff.setType(type);
|
||||
tariff.setTokens(tokens);
|
||||
tariff.setDiskSize(diskSize);
|
||||
tariff.setMaxUsers(maxUsers);
|
||||
tariff.setMaxFilesCount(maxFilesCount);
|
||||
|
||||
return tariffRepository.save(tariff);
|
||||
}
|
||||
|
||||
public List<TariffDTO> getAllTariffs() {
|
||||
return tariffRepository.findAllByOrderByPriceAsc()
|
||||
.stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public TariffDTO getTariffById(Long id) {
|
||||
return tariffRepository.findById(id)
|
||||
.map(this::convertToDTO)
|
||||
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
|
||||
}
|
||||
|
||||
public TariffDTO getTariffByType(TariffType type) {
|
||||
return tariffRepository.findByType(type)
|
||||
.map(this::convertToDTO)
|
||||
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TariffDTO createTariff(TariffDTO tariffDTO) {
|
||||
Tariff tariff = new Tariff();
|
||||
updateTariffFromDTO(tariff, tariffDTO);
|
||||
|
||||
return convertToDTO(tariffRepository.save(tariff));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TariffDTO updateTariff(Long id, TariffDTO tariffDTO) {
|
||||
Tariff tariff = tariffRepository.findById(id)
|
||||
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
|
||||
updateTariffFromDTO(tariff, tariffDTO);
|
||||
|
||||
return convertToDTO(tariffRepository.save(tariff));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteTariff(Long id) {
|
||||
tariffRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private TariffDTO convertToDTO(Tariff tariff) {
|
||||
return new TariffDTO(
|
||||
tariff.getId(),
|
||||
tariff.getType(),
|
||||
tariff.getName(),
|
||||
tariff.getPrice(),
|
||||
tariff.getTokens(),
|
||||
tariff.getMaxFilesCount(),
|
||||
tariff.getDiskSize(),
|
||||
tariff.getMaxUsers()
|
||||
);
|
||||
}
|
||||
|
||||
private void updateTariffFromDTO(Tariff tariff, TariffDTO dto) {
|
||||
tariff.setType(dto.getType());
|
||||
tariff.setName(dto.getName());
|
||||
tariff.setPrice(dto.getPrice());
|
||||
tariff.setTokens(dto.getTokens());
|
||||
tariff.setMaxFilesCount(dto.getMaxFilesCount());
|
||||
tariff.setDiskSize(dto.getDiskSize());
|
||||
tariff.setMaxUsers(dto.getMaxUsers());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user