Search in sources :

Example 6 with BadRequestAlertException

use of io.github.jhipster.sample.web.rest.errors.BadRequestAlertException in project jhipster-sample-app-hazelcast by jhipster.

the class OperationResource method createOperation.

/**
 * POST  /operations : Create a new operation.
 *
 * @param operation the operation to create
 * @return the ResponseEntity with status 201 (Created) and with body the new operation, or with status 400 (Bad Request) if the operation has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/operations")
@Timed
public ResponseEntity<Operation> createOperation(@Valid @RequestBody Operation operation) throws URISyntaxException {
    log.debug("REST request to save Operation : {}", operation);
    if (operation.getId() != null) {
        throw new BadRequestAlertException("A new operation cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Operation result = operationRepository.save(operation);
    return ResponseEntity.created(new URI("/api/operations/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) Operation(io.github.jhipster.sample.domain.Operation) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Example 7 with BadRequestAlertException

use of io.github.jhipster.sample.web.rest.errors.BadRequestAlertException in project jhipster-sample-app-hazelcast by jhipster.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param userDTO the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO) throws URISyntaxException {
    log.debug("REST request to save User : {}", userDTO);
    if (userDTO.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(userDTO);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) User(io.github.jhipster.sample.domain.User) LoginAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Example 8 with BadRequestAlertException

use of io.github.jhipster.sample.web.rest.errors.BadRequestAlertException in project jhipster-sample-app-elasticsearch by jhipster.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param userDTO the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO) throws URISyntaxException {
    log.debug("REST request to save User : {}", userDTO);
    if (userDTO.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(userDTO);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) User(io.github.jhipster.sample.domain.User) LoginAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Example 9 with BadRequestAlertException

use of io.github.jhipster.sample.web.rest.errors.BadRequestAlertException in project jhipster-sample-app-elasticsearch by jhipster.

the class LabelResource method createLabel.

/**
 * POST  /labels : Create a new label.
 *
 * @param label the label to create
 * @return the ResponseEntity with status 201 (Created) and with body the new label, or with status 400 (Bad Request) if the label has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/labels")
@Timed
public ResponseEntity<Label> createLabel(@Valid @RequestBody Label label) throws URISyntaxException {
    log.debug("REST request to save Label : {}", label);
    if (label.getId() != null) {
        throw new BadRequestAlertException("A new label cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Label result = labelRepository.save(label);
    labelSearchRepository.save(result);
    return ResponseEntity.created(new URI("/api/labels/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) Label(io.github.jhipster.sample.domain.Label) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Example 10 with BadRequestAlertException

use of io.github.jhipster.sample.web.rest.errors.BadRequestAlertException in project jhipster-sample-app-elasticsearch by jhipster.

the class OperationResource method createOperation.

/**
 * POST  /operations : Create a new operation.
 *
 * @param operation the operation to create
 * @return the ResponseEntity with status 201 (Created) and with body the new operation, or with status 400 (Bad Request) if the operation has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/operations")
@Timed
public ResponseEntity<Operation> createOperation(@Valid @RequestBody Operation operation) throws URISyntaxException {
    log.debug("REST request to save Operation : {}", operation);
    if (operation.getId() != null) {
        throw new BadRequestAlertException("A new operation cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Operation result = operationRepository.save(operation);
    operationSearchRepository.save(result);
    return ResponseEntity.created(new URI("/api/operations/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) Operation(io.github.jhipster.sample.domain.Operation) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)16 BadRequestAlertException (io.github.jhipster.sample.web.rest.errors.BadRequestAlertException)16 URI (java.net.URI)16 User (io.github.jhipster.sample.domain.User)6 EmailAlreadyUsedException (io.github.jhipster.sample.web.rest.errors.EmailAlreadyUsedException)6 LoginAlreadyUsedException (io.github.jhipster.sample.web.rest.errors.LoginAlreadyUsedException)6 Secured (org.springframework.security.access.annotation.Secured)6 BankAccount (io.github.jhipster.sample.domain.BankAccount)4 Label (io.github.jhipster.sample.domain.Label)3 Operation (io.github.jhipster.sample.domain.Operation)3 BankAccountDTO (io.github.jhipster.sample.service.dto.BankAccountDTO)1 LabelDTO (io.github.jhipster.sample.service.dto.LabelDTO)1 OperationDTO (io.github.jhipster.sample.service.dto.OperationDTO)1