com.mycompany.springrest.controllers.RoleController.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.springrest.controllers.RoleController.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.mycompany.springrest.controllers;

import com.mycompany.springrest.model.Role;
import com.mycompany.springrest.services.RoleService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.util.UriComponentsBuilder;

/**
 *
 * @author ??
 */
@RestController
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired(required = true)
    private RoleService roleService;

    //    @Autowired(required = true)
    //    @Qualifier(value = "roleService")
    //    public void setRoleService(RoleService roleService) {
    //        this.roleService = roleService;
    //    }

    @RequestMapping(value = "/role", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Role>> listRoles() {
        List<Role> roles = roleService.listRoles();
        if (roles.isEmpty()) {
            return new ResponseEntity<List<Role>>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<List<Role>>(roles, HttpStatus.OK);
    }

    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Role> getRole(@PathVariable("id") int id) {
        Role role = roleService.getRoleById(id);
        logger.info("Fetching Role with id " + id);
        if (role == null) {
            logger.info("Role with id " + id + " not found");
            return new ResponseEntity<Role>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Role>(role, HttpStatus.OK);
    }

    @RequestMapping(value = "/role/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> createRole(@RequestBody Role role, UriComponentsBuilder ucBuilder) {
        logger.info("Creating Role " + role.getName());
        if (roleService.isRoleExist(role)) {
            logger.info("A Role with name " + role.getName() + " already exist");
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }
        roleService.addRole(role);

        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ucBuilder.path("/role/{id}").buildAndExpand(role.getId()).toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Role> updateRole(@PathVariable("id") int id, @RequestBody Role role) {
        logger.info("Updating Role " + id);
        Role currentRole = roleService.getRoleById(id);

        if (currentRole == null) {
            logger.info("Role with id " + id + " not found");
            return new ResponseEntity<Role>(HttpStatus.NOT_FOUND);
        }

        currentRole.setName(role.getName());
        currentRole.setUser(role.getUser());

        roleService.updateRole(currentRole);
        return new ResponseEntity<Role>(currentRole, HttpStatus.OK);
    }

    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Role> deleteRole(@PathVariable("id") int id) {
        logger.info("Fetching & Deleting Role with id " + id);

        Role role = roleService.getRoleById(id);
        if (role == null) {
            logger.info("Unable to delete. Role with id " + id + " not found");
            return new ResponseEntity<Role>(HttpStatus.NOT_FOUND);
        }

        roleService.removeRole(id);
        return new ResponseEntity<Role>(HttpStatus.NO_CONTENT);
    }
}