/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package es.esky.rol.users.api.controller;

import es.esky.rol.arch.domain.ApiError;
import es.esky.rol.arch.pagination.PaginationUtils;
import es.esky.rol.users.api.exception.UserNotFoundException;
import es.esky.rol.users.domain.User;
import es.esky.rol.users.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

/**
 * User resource controller.
 *
 * @author Cristian Mateos López
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = UsersController.BASE_URL)
public class UsersController {
    static final String BASE_URL = "/users";

    private final UsersService usersService;

    /**
     * Constructor of UsersController.
     *
     * @param usersService Users service.
     */
    @Autowired
    public UsersController(UsersService usersService) {
        this.usersService = usersService;
    }

    /**
     * Find a page of users by a criteria.
     *
     * @param page               Requested page.
     * @param resourcesAssembler Link assembler.
     * @return Page requested.
     */
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity findByCriteria(Pageable page, PagedResourcesAssembler<User> resourcesAssembler) {
        Page<User> userPage = usersService.findByCriteria(page);
        Link link = linkTo(methodOn(UsersController.class).findByCriteria(page, resourcesAssembler)).withSelfRel();
        return PaginationUtils.buildResponse(userPage, resourcesAssembler, link);
    }

    /**
     * Find an user by a username.
     *
     * @param username Unique username.
     * @return User requested.
     */
    @RequestMapping(value = "/{username}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<User> findByUsername(@PathVariable("username") String username) {
        User user = usersService.findByUsername(username);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    /**
     * Delete a user.
     *
     * @param username User's username that will be deleted.
     */
    @RequestMapping(value = "/{username}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteByUsername(@PathVariable("username") String username) {
        usersService.deleteByUsername(username);
    }

    /**
     * Save a new user.
     *
     * @param user User will be saved.
     * @return The user after save it.
     */
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<User> save(@RequestBody @Validated User user) {
        User userCreated = usersService.save(user);
        return new ResponseEntity<>(userCreated, HttpStatus.CREATED);
    }

    /**
     * Handler {@link UserNotFoundException}.
     *
     * @param e Exception will be handled.
     * @return Api error response.
     */
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<ApiError> userNotFound(UserNotFoundException e) {
        ApiError error = new ApiError();
        error.setCode("user_not_found");
        error.setMessage(e.getMessage());
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }
}

@@ -2733,1778 +2733,7 @@
    %7D
-%0A%0A    /**%0A     * Find an user by a username.%0A     *%0A     * @param username Unique username.%0A     * @return User requested.%0A     */%0A    @RequestMapping(value = %22/%7Busername%7D%22, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)%0A    public ResponseEntity%3CUser%3E findByUsername(@PathVariable(%22username%22) String username) %7B%0A        User user = usersService.findByUsername(username);%0A        return new ResponseEntity%3C%3E(user, HttpStatus.OK);%0A    %7D%0A%0A    /**%0A     * Delete a user.%0A     *%0A     * @param username User's username that will be deleted.%0A     */%0A    @RequestMapping(value = %22/%7Busername%7D%22, method = RequestMethod.DELETE)%0A    @ResponseStatus(HttpStatus.NO_CONTENT)%0A    public void deleteByUsername(@PathVariable(%22username%22) String username) %7B%0A        usersService.deleteByUsername(username);%0A    %7D%0A%0A    /**%0A     * Save a new user.%0A     *%0A     * @param user User will be saved.%0A     * @return The user after save it.%0A     */%0A    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)%0A    public ResponseEntity%3CUser%3E save(@RequestBody @Validated User user) %7B%0A        User userCreated = usersService.save(user);%0A        return new ResponseEntity%3C%3E(userCreated, HttpStatus.CREATED);%0A    %7D%0A%0A    /**%0A     * Handler %7B@link UserNotFoundException%7D.%0A     *%0A     * @param e Exception will be handled.%0A     * @return Api error response.%0A     */%0A    @ExceptionHandler(UserNotFoundException.class)%0A    public ResponseEntity%3CApiError%3E userNotFound(UserNotFoundException e) %7B%0A        ApiError error = new ApiError();%0A        error.setCode(%22user_not_found%22);%0A        error.setMessage(e.getMessage());%0A        return new ResponseEntity%3C%3E(error, HttpStatus.NOT_FOUND);%0A    %7D
 %0A%7D%0A

Remove not tested endpoints