be.bittich.quote.controller.impl.UserController.java Source code

Java tutorial

Introduction

Here is the source code for be.bittich.quote.controller.impl.UserController.java

Source

/*
 * Copyright 2014 nateriver.
 *
 * 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 be.bittich.quote.controller.impl;

import static be.bittich.quote.core.Constant.ROLE_USER;
import be.bittich.quote.exception.EntityExistException;
import be.bittich.quote.model.Role;
import be.bittich.quote.model.User;
import be.bittich.quote.service.FactoryService;
import be.bittich.quote.service.UserService;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author nateriver
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends AbstractController<User, Long> {

    private static final long serialVersionUID = -5455702081118476099L;
    private final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private FactoryService factoryService;

    @Override
    public UserService getService() {
        return this.factoryService.userService();
    }

    @Override
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public User create(@RequestBody @Valid User t) {
        if (t.getId() != null && getService().findOneById(t.getId()) != null) {
            throw new EntityExistException("Ajout impossible : l'entit existe dj!");
        }
        Role roleDefault = factoryService.roleService().findByRoleName(ROLE_USER);
        t.addRole(roleDefault);
        return this.getService().insert(t);
    }

    @Override
    public Logger getLogger() {
        return LOG;
    }
}