This commit is contained in:
@@ -107,7 +107,7 @@ public class TariffHandler implements RequestHandler {
|
||||
Map.of("id", tariffRequest.getId()));
|
||||
}
|
||||
|
||||
TariffDTO tariffDTO = tariffService.updateTariff(tariffRequest.getId(), tariff);
|
||||
TariffDTO tariffDTO = tariffService.updateTariff(tariffRequest.getId(), tariff, tariffRequest);
|
||||
|
||||
TariffResponse tariffResponse = TariffResponse.builder()
|
||||
.tariffName(tariffDTO.getName())
|
||||
|
||||
@@ -4,6 +4,7 @@ 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.dto.tarriff.TariffRequest;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
import ru.soune.nocopy.entity.tarif.TariffTimeTerm;
|
||||
import ru.soune.nocopy.entity.tarif.TariffType;
|
||||
@@ -81,18 +82,10 @@ public class TariffService {
|
||||
}
|
||||
|
||||
@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) {
|
||||
public TariffDTO updateTariff(Long id, TariffDTO tariffDTO, TariffRequest tariffRequest) {
|
||||
Tariff tariff = tariffRepository.findById(id)
|
||||
.orElseThrow(() -> new TariffNotFoundException("Tariff not found"));
|
||||
updateTariffFromDTO(tariff, tariffDTO);
|
||||
updateTariffFromDTO(tariff, tariffDTO, tariffRequest);
|
||||
|
||||
return convertToDTO(tariffRepository.save(tariff));
|
||||
}
|
||||
@@ -119,15 +112,17 @@ public class TariffService {
|
||||
);
|
||||
}
|
||||
|
||||
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());
|
||||
tariff.setDescription(dto.getDescription());
|
||||
tariff.setTariffTerm(TariffTimeTerm.valueOf(dto.getTariffTerm().toUpperCase()));
|
||||
private void updateTariffFromDTO(Tariff tariff, TariffDTO dto, TariffRequest tariffRequest) {
|
||||
tariff.setType(tariffRequest.getType() == null ? dto.getType(): tariffRequest.getType());
|
||||
tariff.setName(tariffRequest.getName() == null ? dto.getName(): tariffRequest.getName());
|
||||
tariff.setPrice(tariffRequest.getPrice() == 0 ? dto.getPrice(): tariffRequest.getPrice());
|
||||
tariff.setTokens(tariffRequest.getTokens() == 0 ? dto.getTokens(): tariffRequest.getTokens());
|
||||
tariff.setMaxFilesCount(tariffRequest.getMaxFilesCount() == 0 ? dto.getMaxFilesCount():
|
||||
tariffRequest.getMaxFilesCount());
|
||||
tariff.setDiskSize(tariffRequest.getDiskSize() == 0 ? dto.getDiskSize(): tariffRequest.getDiskSize());
|
||||
tariff.setMaxUsers(tariffRequest.getMaxUsers() == 0 ? dto.getMaxUsers(): tariffRequest.getMaxUsers());
|
||||
tariff.setDescription(tariffRequest.getDescription() == null ? dto.getDescription(): tariffRequest.getDescription());
|
||||
tariff.setTariffTerm(tariffRequest.getTariffTerm() == null ? TariffTimeTerm.valueOf(dto.getTariffTerm().toUpperCase()):
|
||||
TariffTimeTerm.valueOf(tariffRequest.getTariffTerm()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user