UCSBDiningCommonsMenuItemController.java

package edu.ucsb.cs156.example.controllers;

import edu.ucsb.cs156.example.entities.UCSBDiningCommonsMenuItem;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsMenuItemRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/** This is a REST controller for UCSBDiningCommonsMenuItem */
@Tag(name = "UCSBDiningCommonsMenuItem")
@RequestMapping("/api/ucsbdiningcommonsmenuitem")
@RestController
@Slf4j
public class UCSBDiningCommonsMenuItemController extends ApiController {

  @Autowired UCSBDiningCommonsMenuItemRepository ucsbDiningCommonsMenuItemRepository;

  /**
   * List all UCSB dining commons menu items
   *
   * @return an iterable of UCSBDiningCommonsMenuItem
   */
  @Operation(summary = "List all ucsb dining commons menu items")
  @PreAuthorize("hasRole('ROLE_USER')")
  @GetMapping("/all")
  public Iterable<UCSBDiningCommonsMenuItem> allUCSBDiningCommonsMenuItems() {
    Iterable<UCSBDiningCommonsMenuItem> items = ucsbDiningCommonsMenuItemRepository.findAll();
    return items;
  }

  /**
   * Create a new dining commons menu item
   *
   * @param diningCommonsCode the code of the dining commons (e.g. ortega, de-la-guerra, carrillo,
   *     portola)
   * @param name the name of the menu item
   * @param station the station at which the menu item is served
   * @return the saved UCSBDiningCommonsMenuItem
   */
  @Operation(summary = "Create a new dining commons menu item")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @PostMapping("/post")
  public UCSBDiningCommonsMenuItem postUCSBDiningCommonsMenuItem(
      @Parameter(
              name = "diningCommonsCode",
              description =
                  "code of the dining commons, e.g. ortega, de-la-guerra, carrillo, portola",
              example = "ortega")
          @RequestParam
          String diningCommonsCode,
      @Parameter(
              name = "name",
              description = "name of the menu item",
              example = "Baked Pesto Pasta with Chicken")
          @RequestParam
          String name,
      @Parameter(
              name = "station",
              description = "station at the dining commons where the item is served",
              example = "Entree Specials")
          @RequestParam
          String station) {

    UCSBDiningCommonsMenuItem item = new UCSBDiningCommonsMenuItem();
    item.setDiningCommonsCode(diningCommonsCode);
    item.setName(name);
    item.setStation(station);

    UCSBDiningCommonsMenuItem savedItem = ucsbDiningCommonsMenuItemRepository.save(item);

    return savedItem;
  }

  /**
   * Get a single dining commons menu item by id
   *
   * @param id the id of the menu item
   * @return the UCSBDiningCommonsMenuItem with the given id
   */
  @Operation(summary = "Get a single UCSB dining commons menu item")
  @PreAuthorize("hasRole('ROLE_USER')")
  @GetMapping("")
  public UCSBDiningCommonsMenuItem getById(
      @Parameter(name = "id", description = "id of the menu item to retrieve", example = "1")
          @RequestParam
          Long id) {
    UCSBDiningCommonsMenuItem item =
        ucsbDiningCommonsMenuItemRepository
            .findById(id)
            .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

    return item;
  }

  /**
   * Update a single dining commons menu item
   *
   * @param id the id of the menu item to update
   * @param incoming the new values, as a JSON request body containing diningCommonsCode, name and
   *     station (e.g. {"diningCommonsCode": "ortega", "name": "Baked Pesto Pasta with Chicken",
   *     "station": "Entree Specials"})
   * @return the updated UCSBDiningCommonsMenuItem
   */
  @Operation(summary = "Update a single UCSB dining commons menu item")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @PutMapping("")
  public UCSBDiningCommonsMenuItem updateUCSBDiningCommonsMenuItem(
      @Parameter(name = "id", description = "id of the menu item to update", example = "1")
          @RequestParam
          Long id,
      @io.swagger.v3.oas.annotations.parameters.RequestBody(
              description =
                  "new values for diningCommonsCode, name and station (the id is not changed)",
              required = true,
              content =
                  @Content(
                      mediaType = "application/json",
                      examples =
                          @ExampleObject(
                              value =
                                  "{\"diningCommonsCode\": \"carrillo\", \"name\": \"Chicken Caesar Salad\", \"station\": \"Grill\"}")))
          @RequestBody
          @Valid
          UCSBDiningCommonsMenuItem incoming) {

    UCSBDiningCommonsMenuItem item =
        ucsbDiningCommonsMenuItemRepository
            .findById(id)
            .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

    item.setDiningCommonsCode(incoming.getDiningCommonsCode());
    item.setName(incoming.getName());
    item.setStation(incoming.getStation());

    ucsbDiningCommonsMenuItemRepository.save(item);

    return item;
  }

  /**
   * Delete a single dining commons menu item
   *
   * @param id the id of the menu item to delete
   * @return a message indicating the menu item was deleted
   */
  @Operation(summary = "Delete a UCSB dining commons menu item")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @DeleteMapping("")
  public Object deleteUCSBDiningCommonsMenuItem(
      @Parameter(name = "id", description = "id of the menu item to delete", example = "1")
          @RequestParam
          Long id) {
    UCSBDiningCommonsMenuItem item =
        ucsbDiningCommonsMenuItemRepository
            .findById(id)
            .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

    ucsbDiningCommonsMenuItemRepository.delete(item);
    return genericMessage("UCSBDiningCommonsMenuItem with id %s deleted".formatted(id));
  }
}