pitayaa.nail.msg.core.license.controller.LicenseController.java Source code

Java tutorial

Introduction

Here is the source code for pitayaa.nail.msg.core.license.controller.LicenseController.java

Source

package pitayaa.nail.msg.core.license.controller;

import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 pitayaa.nail.domain.license.License;
import pitayaa.nail.msg.core.common.CoreHelper;
import pitayaa.nail.msg.core.license.service.ILicenseService;

@Controller
public class LicenseController {

    @Autowired
    private CoreHelper coreHelper;

    @Autowired
    private ILicenseService licenseService;

    @RequestMapping(value = "licenses/model", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<?> initAccountModel() throws Exception {

        License account = (License) coreHelper.createModelStructure(new License());

        return ResponseEntity.ok(account);
    }

    @RequestMapping(value = "licenses", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<?> createLicense(@RequestBody License licenseBody) throws Exception {

        License license = licenseService.saveLicense(licenseBody);

        return new ResponseEntity<>(license, HttpStatus.CREATED);
    }

    @RequestMapping(value = "licenses/{ID}", method = RequestMethod.PUT)
    public @ResponseBody ResponseEntity<?> updateLicense(@PathVariable("ID") UUID uid,
            @RequestBody License licenseBody) throws Exception {

        License license = licenseService.updateLicense(licenseBody);

        return ResponseEntity.ok(license);
    }

    @RequestMapping(value = "licenses/{ID}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseEntity<?> deleteLicense(@PathVariable("ID") UUID uid) throws Exception {

        // Find license
        Optional<License> optionalLicense = licenseService.findOne(uid);
        if (!optionalLicense.isPresent()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        // Delete license
        boolean isDelete = licenseService.deleteLicense(uid);

        // Get status
        HttpStatus status = (isDelete) ? HttpStatus.NO_CONTENT : HttpStatus.EXPECTATION_FAILED;

        // Return
        return new ResponseEntity<>(status);
    }
}