package controllers;

import static spark.Spark.*;

import controllers.utils.Request;
import controllers.utils.Response;

import static services.SessionService.*;

public class SessionController {

  public static void init() {
    path("/sessions", () -> {

      before("/*", Request::requiresAuthentication);


      /*
      * Get all exercises
      */

      get("/:id", (req, res) -> {
        int id = Integer.parseInt(req.params(":id"));
        return Response.ok(res, getSessionById(id));
      });

      exception(NumberFormatException.class, (exception, request, response) ->
        Response.badRequest(response)
      );

      exception(SessionNotFound.class, (exception, request, response) ->
        Response.notFound(response)
      );

      /*
      * Create new session
      */

      post("",  (req, res) -> {
        Session session = Request.getBodyAs(req.body(), Session.class);
        Session createdSession = createSession(session);
        return Response.created(res, createdSession);
      });

      exception(SessionNotCreated.class, (exception, request, response) ->
        Response.internalServerError(response)
      );
    });
  }
}

@@ -15,16 +15,75 @@
 llers;%0A%0A
+import static services.UserService.getUserByStudentNumber;%0A
 import s
@@ -172,16 +172,47 @@
 esponse;
+%0Aimport services.UserService.*;
 %0A%0Aimport
@@ -990,16 +990,173 @@
 class);%0A
+%0A        String studentNumber = Request.getAuthIdentifier(req);%0A        User user = getUserByStudentNumber(studentNumber);%0A%0A        session.user = user.id;%0A%0A
         

use currently logged in user's id as the user for new session