com.tamnd.app.rest.controller.AccountController.java Source code

Java tutorial

Introduction

Here is the source code for com.tamnd.app.rest.controller.AccountController.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.tamnd.app.rest.controller;

import com.tamnd.app.core.entities.Account;
import com.tamnd.app.core.entities.AccountRole;
import com.tamnd.app.core.services.AccountService;
import com.tamnd.app.core.services.exceptions.AccountExistsException;
import com.tamnd.app.core.services.util.AccountList;
import com.tamnd.app.rest.exceptions.ConflictException;
import com.tamnd.app.rest.resources.AccountListResource;
import com.tamnd.app.rest.resources.AccountResource;
import com.tamnd.app.rest.resources.asm.AccountListResourceAsm;
import com.tamnd.app.rest.resources.asm.AccountResourceAsm;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
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 tamnd
 */
@RestController
@RequestMapping("/rest/accounts")
public class AccountController {

    private final AccountService accountService;

    @Autowired
    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public ResponseEntity<AccountResource> getUserInfo(Principal user) {
        Account account = accountService.findByAccountName(user.getName());
        if (account != null) {
            AccountResource res = new AccountResourceAsm().toResource(account);
            return new ResponseEntity<>(res, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @RequestMapping(value = "/{accountId}", method = RequestMethod.GET)
    public ResponseEntity<AccountResource> getAccount(@PathVariable Integer accountId) {
        Account account = accountService.findAccount(accountId);
        if (account != null) {
            AccountResource res = new AccountResourceAsm().toResource(account);
            return new ResponseEntity<>(res, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<AccountListResource> findAllAccounts(
            @RequestParam(value = "name", required = false) String name) {
        AccountList list;
        if (name == null) {
            list = accountService.findAllAccounts();
        } else {
            Account account = accountService.findByAccountName(name);
            if (account == null) {
                list = new AccountList(new ArrayList<Account>());
            } else {
                list = new AccountList(Arrays.asList(account));
            }
        }
        AccountListResource res = new AccountListResourceAsm().toResource(list);
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<AccountResource> createAccount(@RequestBody AccountResource sentAccount) {
        try {
            //Encode BCrypt password
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String hashedPassword = passwordEncoder.encode(sentAccount.getPassword());
            sentAccount.setPassword(hashedPassword);

            Account newAccount = sentAccount.toAccount();
            Set<AccountRole> roles = new HashSet(0);
            roles.add(new AccountRole(newAccount, "ROLE_USER"));
            newAccount.setUserRole(roles);

            Account account = accountService.createAccount(newAccount);
            AccountResource res = new AccountResourceAsm().toResource(account);
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(URI.create(res.getLink("self").getHref()));
            return new ResponseEntity<>(res, headers, HttpStatus.CREATED);
        } catch (AccountExistsException ex) {
            throw new ConflictException(ex);
        }
    }
}