| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.MenuItem; | |
| 4 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.dining.models.Entree; | |
| 6 | import edu.ucsb.cs156.dining.repositories.MenuItemRepository; | |
| 7 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import java.util.ArrayList; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Optional; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | import org.springframework.http.ResponseEntity; | |
| 17 | import org.springframework.web.bind.annotation.GetMapping; | |
| 18 | import org.springframework.web.bind.annotation.PathVariable; | |
| 19 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestParam; | |
| 21 | import org.springframework.web.bind.annotation.RestController; | |
| 22 | ||
| 23 | @Tag(name = "UCSBDiningMenuItems") | |
| 24 | @RestController | |
| 25 | @RequestMapping("/api/diningcommons") | |
| 26 | @Slf4j | |
| 27 | public class UCSBDiningMenuItemsController extends ApiController { | |
| 28 | ||
| 29 | @Autowired UCSBDiningMenuItemsService ucsbDiningMenuItemsService; | |
| 30 | ||
| 31 | @Autowired MenuItemRepository menuItemRepository; | |
| 32 | ||
| 33 | @Operation(summary = "Get list of entrees being served at given meal, dining common, and day") | |
| 34 | @GetMapping( | |
| 35 | value = "/{date-time}/{dining-commons-code}/{meal-code}", | |
| 36 | produces = "application/json") | |
| 37 | public ResponseEntity<List<MenuItem>> get_menu_items( | |
| 38 | @Parameter( | |
| 39 | description = | |
| 40 | "date (in iso format, e.g. YYYY-mm-dd) or date-time (in iso format e.g. YYYY-mm-ddTHH:MM:SS)") | |
| 41 | @PathVariable("date-time") | |
| 42 | String datetime, | |
| 43 | @PathVariable("dining-commons-code") String diningcommoncode, | |
| 44 | @PathVariable("meal-code") String mealcode) | |
| 45 | throws Exception { | |
| 46 | ||
| 47 | List<Entree> body = ucsbDiningMenuItemsService.get(datetime, diningcommoncode, mealcode); | |
| 48 | ||
| 49 | List<MenuItem> menuitems = new ArrayList<>(); | |
| 50 | ||
| 51 | for (Entree entree : body) { | |
| 52 | Optional<MenuItem> exists = | |
| 53 | menuItemRepository.findByDiningCommonsCodeAndMealCodeAndNameAndStation( | |
| 54 | diningcommoncode, mealcode, entree.getName(), entree.getStation()); | |
| 55 | ||
| 56 | MenuItem newMenuItem = exists.orElse(new MenuItem()); | |
| 57 | // MenuItem newMenuItem = new MenuItem(); | |
| 58 |
1
1. get_menu_items : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setDiningCommonsCode → KILLED |
newMenuItem.setDiningCommonsCode(diningcommoncode); |
| 59 |
1
1. get_menu_items : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setMealCode → KILLED |
newMenuItem.setMealCode(mealcode); |
| 60 |
1
1. get_menu_items : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setName → KILLED |
newMenuItem.setName(entree.getName()); |
| 61 |
1
1. get_menu_items : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setStation → KILLED |
newMenuItem.setStation(entree.getStation()); |
| 62 | ||
| 63 | menuItemRepository.save(newMenuItem); | |
| 64 | menuitems.add(newMenuItem); | |
| 65 | } | |
| 66 | ||
| 67 |
1
1. get_menu_items : replaced return value with null for edu/ucsb/cs156/dining/controllers/UCSBDiningMenuItemsController::get_menu_items → KILLED |
return ResponseEntity.ok().body(menuitems); |
| 68 | } | |
| 69 | ||
| 70 | @Operation(summary = "Get a single menu item by ID") | |
| 71 | @GetMapping(value = "/menuitem", produces = "application/json") | |
| 72 | public ResponseEntity<MenuItem> get_menu_item_by_id( | |
| 73 | @Parameter(description = "ID of the menu item") @RequestParam long id) throws Exception { | |
| 74 | Optional<MenuItem> menuItem = menuItemRepository.findById(id); | |
| 75 |
1
1. get_menu_item_by_id : negated conditional → KILLED |
if (menuItem.isEmpty()) { |
| 76 | throw new EntityNotFoundException(MenuItem.class, id); | |
| 77 | } | |
| 78 |
1
1. get_menu_item_by_id : replaced return value with null for edu/ucsb/cs156/dining/controllers/UCSBDiningMenuItemsController::get_menu_item_by_id → KILLED |
return ResponseEntity.ok().body(menuItem.get()); |
| 79 | } | |
| 80 | } | |
Mutations | ||
| 58 |
1.1 |
|
| 59 |
1.1 |
|
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 67 |
1.1 |
|
| 75 |
1.1 |
|
| 78 |
1.1 |