Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudController;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.dto.CultureDto;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudService;

Expand All @@ -32,7 +31,7 @@ protected CrudService<Culture, Long> getService() {
protected ModelMapper getModelMapper() {
return this.modelMapper;
}

@GetMapping("/findName/{name}")
public ResponseEntity<CultureDto> findByName(@PathVariable String name){
Culture entity = cultureService.findByName(name);
Expand All @@ -43,4 +42,5 @@ public ResponseEntity<CultureDto> findByName(@PathVariable String name){
return ResponseEntity.noContent().build();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.edu.utfpr.ProjetoIDRAPI.entity.culture;

import br.edu.utfpr.ProjetoIDRAPI.enums.CultureType;
import lombok.Data;


@Data
public class CultureFilter {
private String cultureName; // filtro por nome
private CultureType cultureType; // filtro por tipo

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudService;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.Culture;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface CultureService extends CrudService<Culture, Long> {
Culture findByName(String name);
Page<Culture> search(CultureFilter filter, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import br.edu.utfpr.ProjetoIDRAPI.entity.culture.Culture;
import br.edu.utfpr.ProjetoIDRAPI.enums.CultureType;
import jakarta.persistence.Column;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.Data;

@Data
Expand All @@ -18,6 +21,8 @@ public Culture toCulture() {
return cult;
}

@Column(name = "culture_type")
@Enumerated(EnumType.STRING)
private CultureType cultureType;

private float ms;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import br.edu.utfpr.ProjetoIDRAPI.entity.crud.impl.CrudServiceImpl;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.Culture;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.CultureFilter;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.CultureRepository;
import br.edu.utfpr.ProjetoIDRAPI.entity.culture.CultureService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Service;
Expand All @@ -21,6 +25,25 @@ public Culture findByName(String name) {
return cultureRepository.findByCultureName(name);
}

@Override
public Page<Culture> search(CultureFilter filter, Pageable pageable) {
Specification<Culture> spec = Specification.where(null);

if (filter.getCultureName() != null && !filter.getCultureName().isEmpty()) {
spec = spec.and((root, query, cb) ->
cb.like(cb.lower(root.get("cultureName")), "%" + filter.getCultureName().toLowerCase() + "%")
);
}

if (filter.getCultureType() != null) {
spec = spec.and((root, query, cb) ->
cb.equal(root.get("cultureType"), filter.getCultureType())
);
}

return cultureRepository.findAll(spec, pageable);
}

@Override
protected JpaRepository<Culture, Long> getRepository() {
return this.cultureRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility;

import java.math.BigInteger;
import java.time.LocalDate;
import java.math.BigDecimal;

import br.edu.utfpr.ProjetoIDRAPI.entity.property.Property;
import jakarta.persistence.*;
Expand All @@ -18,32 +18,32 @@
@AllArgsConstructor
public class ForageDisponibility {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private LocalDate date;
private String forage;
private Float entry;
private Float residue;
private Float kg;
private Float picketArea;
private Float efficiency;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private LocalDate date;

private String forage;
private Double averageCost;
private Long usefulLife;
private String growthCycle;
private String observation;
private String ownershipType;
private Double entry;
private Double residue;
private Double kg;
private Double picketArea;
private Double efficiency;

@Column(precision = 20, scale = 0)
private java.math.BigDecimal numCows;
private BigDecimal numCows;

private Float kgCows;
private Double kgCows;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "property_id")
private Property property;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "property_id")
private Property property;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudController;
import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudService;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageCreateDto;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageDisponibilityDto;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageUpdateDto;
import jakarta.validation.Valid;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -17,6 +20,8 @@
public class ForageDisponibilityController
extends CrudController<ForageDisponibility, ForageDisponibilityDto, Long> {

private static final Logger log = LoggerFactory.getLogger(ForageDisponibilityController.class);

private final ForageDisponibilityService forageService;
private final ModelMapper modelMapper;

Expand All @@ -36,16 +41,45 @@ protected ModelMapper getModelMapper() {
return this.modelMapper;
}

/**
* Endpoint para buscar todas as ForageDisponibility de uma propriedade
* @param propertyId ID da propriedade
* @return Lista de ForageDisponibilityDto compatível com o frontend
*/
@Override
@RequestMapping(method = RequestMethod.POST, value = "/_ignored_")
@Deprecated
public ResponseEntity<Long> create(ForageDisponibilityDto dto) {
throw new UnsupportedOperationException("O endpoint de criação de foragem usa o método customizado 'createForageForProperty'.");
}

@GetMapping
public ResponseEntity<List<ForageDisponibilityDto>> getByProperty(@PathVariable Long propertyId) {
List<ForageDisponibilityDto> dtos = forageService.findByPropertyId(propertyId);
return ResponseEntity.ok(dtos);
}

@PostMapping // Endpoint: POST /properties/{propertyId}/forages
public ResponseEntity<ForageDisponibilityDto> createForageForProperty(
@PathVariable Long propertyId,
@RequestBody @Valid ForageCreateDto createDto) {
try {
ForageDisponibility createdForage = forageService.createForage(propertyId, createDto);
ForageDisponibilityDto responseDto = modelMapper.map(createdForage, ForageDisponibilityDto.class);
return new ResponseEntity<>(responseDto, HttpStatus.CREATED);
} catch (Exception e) {
log.error("Erro na criação da Foragem: {}", e.getMessage(), e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}

@PatchMapping("/{forageId}")
public ResponseEntity<Void> updateForage(
@PathVariable Long propertyId,
@PathVariable Long forageId,
@RequestBody @Valid ForageUpdateDto updateDto) {
forageService.updateForage(propertyId, forageId, updateDto);
return ResponseEntity.noContent().build(); // status 204
}

@GetMapping("/{id}/details")
public ResponseEntity<ForageDisponibilityDto> findById(@PathVariable Long id) {
ForageDisponibilityDto forageDto = forageService.findDtoById(id);
return ResponseEntity.ok(forageDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility;

import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.QueryHints; // Import necessário
import jakarta.persistence.QueryHint; // Import necessário

import java.util.Collection;
import java.util.List;
import java.util.Optional;

public interface ForageDisponibilityRepository extends
JpaRepository<ForageDisponibility, Long>,
Expand All @@ -19,4 +19,9 @@ public interface ForageDisponibilityRepository extends

List<ForageDisponibility> findByProperty_Id(Long propertyId);

}
Optional<ForageDisponibility> findByIdAndPropertyId(Long id, Long propertyId);

@Query("SELECT f FROM ForageDisponibility f WHERE f.id = :id")
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "false")})
Optional<ForageDisponibility> findByIdNoCache(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility;

import br.edu.utfpr.ProjetoIDRAPI.entity.crud.CrudService;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageCreateDto;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageDisponibilityDto;
import br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto.ForageUpdateDto;

import java.util.List;

public interface ForageDisponibilityService extends CrudService<ForageDisponibility, Long> {
List<ForageDisponibilityDto> findByPropertyId(Long propertyId);


ForageDisponibility createForage(Long propertyId, ForageCreateDto createDto);
void updateForage(Long propertyId, Long forageId, ForageUpdateDto updateDto);
ForageDisponibilityDto findDtoById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.edu.utfpr.ProjetoIDRAPI.entity.foragedisponibility.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.time.LocalDate;

@Data
public class ForageCreateDto {
@NotNull
private Double area;
@NotNull
private Double averageCost;
@NotNull
private Long usefulLife;
@NotNull
private LocalDate formation;
@NotNull
private String growthCycle;
private String observation;
@NotNull
private String ownershipType;
private String cultivation;
private Double entry;
private Double residue;
private Double kg;
private Double efficiency;
private Long numCows;
private Double kgCows;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
import br.edu.utfpr.ProjetoIDRAPI.entity.property.dto.PropertyDto;
import lombok.Data;

import java.time.LocalDate;

@Data
public class ForageDisponibilityDto {

private Long id;
private String cultivation; // antes era forage
private String area; // caso precise, converta de picketArea ou outra propriedade
private String averageCost; // mapear se tiver campo equivalente no backend
private String usefulLife; // mapear se tiver campo equivalente
private String formation; // antes era date (converta para string YYYY-MM-DD)
private String ownershipType; // antes era propriedade do tipo PropertyDto ou enum
private String growthCycle; // antes era propriedade do tipo enum
private String observation; // opcional, mapear se tiver campo
private Float entry;
private Float residue;
private Float kg;
private Float picketArea;
private Float efficiency;
private Long numCows; // converter BigInteger para Long
private Float kgCows;
private String area;
private String averageCost;
private String usefulLife;
private LocalDate formation; // antes era date
private String ownershipType;
private String growthCycle;
private String observation; // opcional
private Double entry;
private Double residue;
private Double kg;
private Double picketArea;
private Double efficiency;
private Long numCows;
private Double kgCows;

private PropertyDto property;


private PropertyDto property; // se quiser manter o PropertyDto, ok
}
Loading