Repository navigation
Add ingredient API endpoints #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5379a6c
Adss Ingredient API by implementing the controller, service, reposito…
leslyVillanueva1020 63a348c
updated ingredient.java and IngredientService
leslyVillanueva1020 942a0fe
Merge branch 'main' of https://github.com/Solian21/CST438_P2_API into…
leslyVillanueva1020 fd61310
updated Security Config file to allow public ingredient browsing whil…
leslyVillanueva1020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
api/src/main/java/edu/csumb/cst438/api/controller/IngredientController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<IngredientResponse> 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<IngredientResponse> createIngredient( | ||
| @Valid @RequestBody IngredientCreate request) { | ||
|
|
||
| IngredientResponse created = | ||
| ingredientService.create(request); | ||
|
|
||
| return ResponseEntity | ||
| .created(URI.create( | ||
| "/api/v1/ingredients/" + created.getId() | ||
| )) | ||
| .body(created); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
api/src/main/java/edu/csumb/cst438/api/record/IngredientCreate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package edu.csumb.cst438.api.record; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record IngredientCreate( | ||
| @NotBlank String ingredientName | ||
| ) { | ||
| } |
23 changes: 23 additions & 0 deletions
23
api/src/main/java/edu/csumb/cst438/api/record/IngredientResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
api/src/main/java/edu/csumb/cst438/api/repository/IngredientRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Ingredient, Long> { | ||
|
|
||
| boolean existsByIngredientNameIgnoreCase(String ingredientName); | ||
| } |
65 changes: 65 additions & 0 deletions
65
api/src/main/java/edu/csumb/cst438/api/service/IngredientService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 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.dao.DataIntegrityViolationException; | ||
| 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<IngredientResponse> listIngredients(Pageable pageable) { | ||
| return ingredientRepository | ||
| .findAll(pageable) | ||
| .map(IngredientResponse::from); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public Page<IngredientResponse> 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()); | ||
|
|
||
| try { | ||
| Ingredient savedIngredient = | ||
| ingredientRepository.saveAndFlush(ingredient); | ||
|
|
||
|
leslyVillanueva1020 marked this conversation as resolved.
Resolved
|
||
| return IngredientResponse.from(savedIngredient); | ||
|
|
||
| } catch (DataIntegrityViolationException exception) { | ||
| throw new ResponseStatusException( | ||
| HttpStatus.CONFLICT, | ||
| "Ingredient already exists", | ||
| exception | ||
| ); | ||
| } | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
api/src/test/java/edu/csumb/cst438/api/service/IngredientServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Ingredient> ingredientPage = | ||
| new PageImpl<>(List.of(ingredient)); | ||
|
|
||
| when(ingredientRepository.findAll(pageable)) | ||
| .thenReturn(ingredientPage); | ||
|
|
||
| Page<IngredientResponse> result = | ||
| ingredientService.listIngredients(pageable); | ||
|
|
||
| assertEquals(1, result.getContent().size()); | ||
| assertEquals( | ||
| "Flour", | ||
| result.getContent().get(0).getIngredientName() | ||
| ); | ||
|
|
||
| verify(ingredientRepository).findAll(pageable); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.