71 lines
2.3 KiB
Java
71 lines
2.3 KiB
Java
package com.pmu.betengine.service;
|
|
|
|
import com.pmu.betengine.model.Reunion;
|
|
import com.pmu.betengine.model.dto.ReunionDTO;
|
|
import com.pmu.betengine.model.dto.ReunionRequestDTO;
|
|
import com.pmu.betengine.repository.ReunionRepository;
|
|
import org.modelmapper.ModelMapper;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
@Transactional
|
|
public class ReunionService {
|
|
|
|
private final ReunionRepository reunionRepository;
|
|
private final ModelMapper modelMapper;
|
|
|
|
public ReunionService(ReunionRepository reunionRepository, ModelMapper modelMapper) {
|
|
this.reunionRepository = reunionRepository;
|
|
this.modelMapper = modelMapper;
|
|
}
|
|
|
|
public List<ReunionDTO> getAllReunions() {
|
|
return reunionRepository.findAll()
|
|
.stream()
|
|
.map(this::convertToDTO)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
public ReunionDTO getReunionById(Long id) {
|
|
Reunion reunion = reunionRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("Reunion non trouvée"));
|
|
return convertToDTO(reunion);
|
|
}
|
|
|
|
public ReunionDTO createReunion(ReunionRequestDTO requestDTO) {
|
|
if (reunionRepository.existsByCode(requestDTO.getCode())) {
|
|
throw new RuntimeException("Code déjà existant");
|
|
}
|
|
|
|
Reunion reunion = convertToEntity(requestDTO);
|
|
reunion.setCreatedAt(LocalDateTime.now());
|
|
Reunion saved = reunionRepository.save(reunion);
|
|
return convertToDTO(saved);
|
|
}
|
|
|
|
public ReunionDTO updateReunion(Long id, ReunionRequestDTO requestDTO) {
|
|
Reunion existing = reunionRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("Reunion non trouvée"));
|
|
|
|
modelMapper.map(requestDTO, existing);
|
|
existing.setUpdatedAt(LocalDateTime.now());
|
|
return convertToDTO(reunionRepository.save(existing));
|
|
}
|
|
|
|
public void deleteReunion(Long id) {
|
|
reunionRepository.deleteById(id);
|
|
}
|
|
|
|
private ReunionDTO convertToDTO(Reunion reunion) {
|
|
return modelMapper.map(reunion, ReunionDTO.class);
|
|
}
|
|
|
|
private Reunion convertToEntity(ReunionRequestDTO dto) {
|
|
return modelMapper.map(dto, Reunion.class);
|
|
}
|
|
} |