/*
 * 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.daef.controllers;

import com.daef.models.ApplicationUser;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.daef.repositories.ApplicationUserRepository;
import com.daef.utils.AuthUtil;
import java.util.Date;
import java.util.List;
import org.json.simple.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

/**
 *
 * @author emilgras
 */

@RestController
@RequestMapping("/signup")
public class UserController {
    
    private ApplicationUserRepository userRepository;
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public UserController(ApplicationUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.userRepository = userRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }
    
    @GetMapping()
    public String hello() {
        return "hello";
    }
    
    
    @PostMapping()
    public ResponseEntity<JSONObject> signUp(@RequestBody ApplicationUser user) {

        // 1. validate credentials
        if (!AuthUtil.credentialsAreValid(user)) {
            return new ResponseEntity<>(AuthUtil.getErrorMessage(user), new HttpHeaders(), HttpStatus.BAD_REQUEST);
        }
        
        // 2. check if user already excists
        if (userRepository.findByUsername(user.getUsername()) != null) {
            JSONObject response = new JSONObject();
            response.put("message", "Username is already taken");
            return new ResponseEntity<>(response, new HttpHeaders(), HttpStatus.BAD_REQUEST);
        }
        
        // 3. hash password and update user
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        user.setCreatedAt(new Date());
        
        // 4. save user
        userRepository.save(user);

        JSONObject response = new JSONObject();
        response.put("message", "New user created");
        return new ResponseEntity<>(response, new HttpHeaders(), HttpStatus.OK);
    }
    
}

@@ -992,16 +992,77 @@
 Mapping;
+%0Aimport org.springframework.web.bind.annotation.RequestParam;
 %0A%0A/**%0A *
@@ -1125,14 +1125,12 @@
 g(%22/
-signup
+user
 %22)%0Ap
@@ -1540,47 +1540,448 @@
 lic 
-String hello() %7B%0A        return %22hello%22
+ResponseEntity%3CJSONObject%3E getProfile(@RequestParam(%22username%22) String username) %7B%0A        ApplicationUser user = userRepository.findByUsername(username);%0A        JSONObject jsonUser = new JSONObject();%0A        jsonUser.put(%22username%22, user.getUsername());%0A        jsonUser.put(%22karma%22, user.getKarma());%0A        jsonUser.put(%22createdAt%22, user.getCreatedAt());%0A        return new ResponseEntity%3C%3E(jsonUser, new HttpHeaders(), HttpStatus.OK)
 ;%0A  
@@ -2003,32 +2003,41 @@
    @PostMapping(
+%22/signup%22
 )%0A    public Res

task done