ca.qhrtech.controllers.UserController.java Source code

Java tutorial

Introduction

Here is the source code for ca.qhrtech.controllers.UserController.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 ca.qhrtech.controllers;

import ca.qhrtech.entities.BGLTable;
import ca.qhrtech.entities.BGLUser;
import ca.qhrtech.services.interfaces.TableService;
import ca.qhrtech.services.interfaces.UserService;
import java.time.LocalDateTime;
import java.util.List;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.pojo.ApiStage;
import org.jsondoc.core.pojo.ApiVisibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author bryan.bergen
 */
@Api(name = "User Services", description = "Methods for managing users", group = "Users", visibility = ApiVisibility.PUBLIC, stage = ApiStage.ALPHA)
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TableService tableService;

    @ApiMethod(description = "Search for a list of users. Passing no parameter returns all users")
    @RequestMapping("/user")
    public List<BGLUser> searchUsers(@RequestParam(value = "query", required = false) String query) {
        return userService.searchUsers(query);
    }

    @ApiMethod(description = "Retrieve a specific user")
    @RequestMapping("/user/{id}")
    public BGLUser getUser(@PathVariable("id") long id) {
        return userService.findUserById(id);
    }

    @ApiMethod(description = "Add a new User to BGL")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ResponseEntity<BGLUser> createUser(@RequestBody BGLUser user) {
        if (!userService.doesUserExist(user)) {
            user.setJoinDate(LocalDateTime.now());
            String hash = new BCryptPasswordEncoder().encode(user.getPassword());
            user.setPassword(hash);
            BGLUser newUser = userService.saveUser(user);
            return new ResponseEntity<>(newUser, HttpStatus.CREATED);
        }
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    }

    @ApiMethod(description = "Update the User at the specified location. Passed User should contain updated fields")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    public ResponseEntity<BGLUser> updateUser(@PathVariable("id") long id, @RequestBody BGLUser user) {
        BGLUser currentUser = userService.findUserById(id);
        if (currentUser == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        currentUser.setUsername(user.getUsername());
        currentUser.setAvatarPath(user.getAvatarPath());
        currentUser.setEmail(user.getEmail());

        userService.updateUser(currentUser);
        return new ResponseEntity<>(currentUser, HttpStatus.OK);
    }

    @ApiMethod(description = "Deletes the User at the specified location")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<BGLUser> deleteUser(@PathVariable("id") long id) {
        if (userService.findUserById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);

        }
        userService.deleteUser(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiMethod(description = "Retrieves a list of table invites for the User at the specified location")
    @RequestMapping("/user/{id}/invites")
    public ResponseEntity<List<BGLTable>> getTablesInvitedTo(@PathVariable("id") long id) {
        if (userService.findUserById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(tableService.getTablesInvitedTo(id), HttpStatus.OK);
    }
}