Atualização do projeto
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package com.agendaestudantil.servico;
|
||||
|
||||
import com.agendaestudantil.dto.RespostaNotificacaoDTO;
|
||||
import com.agendaestudantil.entidade.Notificacao;
|
||||
import com.agendaestudantil.excecao.ExcecaoRecursoNaoEncontrado;
|
||||
import com.agendaestudantil.repositorio.NotificacaoRepositorio;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class NotificacaoServico {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NotificacaoServico.class);
|
||||
|
||||
private final NotificacaoRepositorio notificacaoRepositorio;
|
||||
|
||||
public NotificacaoServico(NotificacaoRepositorio notificacaoRepositorio) {
|
||||
this.notificacaoRepositorio = notificacaoRepositorio;
|
||||
}
|
||||
|
||||
private void validarAcesso(String estudanteId) {
|
||||
String authUser = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
if (!authUser.equals(estudanteId)) {
|
||||
log.error("Acesso negado. Usuário {} tentou acessar recurso do estudante {}", authUser, estudanteId);
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Acesso negado");
|
||||
}
|
||||
}
|
||||
|
||||
public List<RespostaNotificacaoDTO> listarNaoLidas(String estudanteId) {
|
||||
log.debug("Listando notificações não lidas para estudante: {}", estudanteId);
|
||||
validarAcesso(estudanteId);
|
||||
return notificacaoRepositorio.findByEstudanteIdAndLidaFalse(estudanteId).stream()
|
||||
.map(this::toDTO)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<RespostaNotificacaoDTO> listarTodas(String estudanteId) {
|
||||
log.debug("Listando todas as notificações para estudante: {}", estudanteId);
|
||||
validarAcesso(estudanteId);
|
||||
return notificacaoRepositorio.findByEstudanteId(estudanteId).stream()
|
||||
.map(this::toDTO)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public long contarNaoLidas(String estudanteId) {
|
||||
log.debug("Contando notificações não lidas para estudante: {}", estudanteId);
|
||||
validarAcesso(estudanteId);
|
||||
return notificacaoRepositorio.countByEstudanteIdAndLidaFalse(estudanteId);
|
||||
}
|
||||
|
||||
public RespostaNotificacaoDTO marcarComoLida(String id) {
|
||||
Notificacao notificacao = getNotificacaoEntity(id);
|
||||
validarAcesso(notificacao.getEstudanteId());
|
||||
notificacao.setLida(true);
|
||||
return toDTO(notificacaoRepositorio.save(notificacao));
|
||||
}
|
||||
|
||||
public void marcarTodasComoLidas(String estudanteId) {
|
||||
log.debug("Marcando todas as notificações como lidas para estudante: {}", estudanteId);
|
||||
validarAcesso(estudanteId);
|
||||
List<Notificacao> notificacoes = notificacaoRepositorio.findByEstudanteIdAndLidaFalse(estudanteId);
|
||||
for (Notificacao n : notificacoes) {
|
||||
n.setLida(true);
|
||||
notificacaoRepositorio.save(n);
|
||||
}
|
||||
}
|
||||
|
||||
public void excluirNotificacao(String id) {
|
||||
Notificacao notificacao = getNotificacaoEntity(id);
|
||||
validarAcesso(notificacao.getEstudanteId());
|
||||
notificacaoRepositorio.delete(notificacao);
|
||||
}
|
||||
|
||||
private Notificacao getNotificacaoEntity(String id) {
|
||||
return notificacaoRepositorio.findById(id)
|
||||
.orElseThrow(() -> new ExcecaoRecursoNaoEncontrado("Notificação não encontrada"));
|
||||
}
|
||||
|
||||
private RespostaNotificacaoDTO toDTO(Notificacao notificacao) {
|
||||
return new RespostaNotificacaoDTO(
|
||||
notificacao.getId(),
|
||||
notificacao.getTitulo(),
|
||||
notificacao.getMensagem(),
|
||||
notificacao.getTipo() != null ? notificacao.getTipo().name() : null,
|
||||
notificacao.getReferenciaId(),
|
||||
notificacao.getTipoReferencia() != null ? notificacao.getTipoReferencia().name() : null,
|
||||
notificacao.isLida(),
|
||||
notificacao.getDataGeracao()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user