93 lines
3.5 KiB
Java
93 lines
3.5 KiB
Java
package ru.oa2.mvp.nutriapi.service;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import ru.oa2.mvp.nutriapi.dto.*;
|
|
import ru.oa2.mvp.nutriapi.entity.Supplement;
|
|
import ru.oa2.mvp.nutriapi.exception.ResourceNotFoundException;
|
|
import ru.oa2.mvp.nutriapi.repository.SupplementRepository;
|
|
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
public class SupplementService {
|
|
|
|
private final SupplementRepository supplementRepository;
|
|
private final SupplementMapper supplementMapper;
|
|
|
|
@Transactional(readOnly = true)
|
|
public PageResponse<SupplementResponse> findAll(Pageable pageable) {
|
|
Page<Supplement> page = supplementRepository.findAll(pageable);
|
|
return toPageResponse(page);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public SupplementResponse findById(UUID id) {
|
|
Supplement supplement = supplementRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Supplement", id));
|
|
return supplementMapper.toResponse(supplement);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public PageResponse<SupplementResponse> findByCategory(String category, Pageable pageable) {
|
|
Page<Supplement> page = supplementRepository.findByCategory(category, pageable);
|
|
return toPageResponse(page);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public PageResponse<SupplementResponse> findByBrand(String brand, Pageable pageable) {
|
|
Page<Supplement> page = supplementRepository.findByBrand(brand, pageable);
|
|
return toPageResponse(page);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public PageResponse<SupplementResponse> search(String query, Pageable pageable) {
|
|
Page<Supplement> page = supplementRepository.fullTextSearch(query, pageable);
|
|
return toPageResponse(page);
|
|
}
|
|
|
|
@Transactional
|
|
public SupplementResponse create(SupplementCreateRequest request) {
|
|
Supplement supplement = supplementMapper.toEntity(request);
|
|
supplement = supplementRepository.save(supplement);
|
|
log.info("Created supplement: id={}, name={}", supplement.getId(), supplement.getName());
|
|
return supplementMapper.toResponse(supplement);
|
|
}
|
|
|
|
@Transactional
|
|
public SupplementResponse update(UUID id, SupplementUpdateRequest request) {
|
|
Supplement supplement = supplementRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Supplement", id));
|
|
supplementMapper.updateEntity(supplement, request);
|
|
supplement = supplementRepository.save(supplement);
|
|
log.info("Updated supplement: id={}", id);
|
|
return supplementMapper.toResponse(supplement);
|
|
}
|
|
|
|
@Transactional
|
|
public void delete(UUID id) {
|
|
if (!supplementRepository.existsById(id)) {
|
|
throw new ResourceNotFoundException("Supplement", id);
|
|
}
|
|
supplementRepository.deleteById(id);
|
|
log.info("Deleted supplement: id={}", id);
|
|
}
|
|
|
|
private PageResponse<SupplementResponse> toPageResponse(Page<Supplement> page) {
|
|
return new PageResponse<>(
|
|
page.getContent().stream().map(supplementMapper::toResponse).toList(),
|
|
page.getNumber(),
|
|
page.getSize(),
|
|
page.getTotalElements(),
|
|
page.getTotalPages(),
|
|
page.isLast()
|
|
);
|
|
}
|
|
}
|