com.tsguild.upsproject.controller.HomeController.java Source code

Java tutorial

Introduction

Here is the source code for com.tsguild.upsproject.controller.HomeController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tsguild.upsproject.controller;

import com.tsguild.upsproject.dao.UPSDao;
import com.tsguild.upsproject.dto.Box;
import com.tsguild.upsproject.dto.Cannister;
import com.tsguild.upsproject.dto.Plane;
import java.util.List;
import javax.inject.Inject;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 *
 * @author Tripp Preston
 */
@Controller
public class HomeController {

    private UPSDao dao;

    @Inject
    public HomeController(UPSDao dao) {
        this.dao = dao;

    }

    public HomeController() {
    }

    @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
    public String displayHomePage() {
        return "home";
    }

    @RequestMapping(value = "/planespage", method = RequestMethod.GET)
    public String displayPlanesPage() {
        return "planespage";
    }

    @RequestMapping(value = "/cannisterspage", method = RequestMethod.GET)
    public String displayCannistersPage() {
        return "cannisterspage";
    }

    //Plane Methods 
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/plane", method = RequestMethod.POST)
    public Plane createPlane(@RequestBody Plane incomingPlane) {
        dao.addPlane(incomingPlane);
        return incomingPlane;
    }

    @ResponseBody
    @RequestMapping(value = "/planes", method = RequestMethod.GET)
    public List<Plane> getAllPlanes() {
        return dao.getAllPlanes();
    }

    @ResponseBody
    @RequestMapping(value = "/plane/{planeId}", method = RequestMethod.GET)
    public Plane getPlaneById(@PathVariable("planeId") String planeId) {
        return dao.getPlaneById(planeId);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/plane/planeId", method = RequestMethod.PUT)
    public void updatePlane(@PathVariable("planeId") String planeId, @RequestBody Plane updatedPlane) {
        updatedPlane.setId(planeId);
        dao.updatePlane(updatedPlane);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/plane/{planeId}", method = RequestMethod.DELETE)
    public void removePlane(@PathVariable("planeId") String planeId) {
        dao.removePlane(planeId);
    }

    //Cannister Methods
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/cannister", method = RequestMethod.POST)
    public Cannister createCannister(@RequestBody Cannister incomingCannister) {
        dao.addCannister(incomingCannister);
        return incomingCannister;
    }

    @ResponseBody
    @RequestMapping(value = "/cannisters", method = RequestMethod.GET)
    public List<Cannister> getAllCannisters() {
        return dao.getAllCannisters();
    }

    @ResponseBody
    @RequestMapping(value = "/cannister/{cannisterId}", method = RequestMethod.GET)
    public Cannister getCannisterById(@PathVariable("cannisterId") int cannisterId) {
        return dao.getCannisterById(cannisterId);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/cannister/{cannisterId}", method = RequestMethod.PUT)
    public void updateCannister(@PathVariable("cannisterId") int cannisterId,
            @RequestBody Cannister updatedCannister) {
        updatedCannister.setId(cannisterId);
        dao.updateCannister(updatedCannister);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/cannister/{cannisterId}", method = RequestMethod.DELETE)
    public void removeCannister(@PathVariable("cannisterId") int cannisterId) {
        dao.removeCannister(cannisterId);
    }

    //Box Methods
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/package", method = RequestMethod.POST)
    public Box addPackage(@RequestBody Box incomingPackage) {
        dao.addBox(incomingPackage);
        return incomingPackage;
    }

    @ResponseBody
    @RequestMapping(value = "/packages", method = RequestMethod.GET)
    public List<Box> getAllPackages() {
        return dao.getAllBoxes();
    }

    @ResponseBody
    @RequestMapping(value = "/package/{packageId}", method = RequestMethod.GET)
    public Box getPackageById(@PathVariable("packageId") String packageId) {
        return dao.getBoxById(packageId);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/package/{packageId}", method = RequestMethod.PUT)
    public void updatePackage(@PathVariable("packageId") String packageId, @RequestBody Box updatedPackage) {
        updatedPackage.setId(packageId);
        dao.updateBox(updatedPackage);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/package/{packageId}", method = RequestMethod.DELETE)
    public void removePackage(@PathVariable("packageId") String packageId) {
        dao.removeBox(packageId);
    }
}