/
linto
/
opensearch-example
Обзор
Документация
Войти
/
linto
/
opensearch-example
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/example/demo/service/impl/SubjectServiceImpl.java
70 строк
2 KB
linto
init
30 июл 2025, 11:44
30 июл 2025, 11:44
dc5aaf3
Код
Авторство
О чём код?
package com.example.demo.service.impl; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.demo.dto.SubjectDto; import com.example.demo.model.Subject; import com.example.demo.repository.SubjectRepository; import com.example.demo.service.SubjectService; import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor public class SubjectServiceImpl implements SubjectService { private final SubjectRepository subjectRepository; @Transactional public UUID create(SubjectDto subject) throws SubjectAlreadyExistsException { if (subject.getId() != null && subjectRepository.existsById(subject.getId())) { throw new SubjectAlreadyExistsException("Subject with id=[" + subject.getId() + "] already exists"); } Subject entity = subjectRepository.save(toEntity(subject)); return entity.getId(); } @Transactional public void delete(UUID id) throws SubjectNotFoundException { Subject entity = subjectRepository.findById(id).orElseThrow(SubjectNotFoundException::new); entity.setDeleted(1); } public List<SubjectDto> findAll() { return subjectRepository.findAll().stream().map(this::toDto).collect(Collectors.toList()); } public SubjectDto findById(UUID id) throws SubjectNotFoundException { return subjectRepository.findById(id).map(this::toDto).orElseThrow(SubjectNotFoundException::new); } private SubjectDto toDto(Subject entity) { return SubjectDto.builder() .id(entity.getId()) .name(entity.getName()) .inn(entity.getInn()) .kpp(entity.getKpp()) .ogrn(entity.getOgrn()) .createdat(entity.getCreatedat()) .updatedat(entity.getUpdatedat()) .deleted(entity.getDeleted()) .build(); } private Subject toEntity(SubjectDto subject) { return Subject.builder() .id(subject.getId() == null ? UUID.randomUUID() : subject.getId()) .inn(subject.getInn()) .kpp(subject.getKpp()) .name(subject.getName()) .ogrn(subject.getOgrn()) .build(); } }