Skip to content

Commit

Permalink
61 Refactor schema and api (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
asavershin authored Nov 9, 2024
1 parent 6c5a2b1 commit 1ed9d7c
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public List<CropRotationDTO> getCropRotations(UUID contourID) {
public IdDTO saveCropRotation(UUID contourId, CropRotationDTO cropRotationDTO) {
var cropRotation = cropRotationMapperV2.map(cropRotationDTO);
var cropRotationEntity = cropRotationService.save(contourId, cropRotation);
return new IdDTO(cropRotationEntity.getCropRotationId());
return new IdDTO(cropRotationEntity.getId());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<FieldWithContoursAndCropRotationsDTO> findFields(UUID seasonId) {
public IdDTO saveField(UUID seasonId, FieldDTO fieldDTO) {
var fieldEntity = fieldMapperV2.map(fieldDTO);
var savedFieldEntity = fieldService.save(seasonId, fieldEntity);
return new IdDTO(savedFieldEntity.getFieldId());
return new IdDTO(savedFieldEntity.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public IdDTO saveSeason(SeasonBaseDTO seasonDTO) {
Season season = seasonsMapper.map(seasonDTO);
season.setOrganizationId(token().orgId());
var seasonEntity = seasonsService.save(season);
return new IdDTO(seasonEntity.getSeasonId());
return new IdDTO(seasonEntity.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<SoilCompositionDTO> getSoilCompositions(UUID contourId) {
public IdDTO saveSoilComposition(UUID contourId, SoilCompositionDTO soilCompositionDTO) {
var soilComposition = soilCompositionMapper.map(soilCompositionDTO);
var soilCompositionEntity = soilCompositionsService.save(contourId, soilComposition);
return new IdDTO(soilCompositionEntity.getSoilCompositionId());
return new IdDTO(soilCompositionEntity.getId());
}

}
24 changes: 24 additions & 0 deletions src/main/java/agroscience/fields/v2/entities/AbstractEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package agroscience.fields.v2.entities;

import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import java.util.UUID;
import lombok.Data;

@Data
@MappedSuperclass
public abstract class AbstractEntity {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;

@Column(name = "archived", nullable = false)
protected boolean archived;


}
14 changes: 0 additions & 14 deletions src/main/java/agroscience/fields/v2/entities/ArchivedEntity.java

This file was deleted.

8 changes: 1 addition & 7 deletions src/main/java/agroscience/fields/v2/entities/Contour.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -23,11 +21,7 @@
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Contour extends ArchivedEntity {

@Id
@Column(name = "contour_id")
private UUID contourId;
public class Contour extends AbstractEntity {

@Column(name = "name", nullable = false)
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -19,11 +17,7 @@
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class CropRotationV2 extends ArchivedEntity {

@Id
@Column(name = "crop_rotation_id")
private UUID cropRotationId;
public class CropRotationV2 extends AbstractEntity {

@Column(name = "start_date", nullable = false)
private LocalDate startDate;
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/agroscience/fields/v2/entities/FieldV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -23,11 +20,7 @@
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class FieldV2 extends ArchivedEntity {

@Id
@Column(name = "field_id")
private UUID fieldId;
public class FieldV2 extends AbstractEntity {

@Column(name = "name")
private String name;
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/agroscience/fields/v2/entities/Season.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDate;
Expand All @@ -21,11 +20,7 @@
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Season extends ArchivedEntity {

@Id
@Column(name = "season_id")
private UUID seasonId;
public class Season extends AbstractEntity {

@Column(name = "name")
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -20,11 +18,7 @@
@NoArgsConstructor
@SuppressWarnings("MemberName")
@EqualsAndHashCode(callSuper = true)
public class SoilComposition extends ArchivedEntity {

@Id
@Column(name = "soil_composition_id")
private UUID soilCompositionId;
public class SoilComposition extends AbstractEntity {

@Column(name = "ph")
private String ph;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.AbstractEntity;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface AbstractRepository<T extends AbstractEntity> extends JpaRepository<T, UUID> {

List<T> getAllByIdAndArchivedIsFalse(UUID id);


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.Contour;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ContoursRepository extends JpaRepository<Contour, UUID> {
public interface ContoursRepository extends AbstractRepository<Contour> {

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.CropRotationV2;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CropRotationRepositoryV2 extends JpaRepository<CropRotationV2, UUID> {
public interface CropRotationRepositoryV2 extends AbstractRepository<CropRotationV2> {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import agroscience.fields.v2.entities.FieldV2;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FieldsRepository extends JpaRepository<FieldV2, UUID> {
public interface FieldsRepository extends AbstractRepository<FieldV2> {

List<FieldV2> findAllBySeason_SeasonId(UUID id);
List<FieldV2> findAllBySeason_Id(UUID id);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.Season;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SeasonsRepository extends JpaRepository<Season, UUID> {
public interface SeasonsRepository extends AbstractRepository<Season> {

List<Season> getAllByOrganizationIdAndArchivedIsFalse(UUID organizationId);

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package agroscience.fields.v2.repositories;

import agroscience.fields.v2.entities.SoilComposition;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SoilCompositionsRepository extends JpaRepository<SoilComposition, UUID> {
public interface SoilCompositionsRepository extends AbstractRepository<SoilComposition> {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public Contour findById(UUID contourId) {
public UUID save(UUID fieldId, Contour contour) {
var field = getOrThrow(fieldId, fieldsRepository::findById);
contour.setField(field);
var contourId = UUID.randomUUID();
contour.setContourId(contourId);
contoursRepository.save(contour);
return contourId;
return contoursRepository.save(contour).getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class CropRotationServiceV2 extends DefaultService {
public CropRotationV2 save(UUID contourId, CropRotationV2 cropRotation) {
var contour = getOrThrow(contourId, contoursRepository::findById);
cropRotation.setContour(contour);
var cropRotationId = UUID.randomUUID();
cropRotation.setCropRotationId(cropRotationId);
return cropRotationRepository.save(cropRotation);
}

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

import static java.lang.String.format;

import agroscience.fields.v2.entities.ArchivedEntity;
import agroscience.fields.v2.entities.AbstractEntity;
import jakarta.persistence.EntityNotFoundException;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -19,8 +19,8 @@ protected static <T> T getOrThrow(UUID id, Function<UUID, Optional<T>> function
return function.apply(id).orElseThrow(() -> new EntityNotFoundException(format(NOT_FOUND, id)));
}

protected static <T> void checkArchived(UUID id, T entity) {
if (entity instanceof ArchivedEntity && ((ArchivedEntity) entity).isArchived()) {
protected static <T extends AbstractEntity> void checkArchived(UUID id, T entity) {
if (entity.isArchived()) {
throw new EntityNotFoundException(format(NOT_FOUND + "because archived", id));
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/agroscience/fields/v2/services/FieldsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ public class FieldsService extends DefaultService {
public FieldV2 save(UUID seasonId, FieldV2 field) {
var season = getOrThrow(seasonId, seasonsRepository::findById);
field.setSeason(season);
var fieldId = UUID.randomUUID();
field.setFieldId(fieldId);
field.getContours().forEach(c -> {
c.setContourId(UUID.randomUUID());
c.setField(field);
});
return fieldsRepository.save(field); // try ... catch
field.getContours().forEach(c -> c.setField(field));
return fieldsRepository.save(field);
}

public FieldV2 findById(UUID id) {
return fieldsRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Field not found"));
}

public List<FieldV2> findAll(UUID seasonId) {
return fieldsRepository.findAllBySeason_SeasonId(seasonId);
return fieldsRepository.findAllBySeason_Id(seasonId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ public class SeasonsService {
private final SeasonsRepository seasonsRepository;

public Season save(Season season) {
var seasonId = UUID.randomUUID();
season.setSeasonId(seasonId);
return seasonsRepository.save(season);
}

public List<Season> getAll(UUID organizationId) {
return seasonsRepository.getAllByOrganizationIdAndArchivedIsFalse(organizationId);
return seasonsRepository.getAllByIdAndArchivedIsFalse(organizationId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class SoilCompositionsService extends DefaultService {
public SoilComposition save(UUID contourId, SoilComposition soilComposition) {
var contour = getOrThrow(contourId, contoursRepository::findById);
soilComposition.setContour(contour);
var soilCompositionId = UUID.randomUUID();
soilComposition.setSoilCompositionId(soilCompositionId);
return soilCompositionsRepository.save(soilComposition);
}

Expand Down
Loading

0 comments on commit 1ed9d7c

Please sign in to comment.