package com.dwalldorf.timetrack.backend.rest.controller;

import static org.springframework.http.HttpStatus.OK;

import com.dwalldorf.timetrack.backend.annotation.RequireLogin;
import com.dwalldorf.timetrack.backend.exception.InvalidInputException;
import com.dwalldorf.timetrack.backend.rest.dto.LoginDto;
import com.dwalldorf.timetrack.backend.service.UserService;
import com.dwalldorf.timetrack.model.UserModel;
import javax.inject.Inject;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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;

@RestController
@RequestMapping(UserController.BASE_URI)
public class UserController extends BaseController {

    static final String BASE_URI = "/users";
    static final String URI_ME = "/me";
    static final String URI_LOGIN = "/login";
    static final String URI_LOGOUT = "/logout";

    private final UserService userService;

    @Inject
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public ResponseEntity<UserModel> register(@RequestBody @Valid UserModel user) {
        user = userService.register(user);
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

    @PostMapping(URI_LOGIN)
    public ResponseEntity<UserModel> login(@RequestBody LoginDto loginDto) throws InvalidInputException {
        UserModel user = userService.login(loginDto.getUsername(), loginDto.getPassword());
        if (user == null) {
            throw new InvalidInputException();
        }

        return new ResponseEntity<>(user, OK);
    }

    @GetMapping(URI_ME)
    @RequireLogin
    public UserModel getMe() {
        return this.getCurrentUser();
    }

    @CrossOrigin
    @PostMapping(URI_LOGOUT)
    @RequireLogin
    public ResponseEntity logout() {
        userService.logout();
        return new ResponseEntity(OK);
    }
}
@@ -563,68 +563,8 @@
 ty;%0A
-import org.springframework.web.bind.annotation.CrossOrigin;%0A
 impo
@@ -670,32 +670,91 @@
 on.PostMapping;%0A
+import org.springframework.web.bind.annotation.PutMapping;%0A
 import org.sprin
@@ -2078,19 +2078,269 @@
    @
-CrossOrigin
+PutMapping%0A    @RequireLogin%0A    public ResponseEntity%3CUserModel%3E update(@RequestBody UserModel user) %7B%0A        user.setId(userService.getCurrentUser().getId());%0A        user = this.userService.update(user);%0A        return new ResponseEntity%3C%3E(user, OK);%0A    %7D%0A
 %0A   

update user