package com.tlongdev.spicio.controller;

import com.tlongdev.spicio.domain.User;
import com.tlongdev.spicio.storage.dao.SequenceDao;
import com.tlongdev.spicio.storage.dao.UserDao;
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.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

/**
 * @author Long
 * @since 2016. 03. 15.
 */
@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @Autowired private UserDao userDao;
    @Autowired private SequenceDao sequenceDao;

    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public ResponseEntity<User> getUser(@PathVariable long userId) {
        User user = userDao.getUser(userId);
        if (user == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addUser(@RequestBody User user) {
        if (user.getFacebookId() != null) {
            User result = userDao.getUserByFacebookId(user.getFacebookId());
            if (result == null) {
                user.setId(sequenceDao.nextValue("user"));
                result = userDao.saveUser(user);
            }
            return mapToResponseEntity(result);
        } else if (user.getGoogleId() != null) {
            User result = userDao.getUserByGoogleId(user.getGoogleId());
            if (result == null) {
                user.setId(sequenceDao.nextValue("user"));
                result = userDao.saveUser(user);
            }
            return mapToResponseEntity(result);
        }
        return new ResponseEntity<>(null, null, HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<?> mapToResponseEntity(User user) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(ServletUriComponentsBuilder
                .fromCurrentRequest().path("/{id}")
                .buildAndExpand(user.getId()).toUri());
        return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
    }
}

@@ -241,53 +241,8 @@
 ed;%0A
-import org.springframework.http.HttpHeaders;%0A
 impo
@@ -456,16 +456,38 @@
 ilder;%0A%0A
+import java.net.URI;%0A%0A
 /**%0A * @
@@ -917,36 +917,32 @@
          return 
-new 
 ResponseEntity%3C%3E
@@ -939,18 +939,23 @@
 seEntity
-%3C%3E
+.status
 (HttpSta
@@ -968,16 +968,27 @@
 T_FOUND)
+.body(null)
 ;%0A      
@@ -998,36 +998,32 @@
 %0A        return 
-new 
 ResponseEntity%3C%3E
@@ -1024,30 +1024,16 @@
 tity
-%3C%3E
+.ok
 (user
-, HttpStatus.OK
 );%0A 
@@ -1818,36 +1818,32 @@
 %0A        return 
-new 
 ResponseEntity%3C%3E
@@ -1844,45 +1844,31 @@
 tity
-%3C%3E(null, null, HttpStatus.BAD_REQUEST
+.badRequest().body(null
 );%0A 
@@ -1948,85 +1948,26 @@
     
-HttpHeaders httpHeaders = new HttpHeaders();%0A        httpHeaders.setL
+URI l
 ocation
-(
+Uri = 
 Serv
@@ -2095,17 +2095,16 @@
 .toUri()
-)
 ;%0A      
@@ -2116,12 +2116,8 @@
 urn 
-new 
 Resp
@@ -2130,48 +2130,39 @@
 tity
-%3C%3E(null, httpHeaders, HttpStatus.CREATED
+.created(locationUri).body(null
 );%0A 

use helper methods