package squire.Networking;

import squire.Main;
import squire.Users.Session;
import squire.Users.User;

import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import java.sql.SQLException;

/**
 * The request handler dealing with all things Users
 */
@Route("user/")
class UserRequestHandler
{
    @Route("register")
    static void register(Request req, Response res)
    {
        // If there is no user with this username and email.
        if (User.find.where().username.equalTo((String)req.get("username")).and().email.equalTo((String)req.get("email")).findUnique() == null)
        {
            User u = new User((String)req.get("username"), (String)req.get("email"), (String)req.get("password"));
            u.save();
        }
        else
        {
            res.setFail();
        }
    }

    @Route("login")
    static void login(Request req, Response res)
    {
        User u = User.find.authenticate((String)req.get("username"), (String) req.get("password"));
        if (u == null)
        {
            res.setFail();
        }
        res.set("sessionID", Session.login(u).getToken());
        res.set("userName", u.getUsername());
        res.set("userID", u.getId());
    }

    @Route("getUsernameFromSessionId")
    static void getUsernameFromSessionId(Request req, Response res)
    {
        User u = Session.find.activeSession((String) req.get("sessionID")).getUser();
        if (u != null)
        {
            res.set("username", u.getUsername());
        }
    }
}


@@ -1533,11 +1533,269 @@
 %7D%0A    %7D%0A
+%0A    @Route(%22ChangePassword%22)%0A    static void ChangePassword(Request req, Response res)%0A    %7B%0A        User u = Session.find.activeSession((String) req.get(%22sessionID%22)).getUser();%0A        u.setPassword((String) req.get(%22password%22));%0A        u.save();%0A    %7D%0A%0A
 %7D%0A%0A

Change Password functionality added (actually)