70%
This commit is contained in:
@@ -1,79 +1,69 @@
|
||||
package com.pmu.betengine.service;
|
||||
|
||||
import com.pmu.betengine.model.Agent;
|
||||
import com.pmu.betengine.model.TPE;
|
||||
import com.pmu.betengine.model.dto.TPEDTO;
|
||||
import com.pmu.betengine.model.dto.TPERequestDTO;
|
||||
import com.pmu.betengine.model.statut.StatutTPE;
|
||||
import com.pmu.betengine.repository.AgentRepository;
|
||||
import com.pmu.betengine.repository.TPERepository;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class TPEService {
|
||||
|
||||
private final TPERepository tpeRepository;
|
||||
private final ModelMapper modelMapper;
|
||||
private final AgentRepository agentRepository;
|
||||
|
||||
public TPEService(TPERepository tpeRepository, ModelMapper modelMapper) {
|
||||
public TPEService(TPERepository tpeRepository, AgentRepository agentRepository) {
|
||||
this.tpeRepository = tpeRepository;
|
||||
this.modelMapper = modelMapper;
|
||||
this.agentRepository = agentRepository;
|
||||
}
|
||||
|
||||
public List<TPEDTO> getAllTPEs() {
|
||||
return tpeRepository.findAll()
|
||||
.stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
public List<TPE> getAllTPEs() {
|
||||
return tpeRepository.findAll();
|
||||
}
|
||||
|
||||
public TPEDTO getTPEById(Long id) {
|
||||
TPE tpe = tpeRepository.findById(id)
|
||||
public TPE getTPEById(Long id) {
|
||||
return tpeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
||||
return convertToDTO(tpe);
|
||||
}
|
||||
|
||||
public TPEDTO createTPE(TPERequestDTO requestDTO) {
|
||||
// Vérifier l'unicité de l'IMEI
|
||||
if (tpeRepository.existsByImei(requestDTO.getImei())) {
|
||||
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + requestDTO.getImei());
|
||||
public TPE createTPE(TPE tpe) {
|
||||
if (tpeRepository.existsByImei(tpe.getImei())) {
|
||||
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + tpe.getImei());
|
||||
}
|
||||
|
||||
// Vérifier l'unicité du numéro de série
|
||||
if (tpeRepository.existsBySerial(requestDTO.getSerial())) {
|
||||
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + requestDTO.getSerial());
|
||||
if (tpeRepository.existsBySerial(tpe.getSerial())) {
|
||||
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + tpe.getSerial());
|
||||
}
|
||||
|
||||
TPE tpe = convertToEntity(requestDTO);
|
||||
TPE saved = tpeRepository.save(tpe);
|
||||
return convertToDTO(saved);
|
||||
return tpeRepository.save(tpe);
|
||||
}
|
||||
|
||||
public TPEDTO updateTPE(Long id, TPERequestDTO requestDTO) {
|
||||
public TPE updateTPE(Long id, TPE tpeDetails) {
|
||||
TPE existing = tpeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
||||
|
||||
// Vérifier l'unicité de l'IMEI si modifié
|
||||
if (!existing.getImei().equals(requestDTO.getImei()) &&
|
||||
tpeRepository.existsByImei(requestDTO.getImei())) {
|
||||
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + requestDTO.getImei());
|
||||
if (!existing.getImei().equals(tpeDetails.getImei()) &&
|
||||
tpeRepository.existsByImei(tpeDetails.getImei())) {
|
||||
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + tpeDetails.getImei());
|
||||
}
|
||||
|
||||
// Vérifier l'unicité du numéro de série si modifié
|
||||
if (!existing.getSerial().equals(requestDTO.getSerial()) &&
|
||||
tpeRepository.existsBySerial(requestDTO.getSerial())) {
|
||||
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + requestDTO.getSerial());
|
||||
if (!existing.getSerial().equals(tpeDetails.getSerial()) &&
|
||||
tpeRepository.existsBySerial(tpeDetails.getSerial())) {
|
||||
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + tpeDetails.getSerial());
|
||||
}
|
||||
|
||||
// Mise à jour des champs
|
||||
modelMapper.map(requestDTO, existing);
|
||||
// Update fields
|
||||
existing.setImei(tpeDetails.getImei());
|
||||
existing.setSerial(tpeDetails.getSerial());
|
||||
existing.setType(tpeDetails.getType());
|
||||
existing.setMarque(tpeDetails.getMarque());
|
||||
existing.setModele(tpeDetails.getModele());
|
||||
existing.setStatut(tpeDetails.getStatut());
|
||||
|
||||
return convertToDTO(tpeRepository.save(existing));
|
||||
return tpeRepository.save(existing);
|
||||
}
|
||||
|
||||
public void deleteTPE(Long id) {
|
||||
@@ -83,69 +73,44 @@ public class TPEService {
|
||||
tpeRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public TPEDTO updateStatut(Long id, StatutTPE statut) {
|
||||
TPE tpe = tpeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
||||
|
||||
public TPE updateStatut(Long id, StatutTPE statut) {
|
||||
TPE tpe = getTPEById(id);
|
||||
tpe.setStatut(statut);
|
||||
|
||||
// Si le statut est AFFECTE, mettre à jour le champ assigne
|
||||
if (statut == StatutTPE.AFFECTE) {
|
||||
tpe.setAssigne(true);
|
||||
} else if (statut == StatutTPE.DISPONIBLE) {
|
||||
tpe.setAssigne(false);
|
||||
}
|
||||
|
||||
return convertToDTO(tpeRepository.save(tpe));
|
||||
tpe.setAssigne(statut == StatutTPE.AFFECTE);
|
||||
if (statut == StatutTPE.DISPONIBLE) tpe.setAssigne(false);
|
||||
return tpeRepository.save(tpe);
|
||||
}
|
||||
|
||||
public TPEDTO assignerTPE(Long id) {
|
||||
TPE tpe = tpeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
||||
|
||||
if (tpe.isAssigne()) {
|
||||
throw new RuntimeException("Le TPE est déjà assigné");
|
||||
}
|
||||
|
||||
public TPE assignerTPE(Long tpeId, Long agentId) {
|
||||
TPE tpe = getTPEById(tpeId);
|
||||
Agent agent = agentRepository.findById(agentId)
|
||||
.orElseThrow(() -> new RuntimeException("Agent not found"));
|
||||
tpe.setAgent(agent);
|
||||
tpe.setAssigne(true);
|
||||
tpe.setStatut(StatutTPE.AFFECTE);
|
||||
|
||||
return convertToDTO(tpeRepository.save(tpe));
|
||||
return tpeRepository.save(tpe);
|
||||
}
|
||||
|
||||
public TPEDTO libererTPE(Long id) {
|
||||
TPE tpe = tpeRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
||||
|
||||
public TPE libererTPE(Long id) {
|
||||
TPE tpe = getTPEById(id);
|
||||
if (!tpe.isAssigne()) {
|
||||
throw new RuntimeException("Le TPE n'est pas assigné");
|
||||
}
|
||||
|
||||
tpe.setAssigne(false);
|
||||
tpe.setStatut(StatutTPE.DISPONIBLE);
|
||||
|
||||
return convertToDTO(tpeRepository.save(tpe));
|
||||
tpe.setAgent(null);
|
||||
return tpeRepository.save(tpe);
|
||||
}
|
||||
|
||||
public List<TPEDTO> getTPEsByStatut(StatutTPE statut) {
|
||||
return tpeRepository.findByStatut(statut)
|
||||
.stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
public List<TPE> getTPEsByStatut(StatutTPE statut) {
|
||||
return tpeRepository.findByStatut(statut);
|
||||
}
|
||||
|
||||
public List<TPEDTO> getTPEsDisponibles() {
|
||||
return tpeRepository.findAvailableTPE()
|
||||
.stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
public List<TPE> getTPEsDisponibles() {
|
||||
return tpeRepository.findAvailableTPE();
|
||||
}
|
||||
|
||||
public List<TPEDTO> searchTPEs(String searchTerm) {
|
||||
return tpeRepository.searchByTerm(searchTerm)
|
||||
.stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
public List<TPE> searchTPEs(String searchTerm) {
|
||||
return tpeRepository.searchByTerm(searchTerm);
|
||||
}
|
||||
|
||||
public long countTPEsByStatut(StatutTPE statut) {
|
||||
@@ -156,22 +121,4 @@ public class TPEService {
|
||||
return tpeRepository.countByAssigne(true);
|
||||
}
|
||||
|
||||
private TPEDTO convertToDTO(TPE tpe) {
|
||||
TPEDTO dto = modelMapper.map(tpe, TPEDTO.class);
|
||||
|
||||
// Formatage des dates
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
|
||||
if (tpe.getCreatedAt() != null) {
|
||||
dto.setCreatedAt(tpe.getCreatedAt().format(formatter));
|
||||
}
|
||||
if (tpe.getUpdatedAt() != null) {
|
||||
dto.setUpdatedAt(tpe.getUpdatedAt().format(formatter));
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private TPE convertToEntity(TPERequestDTO dto) {
|
||||
return modelMapper.map(dto, TPE.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user