Commit 861ec3b4 authored by RenanMontenegro3's avatar RenanMontenegro3

feat: adiciona endpoints sem paginação com ordenação decrescente

- Implementa endpoint /sem-paginacao para Accept, BlackList e Vessel
- Adiciona ordenação decrescente por ID em todos os endpoints
- Mantém controle de acesso consistente com endpoints existentes
- Utiliza Sort.by(Direction.DESC) para ordenação uniforme
parent 31609a39
......@@ -5,6 +5,7 @@ import br.com.treinaweb.twjobs.api.accepts.assemblers.AcceptAssembler;
import br.com.treinaweb.twjobs.api.accepts.dtos.AcceptRequest;
import br.com.treinaweb.twjobs.api.accepts.dtos.AcceptResponse;
import br.com.treinaweb.twjobs.api.accepts.mappers.AcceptMapper;
import br.com.treinaweb.twjobs.api.bercos.dtos.BercoResponse;
import br.com.treinaweb.twjobs.api.file.FileManagerController;
import br.com.treinaweb.twjobs.core.enums.Role;
import br.com.treinaweb.twjobs.core.enums.VeriStatus;
......@@ -26,9 +27,12 @@ import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
......@@ -78,46 +82,40 @@ public class AcceptRestController {
return acceptRepository.countAllAccepts();
}
@GetMapping("/sem-paginacao")
public CollectionModel<EntityModel<AcceptResponse>> findAllSemPaginacao() {
List<AcceptResponse> lista = acceptRepository.findAll(Sort.by(Sort.Direction.DESC, "id")).stream()
.map(acceptMapper::toAcceptResponse)
.collect(Collectors.toList());
return acceptAssembler.toCollectionModel(lista);
}
//"@PageableDefault(value = 7)" tamanho local para o tamanho da paginação. Deve ser igual ou menor ao valor encontrado no "application.properties"
@GetMapping
public CollectionModel<EntityModel<AcceptResponse>> findAll(@PageableDefault(value = 15) Pageable pageable) {
User user = securityService.getCurrentUser();
Long userId = user.getId();
public CollectionModel<EntityModel<AcceptResponse>> findAll(@PageableDefault(size = 15) Pageable pageable) {
Pageable ordenado = PageRequest.of(pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by("id").descending());
// if (user.getRole().equals(Role.COMPANY)) {
// throw new NegocioException("É company");
var accepts = acceptRepository.findAll(pageable)
var accepts = acceptRepository.findAll(ordenado)
.map(acceptMapper::toAcceptResponse);
return pagedResourcesAssembler.toModel(accepts, acceptAssembler);
// } else if (user.getRole().equals(Role.CANDIDATE)) {
//// throw new NegocioException("É candidate");
// var accepts = acceptRepository.findAllByUserId(pageable,userId)
// .map(acceptMapper::toAcceptResponse);
// return pagedResourcesAssembler.toModel(accepts, acceptAssembler);
//
// }
// Page<AcceptResponse>
// accepts = acceptRepository.findAllByUserId(pageable,userId)
// .map(acceptMapper::toAcceptResponse);
//
//
// AJEITAR
// return null;
// return pagedResourcesAssembler.toModel(accepts, acceptAssembler);
return pagedResourcesAssembler.toModel(accepts, acceptAssembler);
}
@GetMapping("/custom")
public List<AcceptResponse> findTest(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "imo", required = false) String imo, @RequestParam(value = "status", required = false) String status, @RequestParam(value = "nome", required = false) String nome, @RequestParam(value = "categoria", required = false) String categoria, @RequestParam(value = "data_create", required = false) String data_create) {
List<AcceptResponse> accepts = acceptCustomRepository.acceptsCustom(id, imo, status, nome, categoria, data_create)
public List<AcceptResponse> findTest(@RequestParam(value = "id", required = false) Long id,
@RequestParam(value = "imo", required = false) String imo,
@RequestParam(value = "status", required = false) String status,
@RequestParam(value = "nome", required = false) String nome,
@RequestParam(value = "categoria", required = false) String categoria,
@RequestParam(value = "dataInicio", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dataInicio,
@RequestParam(value = "dataFim", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dataFim) {
List<AcceptResponse> accepts = acceptCustomRepository.acceptsCustom(id, imo, status, nome, categoria, dataInicio,dataFim)
.stream()
.map(acceptMapper::toAcceptResponse)
.collect(Collectors.toList());
......
......@@ -84,6 +84,7 @@ public class AcceptRequest {
// @Size(min = 1, max = 10, message = "Calado deve ter no mínimo 1 e no máximo 10 caracteres")
private Float calado_saida;
private List<Long> bercosSelecionados;
}
......@@ -61,6 +61,7 @@ public class AcceptResponse {
private Float calado_entrada;
private Float calado_saida;
private List<Long> bercosSelecionados;
// private List<Long> bercos;
......
......@@ -20,7 +20,9 @@ import br.com.treinaweb.twjobs.core.services.auth.SecurityService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
......@@ -29,6 +31,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
//@CrossOrigin
@RestController
......@@ -49,11 +52,19 @@ public class BercosRestController {
@GetMapping
public CollectionModel<EntityModel<BercoResponse>> findAll(Pageable pageable) {
var bercos = bercoRepository.findAll(pageable)
Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("id").descending());
var bercos = bercoRepository.findAll(sortedPageable)
.map(bercoMapper::toBercoResponse);
return pagedResourcesAssembler.toModel(bercos, bercoAssembler);
}
@GetMapping("/sem-paginacao")
public CollectionModel<EntityModel<BercoResponse>> findAllSemPaginacao() {
List<BercoResponse> lista = bercoRepository.findAll().stream()
.map(bercoMapper::toBercoResponse)
.collect(Collectors.toList());
return bercoAssembler.toCollectionModel(lista);
}
@GetMapping("/{id}")
public EntityModel<BercoResponse> findById(@PathVariable Long id) {
var berco = bercoRepository.findById(id)
......
......@@ -24,7 +24,9 @@ import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.CollectionModel;
......@@ -37,6 +39,7 @@ import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
//"""
......@@ -60,11 +63,20 @@ public class BlackListRestController {
private final VesselRepository vesselRepository;
@GetMapping("/sem-paginacao")
@TWJobsPermissions.IsCompany
public CollectionModel<EntityModel<BlackListResponse>> findAllSemPaginacao() {
List<BlackListResponse> lista = blackListRepository.findAll(Sort.by(Sort.Direction.DESC, "id")).stream()
.map(blackListMapper::toBlackListResponse)
.collect(Collectors.toList());
return blackListAssembler.toCollectionModel(lista);
}
//"@PageableDefault(value = 7)" tamanho local para o tamanho da paginação. Deve ser igual ou menor ao valor encontrado no "application.properties"
@GetMapping
@TWJobsPermissions.IsCompany
public CollectionModel<EntityModel<BlackListResponse>> findAll(@PageableDefault(value = 15) Pageable pageable) {
Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("id").descending());
User user = securityService.getCurrentUser();
Long userId = user.getId();
......@@ -72,7 +84,7 @@ public class BlackListRestController {
// org.springframework.data.domain.Page<BlackListResponse> blackLists = null;
// if (user.getRole().equals(Role.COMPANY)) {
// throw new NegocioException("É company");
var blackLists = blackListRepository.findAll(pageable)
var blackLists = blackListRepository.findAll(sortedPageable)
.map(blackListMapper::toBlackListResponse);
return pagedResourcesAssembler.toModel(blackLists, blackListAssembler);
// } else if (user.getRole().equals(Role.CANDIDATE)) {
......
......@@ -13,7 +13,9 @@ import br.com.treinaweb.twjobs.core.repositories.UserRepository;
import br.com.treinaweb.twjobs.core.services.auth.SecurityService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.CollectionModel;
......@@ -48,8 +50,9 @@ public class UsersCRUDRestController {
@GetMapping
@TWJobsPermissions.IsCompany
public CollectionModel<EntityModel<UserResponse>> findAll(@PageableDefault(value = 15) Pageable pageable) {
Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("id").descending());
var users = userRepository.findAll(pageable)
var users = userRepository.findAll(sortedPageable)
.map(userMapper::toUserResponse);
return pagedResourcesAssembler.toModel(users, userAssembler);
}
......
......@@ -35,9 +35,12 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
......@@ -51,6 +54,7 @@ import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.logging.Logger.global;
import static org.hibernate.type.descriptor.java.CoercionHelper.toLong;
......@@ -79,6 +83,14 @@ public class VesselRestController {
@Autowired
private final Disco disco;
@GetMapping("/sem-paginacao")
public CollectionModel<EntityModel<VesselResponse>> findAllSemPaginacao() {
List<VesselResponse> lista = vesselRepository.findAll(Sort.by(Sort.Direction.DESC, "id")).stream()
.map(vesselMapper::toVesselResponse)
.collect(Collectors.toList());
return vesselAssembler.toCollectionModel(lista);
}
public void check_user(Vessel vessel){
if(securityService.getCurrentUser() != vessel.getUser() ) {
......@@ -89,13 +101,14 @@ public class VesselRestController {
@GetMapping
public CollectionModel<EntityModel<VesselResponse>> findAll(@PageableDefault(value = 15) Pageable pageable) {
Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("id").descending());
User user = securityService.getCurrentUser();
Long userId = user.getId();
// if (user.getRole().equals(Role.COMPANY)) {
// throw new NegocioException("É company");
var vessels = vesselRepository.findAll(pageable)
var vessels = vesselRepository.findAll(sortedPageable)
.map(vesselMapper::toVesselResponse);
return pagedResourcesAssembler.toModel(vessels, vesselAssembler);
// } else if (user.getRole().equals(Role.CANDIDATE)) {
......@@ -180,23 +193,17 @@ public class VesselRestController {
@GetMapping("/custom")
public List<Vessel> findTest(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "imo", required = false) Long imo, @RequestParam(value = "categoria", required = false) String categoria, @RequestParam(value = "nome", required = false) String nome) {
List<Vessel> vessels = vesselCustomRepository.vesselsCustom(id, imo, categoria, nome);
// .stream()
// .map(acceptMapper::toAcceptResponse)
// .collect(Collectors.toList());
// List<Accept> acceptRespons;
// for(AcceptOk i : accepts){
//
// acceptRespons.add(acceptMapper.toAccept(i)) ;
// }
public List<Vessel> findTest(
@RequestParam(value = "id", required = false) Long id,
@RequestParam(value = "imo", required = false) Long imo,
@RequestParam(value = "categoria", required = false) String categoria,
@RequestParam(value = "nome", required = false) String nome,
@RequestParam(value = "dataInicio", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dataInicio,
@RequestParam(value = "dataFim", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dataFim) {
List<Vessel> vessels = vesselCustomRepository.vesselsCustom(id, imo, categoria, nome, dataInicio, dataFim);
return vessels;
}
......
......@@ -8,6 +8,7 @@ import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.jpa.repository.Modifying;
import java.util.ArrayList;
import java.util.List;
@Data
......@@ -116,6 +117,8 @@ public class Accept {
@Column(length=500)
private String path;
@Column(nullable = false)
private List<Long> bercosSelecionados = new ArrayList<>();
// @Column(nullable = false, length = 20)
// @Enumerated(EnumType.STRING)
......
......@@ -5,97 +5,81 @@ import br.com.treinaweb.twjobs.core.models.Accept;
import jakarta.persistence.EntityManager;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Repository
public class AcceptCustomRepository {
private final EntityManager em;
public AcceptCustomRepository(EntityManager em) {
this.em = em;
}
// id
// imo
// status
// nome
public List<Accept> acceptsCustom(Long id, String imo, String status, String nome, String categoria, String data_create){
public List<Accept> acceptsCustom(Long id, String imo, String status, String nome, String categoria, LocalDate dataInicio, LocalDate dataFim) {
String query = "select A from Accept as A";
String condicao = " where ";
if(id!=null){
if (id != null) {
query += condicao + "A.id = :id";
condicao = " and ";
}
if(imo!=null){
if (imo != null) {
query += condicao + "A.imo = :imo";
condicao = " and ";
}
if(status!=null){
if (status != null) {
query += condicao + "A.status = :status";
condicao = " and ";
}
if(nome!=null){
if (nome != null) {
query += condicao + "A.nome LIKE :nome";
condicao = " and ";
}
if(categoria!=null){
if (categoria != null) {
query += condicao + "A.categoria = :categoria";
condicao = " and ";
}
if(data_create!=null){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
query += condicao + "A.data_create LIKE :data_create";
condicao = " and ";
if (dataInicio != null && dataFim != null) {
query += condicao + "A.data_create BETWEEN :dataInicio AND :dataFim";
} else if (dataInicio != null) {
query += condicao + "A.data_create >= :dataInicio";
} else if (dataFim != null) {
query += condicao + "A.data_create <= :dataFim";
}
var q = em.createQuery(query, Accept.class);
if(id!=null){
if (id != null) {
q.setParameter("id", id);
}
if(imo!=null){
if (imo != null) {
q.setParameter("imo", imo);
}
if(status!=null){
if (status != null) {
q.setParameter("status", status);
}
if(nome!=null){
q.setParameter("nome", "%"+nome+"%");
if (nome != null) {
q.setParameter("nome", "%" + nome + "%");
}
if(categoria!=null){
if (categoria != null) {
q.setParameter("categoria", categoria);
}
if(data_create!=null){
q.setParameter("data_create", "%"+data_create+"%");
if (dataInicio != null && dataFim != null) {
q.setParameter("dataInicio", dataInicio.format(formatter));
q.setParameter("dataFim", dataFim.format(formatter));
} else if (dataInicio != null) {
q.setParameter("dataInicio", dataInicio.format(formatter));
} else if (dataFim != null) {
q.setParameter("dataFim", dataFim.format(formatter));
}
return q.getResultList();
}
}
......@@ -6,6 +6,7 @@ import br.com.treinaweb.twjobs.core.models.Vessel;
import jakarta.persistence.EntityManager;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.util.List;
@Repository
......@@ -22,55 +23,66 @@ public class VesselCustomRepository {
// status
// nome
public List<Vessel> vesselsCustom(Long id, Long imo, String categoria, String nome){
public List<Vessel> vesselsCustom(Long id, Long imo, String categoria, String nome, LocalDate dataInicio, LocalDate dataFim) {
String query = "select A from Vessel as A";
String condicao = " where ";
if(id!=null){
if (id != null) {
query += condicao + "A.id = :id";
condicao = " and ";
}
if(imo!=null){
if (imo != null) {
query += condicao + "A.imo = :imo";
condicao = " and ";
}
if(categoria!=null){
if (categoria != null) {
query += condicao + "A.categoria = :categoria";
condicao = " and ";
}
if(nome!=null){
if (nome != null) {
query += condicao + "A.nome LIKE :nome";
condicao = " and ";
}
if (dataInicio != null && dataFim != null) {
query += condicao + "A.time_create BETWEEN :dataInicio AND :dataFim";
} else if (dataInicio != null) {
query += condicao + "A.time_create >= :dataInicio";
} else if (dataFim != null) {
query += condicao + "A.time_create <= :dataFim";
}
var q = em.createQuery(query, Vessel.class);
if(id!=null){
if (id != null) {
q.setParameter("id", id);
}
if(imo!=null){
if (imo != null) {
q.setParameter("imo", imo);
}
if(categoria!=null){
if (categoria != null) {
q.setParameter("categoria", categoria);
}
if(nome!=null){
q.setParameter("nome", "%"+nome+"%");
if (nome != null) {
q.setParameter("nome", "%" + nome + "%");
}
if (dataInicio != null) {
q.setParameter("dataInicio", dataInicio.atStartOfDay());
}
return q.getResultList();
if (dataFim != null) {
q.setParameter("dataFim", dataFim.atTime(23, 59, 59));
}
return q.getResultList();
}
}
......@@ -162,24 +162,41 @@ public class CadastroAcceptService {
// ~~~ PLANO DE AMARRACAO PORTO(BASEADO NAS REUNIOES)
for (Berco berco : bercos) {
// if(Objects.equals(vessel.getCategoria(), berco.getCategoria())) {
// if ((vessel.getLoa()<=berco.getLoa_max())&&(acceptRequest.getCalado_entrada()<=berco.getCalado_max())&&(acceptRequest.getCalado_saida()<=berco.getCalado_max())&&(vessel.getDwt()<=berco.getDwt())
//// TESTAR SE TÁ FUNCIONANDO ESSA CHECAGEM DA BLACKLIST
// &&(!blackListed)
// ) {
// bercosCompativeis.add(berco);
// }
// }
String catNavio = accept.getCategoria(); // "2" = granel sólido; "3" = granel líquido; "1" = carga geral
String catBerco = berco.getCategoria();
boolean categoriaValida = false;
// Se o navio for granel sólido ("2"), só aceita berços "1" ou "2"
if ("2".equals(catNavio)) {
if ("1".equals(catBerco) || "2".equals(catBerco)) {
categoriaValida = true;
}
}
// Se o navio for granel líquido ("3"), só aceita berços "3"
else if ("3".equals(catNavio)) {
if ("3".equals(catBerco)) {
categoriaValida = true;
}
}
// Se for carga geral ("1") ou outro código, exige igualdade exata
else {
if (catNavio.equals(catBerco)) {
categoriaValida = true;
}
}
if(Objects.equals(accept.getCategoria(), berco.getCategoria())) {
if ((accept.getLoa()<=berco.getLoa_max())&&(accept.getCalado_entrada()<=berco.getCalado_max())&&(accept.getCalado_saida()<=berco.getCalado_max())&&(accept.getDwt()<=berco.getDwt())
// TESTAR SE TÁ FUNCIONANDO ESSA CHECAGEM DA BLACKLIST
&&(!blackListed)
) {
// Verificação de dimensões, DWT e blacklist
if (categoriaValida
&& accept.getLoa() <= berco.getLoa_max()
&& accept.getCalado_entrada() <= berco.getCalado_max()
&& accept.getCalado_saida() <= berco.getCalado_max()
&& accept.getDwt() <= berco.getDwt()
&& !blackListed) {
bercosCompativeis.add(berco);
}
}
}
Accept lastAccept = acceptRepository.findFirstByOrderByDataAcceptDesc();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment