commit 042274e955d63453638c3a48d9bfe4fca882249e
Author: Jiri Trefil <badjokke@gmail.com>
Date:   Sun Apr 2 21:49:09 2023 +0200

    new: #10244 Vytvoření controlleru pro modul správy uživatelů. Připavení service, entity a repository pro komunikaci s databází.

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java
new file mode 100644
index 0000000..b6dd69d
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java
@@ -0,0 +1,56 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * Controller for providing basic support for user management
+ * @version 1.0
+ * @author Vaclav Hrabik, Jiri Trefil
+ */
+@RestController
+@RequestMapping("v2/user")
+public class UserController {
+
+    /**
+     * Service for work with user management
+     */
+    @Autowired
+    private UserService userService;
+
+    /**
+     * Method to register new user in the app
+     * @param user  User who wants register in to the app
+     * @return      message
+     */
+    @PostMapping(value = "/register")
+    public String registerUser(@RequestBody User user) {
+        int statusCode = userService.registerUser(user);
+        return "user register";
+    }
+
+    /**
+     * Method to login user in to the app
+     * @param user  User who wants to login in to the app
+     * @return      message
+     */
+    @PostMapping(value = "/login")
+    public String loginUser(@RequestBody User user) {
+        int statusCode = userService.loginUser(user);
+        return "user logged";
+    }
+
+    /**
+     * Method to logout user from the app
+     * @param user  User who wants logout from the app
+     * @return      message after logout
+     */
+    @PostMapping(value = "/logout")
+    public String logoutUser(@RequestBody User user) {
+        int statusCode = userService.logoutUser(user);
+        return "user logout";
+    }
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java
new file mode 100644
index 0000000..f9d8680
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java
@@ -0,0 +1,78 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model;
+
+import javax.persistence.*;
+
+/**
+ * Model of database table represents one user
+ * @version 1.0
+ * @author Vaclav Hrabik, Jiri Trefil
+ */
+@Entity
+public class User {
+
+    /**
+     * Max length of column in the table
+     */
+    @Transient
+    private static final int MAX_COLUMN_LENGTH = 255;
+    /**
+     * Unique key of table
+     */
+    @Id
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    private int id;
+
+    /**
+     * Name of the User
+     */
+    @Column(unique = true,length = MAX_COLUMN_LENGTH)
+    private String name;
+
+    /**
+     * Email of the User
+     */
+    @Column(length = MAX_COLUMN_LENGTH)
+    private String email;
+
+
+    /**
+     * Getter of id
+     * @return id of the user
+     */
+    public int getId() {
+        return id;
+    }
+
+    /**
+     * Getter of name
+     * @return  name of the user
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Getter of email
+     * @return  email of the user
+     */
+    public String getEmail() {
+        return email;
+    }
+
+    /**
+     * Getter for email constraint
+     * @return  email constraint
+     */
+    public static int getEmailConstraint(){
+        return MAX_COLUMN_LENGTH;
+    }
+
+    /**
+     * Getter for name constraint
+     * @return  name constraint
+     */
+    public static int getNameConstraint(){
+        return MAX_COLUMN_LENGTH;
+    }
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/UserRepository.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/UserRepository.java
new file mode 100644
index 0000000..9df9155
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/UserRepository.java
@@ -0,0 +1,18 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * Interface as database object
+ * @version 1.0
+ * @author Vaclav Hrabik, Jiri Trefil
+ */
+@Repository
+public interface UserRepository extends JpaRepository<User, Integer> {
+    List<User> findUserByEmail(String email);
+    User findByName(String name);
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java
new file mode 100644
index 0000000..c9b0b9a
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java
@@ -0,0 +1,31 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+
+/**
+ * Interface implemented by service(s) which are responsible for user management (registration, login, logout)
+ * @version 1.0
+ * @author Vaclav Hrabik, Jiri Trefil
+ */
+public interface UserService {
+    /**
+     * Method attempts to register a user and returns status code indicating registration result
+     * @param user serialized JSON object representing user
+     * @return Integer - status code of the operation, ie 1 - successful, 0 - name taken, ...
+     */
+    public int registerUser(User user);
+    /**
+     * Method attempts to log in a user and returns status code indicating login result
+     * @param user serialized JSON object representing user
+     * @return Integer - status code of the operation, ie 1 - successful, 0 - failed, ....
+     */
+    public int loginUser(User user);
+    /**
+     * Method attempts to log out a user
+     * @param user serialized JSON object representing user
+     * @return Integer - status code of the operation, ie 1 - successful, 0 - failed, ....
+     */
+    public int logoutUser(User user);
+
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java
new file mode 100644
index 0000000..bf584f8
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java
@@ -0,0 +1,71 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Service provides functionality for user login/register/logout
+ * @author Vaclav Hrabik, Jiri Trefil
+ */
+@Service
+public class UserServiceImpl implements UserService {
+
+    @Autowired
+    private UserRepository userRepository;
+
+    /**
+     * Method attempts to register a user
+     * @param user serialized JSON object representing user
+     * @return Integer flag:
+     *  1 - if the user is registered successfully
+     *  0 - if the user request violates constraints of column(s)
+     *  -1 - if the username is already taken
+     *
+     */
+    @Override
+    public int registerUser(User user) {
+        final String email = user.getEmail();
+        final String name = user.getName();
+        //if client request violates constraints - kill the request. User will not be registered.
+        if (email.length() > User.getEmailConstraint() || name.length() > User.getNameConstraint()) {
+            return 0;
+        }
+        //if the username is taken, kill the request aswell
+        if (userRepository.findByName(user.getName()) != null) {
+            return -1;
+        }
+        //save the user
+        User u = userRepository.save(user);
+
+        //TODO request to OAuth for token - send user info to the oauth app for token
+        //return okay status code, the user was created
+        return 1;
+    }
+
+    /**
+     * Method
+     * @param user serialized JSON object representing user
+     * @return Integer Flag:
+     *      0 - if username is not registered
+     *      1 - if user is logged successfully
+     */
+    @Override
+    public int loginUser(User user) {
+        User u = userRepository.findByName(user.getName());
+        //TODO request to OAuth for authentication
+        return u == null ? 0 : 1;
+    }
+
+    /**
+     * Methods invalidates JWT token in OAuth application
+     * @param user serialized JSON object representing user
+     * @return
+     */
+    @Override
+    public int logoutUser(User user) {
+        //TODO with OAuth app
+        return 0;
+    }
+}
