WIP ingredients

This commit is contained in:
Meutel 2023-10-24 18:59:37 +02:00
parent 627d461e27
commit 623ec11e3b
9 changed files with 150 additions and 15 deletions

View File

@ -1,5 +1,7 @@
package net.meutel.recettes.api.entity;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@ -14,11 +16,13 @@ public class ReceipeEntity {
private String prepTime;
private String author;
private ReceipeYieldEntity receipeYield;
private List<ReceipeIngredientEntity> ingredients;
@Override
public String toString() {
return "ReceipeEntity [id=" + id + ", name=" + name + ", description=" + description + ", cookTime=" + cookTime
+ ", prepTime=" + prepTime + ", author=" + author + ", receipeYield=" + receipeYield + "]";
+ ", prepTime=" + prepTime + ", author=" + author + ", receipeYield=" + receipeYield + ", ingredients="
+ ingredients + "]";
}
public String getId() {
return id;
@ -61,6 +65,12 @@ public class ReceipeEntity {
}
public void setReceipeYield(ReceipeYieldEntity receipeYield) {
this.receipeYield = receipeYield;
}
public List<ReceipeIngredientEntity> getIngredients() {
return ingredients;
}
public void setIngredients(List<ReceipeIngredientEntity> ingredients) {
this.ingredients = ingredients;
}
}

View File

@ -0,0 +1,43 @@
package net.meutel.recettes.api.entity;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
public class ReceipeIngredientEntity {
private QuantityEntity quantity;
private String text;
@DocumentReference(collection = "parameters")
private RecetteParamEntity ref;
@Override
public String toString() {
return "ReceipeIngredientEntity [quantity=" + quantity + ", text=" + text + ", ref=" + ref + "]";
}
public RecetteParamEntity getRef() {
return ref;
}
public void setRef(RecetteParamEntity ref) {
this.ref = ref;
}
public QuantityEntity getQuantity() {
return quantity;
}
public void setQuantity(QuantityEntity quantity) {
this.quantity = quantity;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

View File

@ -0,0 +1,16 @@
package net.meutel.recettes.api.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import net.meutel.recettes.api.entity.ReceipeIngredientEntity;
import net.meutel.recettes.api.model.Ingredient;
@Mapper(componentModel = "spring", uses = {RecetteParamRefMapper.class})
public interface IngredientMapper {
Ingredient fromEntity(ReceipeIngredientEntity entity);
@Mapping(target = "ref", ignore = true)
ReceipeIngredientEntity toEntity(Ingredient model);
}

View File

@ -5,7 +5,7 @@ import org.mapstruct.Mapper;
import net.meutel.recettes.api.entity.ReceipeEntity;
import net.meutel.recettes.api.model.Receipe;
@Mapper(componentModel = "spring")
@Mapper(componentModel = "spring", uses = {IngredientMapper.class})
public interface ReceipeFullMapper {
Receipe fromEntity(ReceipeEntity entity);

View File

@ -9,9 +9,11 @@ import net.meutel.recettes.api.model.Receipe;
@Mapper(componentModel = "spring")
public interface ReceipeSimplifiedMapper {
@Mapping(target = "ingredients", ignore = true)
@Mapping(target = "receipeYield", ignore = true)
Receipe fromEntity(ReceipeEntity entity);
@Mapping(target = "ingredients", ignore = true)
ReceipeEntity toEntity(Receipe model);
}

View File

@ -0,0 +1,14 @@
package net.meutel.recettes.api.mapper;
import org.mapstruct.Mapper;
import net.meutel.recettes.api.entity.RecetteParamEntity;
@Mapper(componentModel = "spring")
public interface RecetteParamRefMapper {
default String fromEntity(RecetteParamEntity entity) {
return entity.getId();
};
}

View File

@ -15,15 +15,17 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import net.meutel.recettes.api.entity.QuantityEntity;
import net.meutel.recettes.api.entity.ReceipeEntity;
import net.meutel.recettes.api.entity.ReceipeIngredientEntity;
import net.meutel.recettes.api.entity.ReceipeYieldEntity;
import net.meutel.recettes.api.exception.ItemNotFoundException;
import net.meutel.recettes.api.model.Ingredient;
import net.meutel.recettes.api.model.Receipe;
import net.meutel.recettes.api.model.ReceipeReceipeYield;
import net.meutel.recettes.api.repository.ReceipeRepository;
@SpringBootTest
public class ReceipeServiceImplTest {
@Autowired
ReceipeService tested;
@ -34,19 +36,13 @@ public class ReceipeServiceImplTest {
void getReceipeById_noData_exception() {
Assertions.assertThrows(ItemNotFoundException.class, () -> tested.getReceipeById("TEST"));
}
@Test
void getReceipeById_exists_receipe() {
var r1 = new ReceipeEntity();
r1.setName("pates carbo");
var yield = new ReceipeYieldEntity();
yield.setOf("personne");
var qty = new QuantityEntity();
qty.setValue(new BigDecimal(1));
yield.setQuantity(qty);
r1.setReceipeYield(yield);
var r1 = buildReceipe();
when(repo.findById("TEST"))
.thenReturn(Optional.of(r1));
.thenReturn(Optional.of(r1));
var result = tested.getReceipeById("TEST");
@ -63,7 +59,13 @@ public class ReceipeServiceImplTest {
.extracting(Receipe::getReceipeYield)
.extracting(ReceipeReceipeYield::getOf)
.isEqualTo("personne");
assertThat(result)
.extracting(Receipe::getIngredients)
.isNotNull();
assertThat(result.getIngredients())
.singleElement()
.extracting(Ingredient::getText)
.isEqualTo("Parmesan");
}
@Test
@ -83,7 +85,7 @@ public class ReceipeServiceImplTest {
r2.setName("soupe a l'oignon");
when(repo.findAll())
.thenReturn(List.of(r1, r2));
.thenReturn(List.of(r1, r2));
var result = tested.findReceipes();
@ -96,5 +98,23 @@ public class ReceipeServiceImplTest {
.returns("soupe a l'oignon", Receipe::getName);
}
private ReceipeEntity buildReceipe() {
var r1 = new ReceipeEntity();
r1.setName("pates carbo");
var yield = new ReceipeYieldEntity();
yield.setOf("personne");
var qty = new QuantityEntity();
qty.setValue(new BigDecimal(1));
yield.setQuantity(qty);
r1.setReceipeYield(yield);
var i1 = new ReceipeIngredientEntity();
var i1qty = new QuantityEntity();
i1qty.setValue(new BigDecimal(100));
i1qty.setUnit("g");
i1.setQuantity(i1qty);
i1.setText("Parmesan");
r1.setIngredients(List.of(i1));
return r1;
}
}

View File

@ -0,0 +1,11 @@
meta {
name: GET receipe 6534c4bd1528e955187952b3
type: http
seq: 2
}
get {
url: http://localhost:8080/receipes/6534c4bd1528e955187952b3
body: none
auth: none
}

View File

@ -108,6 +108,8 @@ components:
type: object
required:
- id
- ingredients
- name
properties:
id:
description: Receipe unique id
@ -131,6 +133,10 @@ components:
author:
description: Receipe author/source
type: string
ingredients:
type: array
items:
$ref: '#/components/schemas/Ingredient'
receipeYield:
description: Quantity produced by receipe
type: object
@ -143,6 +149,19 @@ components:
of:
description: Production target
type: string
Ingredient:
description: Ingredient in receipe
type: object
properties:
quantity:
$ref: '#/components/schemas/Quantity'
text:
description: Name of ingredient
type: string
ref:
description: Link to ingredient parameter
type: string
format: url
Quantity:
type: object
required: