From 5379a6cd34c12f002f34fdae1a08527127bb8a69 Mon Sep 17 00:00:00 2001 From: Lesly Villanueva Date: Sun, 18 Oct 2026 10:29:45 -0700 Subject: [PATCH 1/3] Adss Ingredient API by implementing the controller, service, repository, request/response records Created IngredientController - handles HTTP requests from client - GET /api/v1/ingredients - POST /api/v1/ingredients Created IngredientCreate - represents data needed to create an ingredient Created Ingredient Response - represents the data sent back to the client - converts an Ingredient database entity into safe API output Created IngredientService.java - handles pagination - checks duplicate names - creates/saves ingredients - converts entities into reponse objects --- .../api/controller/IngredientController.java | 52 ++++++++++++++++++ .../cst438/api/record/IngredientCreate.java | 8 +++ .../cst438/api/record/IngredientResponse.java | 23 ++++++++ .../api/repository/IngredientRepository.java | 10 ++++ .../cst438/api/service/IngredientService.java | 55 +++++++++++++++++++ .../api/service/IngredientServiceTest.java | 53 ++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 api/src/main/java/edu/csumb/cst438/api/controller/IngredientController.java create mode 100644 api/src/main/java/edu/csumb/cst438/api/record/IngredientCreate.java create mode 100644 api/src/main/java/edu/csumb/cst438/api/record/IngredientResponse.java create mode 100644 api/src/main/java/edu/csumb/cst438/api/repository/IngredientRepository.java create mode 100644 api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java create mode 100644 api/src/test/java/edu/csumb/cst438/api/service/IngredientServiceTest.java diff --git a/api/src/main/java/edu/csumb/cst438/api/controller/IngredientController.java b/api/src/main/java/edu/csumb/cst438/api/controller/IngredientController.java new file mode 100644 index 0000000..72556c2 --- /dev/null +++ b/api/src/main/java/edu/csumb/cst438/api/controller/IngredientController.java @@ -0,0 +1,52 @@ +package edu.csumb.cst438.api.controller; + +import edu.csumb.cst438.api.record.IngredientCreate; +import edu.csumb.cst438.api.record.IngredientResponse; +import edu.csumb.cst438.api.service.IngredientService; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import org.springframework.data.web.PagedModel; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.net.URI; + +@RestController +@RequestMapping("/api/v1/ingredients") +public class IngredientController { + + private final IngredientService ingredientService; + + public IngredientController(IngredientService ingredientService) { + this.ingredientService = ingredientService; + } + + @GetMapping + public PagedModel listIngredients( + @RequestParam(defaultValue = "0") + @Min(0) int page, + + @RequestParam(defaultValue = "20") + @Min(1) + @Max(100) int size) { + + return new PagedModel<>( + ingredientService.list(page, size) + ); + } + + @PostMapping + public ResponseEntity createIngredient( + @Valid @RequestBody IngredientCreate request) { + + IngredientResponse created = + ingredientService.create(request); + + return ResponseEntity + .created(URI.create( + "/api/v1/ingredients/" + created.getId() + )) + .body(created); + } +} \ No newline at end of file diff --git a/api/src/main/java/edu/csumb/cst438/api/record/IngredientCreate.java b/api/src/main/java/edu/csumb/cst438/api/record/IngredientCreate.java new file mode 100644 index 0000000..eb76896 --- /dev/null +++ b/api/src/main/java/edu/csumb/cst438/api/record/IngredientCreate.java @@ -0,0 +1,8 @@ +package edu.csumb.cst438.api.record; + +import jakarta.validation.constraints.NotBlank; + +public record IngredientCreate( + @NotBlank String ingredientName +) { +} \ No newline at end of file diff --git a/api/src/main/java/edu/csumb/cst438/api/record/IngredientResponse.java b/api/src/main/java/edu/csumb/cst438/api/record/IngredientResponse.java new file mode 100644 index 0000000..4d0a177 --- /dev/null +++ b/api/src/main/java/edu/csumb/cst438/api/record/IngredientResponse.java @@ -0,0 +1,23 @@ +package edu.csumb.cst438.api.record; + +import edu.csumb.cst438.api.entity.Ingredient; + +public record IngredientResponse( + Long id, + String ingredientName +) { + public static IngredientResponse from(Ingredient ingredient) { + return new IngredientResponse( + ingredient.getId(), + ingredient.getIngredientName() + ); + } + + public Long getId() { + return id; + } + + public String getIngredientName() { + return ingredientName; + } +} \ No newline at end of file diff --git a/api/src/main/java/edu/csumb/cst438/api/repository/IngredientRepository.java b/api/src/main/java/edu/csumb/cst438/api/repository/IngredientRepository.java new file mode 100644 index 0000000..7f94785 --- /dev/null +++ b/api/src/main/java/edu/csumb/cst438/api/repository/IngredientRepository.java @@ -0,0 +1,10 @@ +package edu.csumb.cst438.api.repository; + +import edu.csumb.cst438.api.entity.Ingredient; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface IngredientRepository + extends JpaRepository { + + boolean existsByIngredientNameIgnoreCase(String ingredientName); +} \ No newline at end of file diff --git a/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java b/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java new file mode 100644 index 0000000..fdacbe9 --- /dev/null +++ b/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java @@ -0,0 +1,55 @@ +package edu.csumb.cst438.api.service; + +import edu.csumb.cst438.api.entity.Ingredient; +import edu.csumb.cst438.api.record.IngredientCreate; +import edu.csumb.cst438.api.record.IngredientResponse; +import edu.csumb.cst438.api.repository.IngredientRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.server.ResponseStatusException; + +@Service +@Transactional +public class IngredientService { + + private final IngredientRepository ingredientRepository; + + public IngredientService(IngredientRepository ingredientRepository) { + this.ingredientRepository = ingredientRepository; + } + + @Transactional(readOnly = true) + public Page listIngredients(Pageable pageable) { + return ingredientRepository + .findAll(pageable) + .map(IngredientResponse::from); + } + + @Transactional(readOnly = true) + public Page list(int page, int size) { + return listIngredients(PageRequest.of(page, size)); + } + + public IngredientResponse create(IngredientCreate request) { + if (ingredientRepository.existsByIngredientNameIgnoreCase( + request.ingredientName())) { + + throw new ResponseStatusException( + HttpStatus.CONFLICT, + "Ingredient already exists" + ); + } + + Ingredient ingredient = + new Ingredient(request.ingredientName()); + + Ingredient savedIngredient = + ingredientRepository.save(ingredient); + + return IngredientResponse.from(savedIngredient); + } +} \ No newline at end of file diff --git a/api/src/test/java/edu/csumb/cst438/api/service/IngredientServiceTest.java b/api/src/test/java/edu/csumb/cst438/api/service/IngredientServiceTest.java new file mode 100644 index 0000000..0ae5297 --- /dev/null +++ b/api/src/test/java/edu/csumb/cst438/api/service/IngredientServiceTest.java @@ -0,0 +1,53 @@ +package edu.csumb.cst438.api.service; + +import edu.csumb.cst438.api.record.IngredientResponse; +import edu.csumb.cst438.api.entity.Ingredient; +import edu.csumb.cst438.api.repository.IngredientRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class IngredientServiceTest { + + @Mock + private IngredientRepository ingredientRepository; + + @InjectMocks + private IngredientService ingredientService; + + @Test + void listIngredientsReturnsIngredientResponses() { + Pageable pageable = PageRequest.of(0, 20); + Ingredient ingredient = new Ingredient("Flour"); + + Page ingredientPage = + new PageImpl<>(List.of(ingredient)); + + when(ingredientRepository.findAll(pageable)) + .thenReturn(ingredientPage); + + Page result = + ingredientService.listIngredients(pageable); + + assertEquals(1, result.getContent().size()); + assertEquals( + "Flour", + result.getContent().get(0).getIngredientName() + ); + + verify(ingredientRepository).findAll(pageable); + } +} \ No newline at end of file From 63a348c1552d7075dd42646f644b9e2f23cee322 Mon Sep 17 00:00:00 2001 From: Lesly Villanueva Date: Mon, 19 Oct 2026 09:05:58 -0700 Subject: [PATCH 2/3] updated ingredient.java and IngredientService - ingredient updated so it stores the original name and a normalized lowercase - changed IngredientService to use saveAndFlush() and the exception returns a 409 --- .../csumb/cst438/api/entity/Ingredient.java | 33 ++++++++++++++++--- .../cst438/api/service/IngredientService.java | 16 +++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/edu/csumb/cst438/api/entity/Ingredient.java b/api/src/main/java/edu/csumb/cst438/api/entity/Ingredient.java index fbb57b9..09a2725 100644 --- a/api/src/main/java/edu/csumb/cst438/api/entity/Ingredient.java +++ b/api/src/main/java/edu/csumb/cst438/api/entity/Ingredient.java @@ -3,27 +3,37 @@ import jakarta.persistence.*; import java.util.HashSet; +import java.util.Locale; import java.util.Set; @Entity -@Table(name="ingredients") +@Table(name = "ingredients") public class Ingredient { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(name="ingredient_name", nullable = false, unique = true) + @Column(name = "ingredient_name", nullable = false) private String ingredientName; + @Column( + name = "ingredient_name_normalized", + nullable = false, + unique = true + ) + private String normalizedName; + @ManyToMany(mappedBy = "ingredients") private Set recipes = new HashSet<>(); - protected Ingredient() {} + protected Ingredient() { + } public Ingredient(String ingredientName) { - this.ingredientName = ingredientName; + setIngredientName(ingredientName); } + public Long getId() { return id; } @@ -34,6 +44,7 @@ public String getIngredientName() { public void setIngredientName(String ingredientName) { this.ingredientName = ingredientName; + this.normalizedName = normalize(ingredientName); } public Set getRecipes() { @@ -47,4 +58,16 @@ void addRecipe(Recipe recipe) { void removeRecipe(Recipe recipe) { recipes.remove(recipe); } -} + + @PrePersist + @PreUpdate + private void updateNormalizedName() { + this.normalizedName = normalize(this.ingredientName); + } + + private static String normalize(String value) { + return value == null + ? null + : value.trim().toLowerCase(Locale.ROOT); + } +} \ No newline at end of file diff --git a/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java b/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java index fdacbe9..010b6fc 100644 --- a/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java +++ b/api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java @@ -4,6 +4,7 @@ import edu.csumb.cst438.api.record.IngredientCreate; import edu.csumb.cst438.api.record.IngredientResponse; import edu.csumb.cst438.api.repository.IngredientRepository; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -47,9 +48,18 @@ public IngredientResponse create(IngredientCreate request) { Ingredient ingredient = new Ingredient(request.ingredientName()); - Ingredient savedIngredient = - ingredientRepository.save(ingredient); + try { + Ingredient savedIngredient = + ingredientRepository.saveAndFlush(ingredient); - return IngredientResponse.from(savedIngredient); + return IngredientResponse.from(savedIngredient); + + } catch (DataIntegrityViolationException exception) { + throw new ResponseStatusException( + HttpStatus.CONFLICT, + "Ingredient already exists", + exception + ); + } } } \ No newline at end of file From fd613101c483422879633eb0b45172a72f59df54 Mon Sep 17 00:00:00 2001 From: Lesly Villanueva Date: Mon, 19 Oct 2026 09:18:15 -0700 Subject: [PATCH 3/3] updated Security Config file to allow public ingredient browsing while requiring authentication to create ingredients --- api/src/main/java/edu/csumb/cst438/api/SecurityConfig.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/src/main/java/edu/csumb/cst438/api/SecurityConfig.java b/api/src/main/java/edu/csumb/cst438/api/SecurityConfig.java index c34e866..47af714 100644 --- a/api/src/main/java/edu/csumb/cst438/api/SecurityConfig.java +++ b/api/src/main/java/edu/csumb/cst438/api/SecurityConfig.java @@ -58,6 +58,11 @@ public SecurityFilterChain securityFilterChain( .requestMatchers(HttpMethod.GET, "/api/v1/users/{userid}").hasRole("ADMIN") .requestMatchers(HttpMethod.PATCH, "/api/v1/users/{userid}").hasRole("ADMIN") .requestMatchers(HttpMethod.DELETE, "/api/v1/users/{userid}").hasRole("ADMIN") + // Anyone can browse ingredients. + .requestMatchers(HttpMethod.GET, "/api/v1/ingredients", "/api/v1/ingredients/**").permitAll() + //only authenticated users can add ingredients + .requestMatchers(HttpMethod.POST, "/api/v1/ingredients").authenticated() + .anyRequest().authenticated() ) .exceptionHandling(ex -> ex