<commit_before>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)
      );
    });<commit_msg>use currently logged in user's id as the user for new session<commit_after>package controllers;

import static services.UserService.getUserByStudentNumber;
import static spark.Spark.*;

import controllers.utils.Request;
import controllers.utils.Response;
import services.UserService.*;

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);

        String studentNumber = Request.getAuthIdentifier(req);
        User user = getUserByStudentNumber(studentNumber);

        session.user = user.id;

        Session createdSession = createSession(session);
        return Response.created(res, createdSession);
      });

      exception(SessionNotCreated.class, (exception, request, response) ->
        Response.internalServerError(response)
      );
    });
{'fraction_non_alphanumeric': 0.12858740216175923, 'fraction_numerical': 0.0, 'mean_word_length': 2.867435158501441, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1684598', 'n_tokens_mistral': 714, 'n_tokens_neox': 683, 'n_words': 154}