Skip to content
Snippets Groups Projects

Resolve "Create and test Building & Room controller"

12 files
+ 783
0
Compare changes
  • Side-by-side
  • Inline

Files

/*
* Portal - A site for direct access to the Labrador system as a teacher or admin
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.portal.controller;
import java.util.List;
import javax.transaction.Transactional;
import javax.validation.Valid;
import nl.tudelft.labracore.api.BuildingControllerApi;
import nl.tudelft.labracore.api.dto.BuildingCreateDTO;
import nl.tudelft.labracore.api.dto.BuildingDetailsDTO;
import nl.tudelft.labracore.api.dto.BuildingPatchDTO;
import nl.tudelft.labracore.api.dto.BuildingSummaryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
/**
* The controller for performing CRUD operations on buildings.
*/
@Controller
@Transactional
@RequestMapping("building")
public class BuildingController {
@Autowired
private BuildingControllerApi buildingApi;
/**
* Provides the details of a building based on an id.
*
* @param id the id of the desired building
* @param model the model to add the data into
* @return the page to be loaded
*/
@GetMapping("{id}")
public String getBuilding(@PathVariable Long id, Model model) {
BuildingDetailsDTO building = buildingApi.getBuildingById(id).block();
model.addAttribute("building", building);
return "building/view";
}
/**
* Returns a list of all the existing buildings.
*
* @param model the model to add the data into
* @return the page to be loaded
*/
@GetMapping("all")
public String getAllBuildings(Model model) {
List<BuildingSummaryDTO> buildings = buildingApi.getAllBuildings().collectList().block();
model.addAttribute("buildings", buildings);
return "building/list";
}
/**
* Provides an empty BuildingCreateDTO so a new building can be created.
*
* @param model the model to add the data into
* @return the page to be loaded
*/
@GetMapping
public String getEmptyBuilding(Model model) {
BuildingCreateDTO create = new BuildingCreateDTO();
model.addAttribute("building", create);
return "building/create";
}
/**
* Creates a new building.
*
* @param create the createDTO which holds the data about the new building
* @return the page to be loaded
*/
@PostMapping
public String createBuilding(@Valid @ModelAttribute("building") BuildingCreateDTO create) {
Long id = buildingApi.addBuilding(create).block();
return "redirect:/building/" + id;
}
/**
* Provides the details of a building so it can be edited.
*
* @param id the id of the building
* @param model the model to add the data into
* @return the page to be loaded
*/
@GetMapping("{id}/edit")
public String editBuilding(@PathVariable Long id, Model model) {
BuildingDetailsDTO building = buildingApi.getBuildingById(id).block();
model.addAttribute("building", building);
return "building/edit";
}
/**
* Patches a specific building.
*
* @param id the id of the building to patch
* @param patch the dto that contains the patch data
* @return the page to be loaded
*/
@PatchMapping("{id}")
public String patchBuilding(@PathVariable Long id, @Valid @ModelAttribute BuildingPatchDTO patch) {
buildingApi.patchBuilding(id, patch);
return "redirect:/building/" + id;
}
}
Loading