<commit_before>package api

import (
	"log"
	"net/http"

	"github.com/labstack/echo"
)

// structs for data
type HomeworkClass struct {
	ID int `json:"id"`
	Name string `json:"name"`
	Teacher string `json:"teacher"`
	UserID int `json:"userId"`
}
// responses
type ClassResponse struct {
	Status string `json:"status"`
	Classes []HomeworkClass `json:"classes"`
}
type SingleClassResponse struct {
	Status string `json:"status"`
	Class HomeworkClass `json:"class"`
}

func InitClassesAPI(e *echo.Echo) {
	e.GET("/classes/get", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		rows, err := DB.Query("SELECT id, name, teacher, userId FROM classes WHERE userId = ?", GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while getting class information: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer rows.Close()

		classes := []HomeworkClass{}
		for rows.Next() {
			resp := HomeworkClass{-1, "", "", -1}
			rows.Scan(&resp.ID, &resp.Name, &resp.Teacher, &resp.UserID)
			classes = append(classes, resp)
		}
		jsonResp := ClassResponse{"ok", classes}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.GET("/classes/get/:id", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		rows, err := DB.Query("SELECT id, name, teacher, userId FROM classes WHERE id = ? AND userId = ?", c.Param("id"), GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while getting class information: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer rows.Close()

		if !rows.Next() {
			jsonResp := ErrorResponse{"error", "Invalid ID."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}
		resp := HomeworkClass{-1, "", "", -1}
		rows.Scan(&resp.ID, &resp.Name, &resp.Teacher, &resp.UserID)
		jsonResp := SingleClassResponse{"ok", resp}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.POST("/classes/add", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		if c.FormValue("name") == "" {
			jsonResp := ErrorResponse{"error", "Name is required."}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}

		stmt, err := DB.Prepare("INSERT INTO classes(name, teacher, userId) VALUES(?, ?, ?)")
		if err != nil {
			log.Println("Error while adding class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		_, err = stmt.Exec(c.FormValue("name"), c.FormValue("teacher"), GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while adding class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		jsonResp := StatusResponse{"ok"}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.POST("/classes/edit", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		if c.FormValue("id") == "" || c.FormValue("name") == "" || c.FormValue("teacher") == "" {
			jsonResp := ErrorResponse{"error", "Missing required parameters."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}

		// check if you are allowed to edit the given id
		idRows, err := DB.Query("SELECT id FROM classes WHERE userId = ? AND id = ?", GetSessionUserID(&c), c.FormValue("id"))
		if err != nil {
			log.Println("Error while editing classes: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer idRows.Close()
		if !idRows.Next() {
			jsonResp := ErrorResponse{"error", "Invalid ID."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}

		stmt, err := DB.Prepare("UPDATE classes SET name = ?, teacher = ? WHERE id = ?")
		if err != nil {
			log.Println("Error while editing class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		_, err = stmt.Exec(c.FormValue("name"), c.FormValue("teacher"), c.FormValue("id"))
		if err != nil {
			log.Println("Error while editing class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		jsonResp := StatusResponse{"ok"}
		return c.JSON(http.StatusOK, jsonResp)
	})
}
<commit_msg>teacher should be optional for classes<commit_after>package api

import (
	"log"
	"net/http"

	"github.com/labstack/echo"
)

// structs for data
type HomeworkClass struct {
	ID int `json:"id"`
	Name string `json:"name"`
	Teacher string `json:"teacher"`
	UserID int `json:"userId"`
}
// responses
type ClassResponse struct {
	Status string `json:"status"`
	Classes []HomeworkClass `json:"classes"`
}
type SingleClassResponse struct {
	Status string `json:"status"`
	Class HomeworkClass `json:"class"`
}

func InitClassesAPI(e *echo.Echo) {
	e.GET("/classes/get", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		rows, err := DB.Query("SELECT id, name, teacher, userId FROM classes WHERE userId = ?", GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while getting class information: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer rows.Close()

		classes := []HomeworkClass{}
		for rows.Next() {
			resp := HomeworkClass{-1, "", "", -1}
			rows.Scan(&resp.ID, &resp.Name, &resp.Teacher, &resp.UserID)
			classes = append(classes, resp)
		}
		jsonResp := ClassResponse{"ok", classes}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.GET("/classes/get/:id", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		rows, err := DB.Query("SELECT id, name, teacher, userId FROM classes WHERE id = ? AND userId = ?", c.Param("id"), GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while getting class information: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer rows.Close()

		if !rows.Next() {
			jsonResp := ErrorResponse{"error", "Invalid ID."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}
		resp := HomeworkClass{-1, "", "", -1}
		rows.Scan(&resp.ID, &resp.Name, &resp.Teacher, &resp.UserID)
		jsonResp := SingleClassResponse{"ok", resp}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.POST("/classes/add", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		if c.FormValue("name") == "" {
			jsonResp := ErrorResponse{"error", "Name is required."}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}

		stmt, err := DB.Prepare("INSERT INTO classes(name, teacher, userId) VALUES(?, ?, ?)")
		if err != nil {
			log.Println("Error while adding class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		_, err = stmt.Exec(c.FormValue("name"), c.FormValue("teacher"), GetSessionUserID(&c))
		if err != nil {
			log.Println("Error while adding class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		jsonResp := StatusResponse{"ok"}
		return c.JSON(http.StatusOK, jsonResp)
	})

	e.POST("/classes/edit", func(c echo.Context) error {
		if GetSessionUserID(&c) == -1 {
			jsonResp := ErrorResponse{"error", "logged_out"}
			return c.JSON(http.StatusUnauthorized, jsonResp)
		}
		if c.FormValue("id") == "" || c.FormValue("name") == "" {
			jsonResp := ErrorResponse{"error", "Missing required parameters."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}

		// check if you are allowed to edit the given id
		idRows, err := DB.Query("SELECT id FROM classes WHERE userId = ? AND id = ?", GetSessionUserID(&c), c.FormValue("id"))
		if err != nil {
			log.Println("Error while editing classes: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		defer idRows.Close()
		if !idRows.Next() {
			jsonResp := ErrorResponse{"error", "Invalid ID."}
			return c.JSON(http.StatusBadRequest, jsonResp)
		}

		stmt, err := DB.Prepare("UPDATE classes SET name = ?, teacher = ? WHERE id = ?")
		if err != nil {
			log.Println("Error while editing class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		_, err = stmt.Exec(c.FormValue("name"), c.FormValue("teacher"), c.FormValue("id"))
		if err != nil {
			log.Println("Error while editing class: ")
			log.Println(err)
			jsonResp := StatusResponse{"error"}
			return c.JSON(http.StatusInternalServerError, jsonResp)
		}
		jsonResp := StatusResponse{"ok"}
		return c.JSON(http.StatusOK, jsonResp)
	})
}

{'fraction_non_alphanumeric': 0.1609962904080551, 'fraction_numerical': 0.001695813460519343, 'mean_word_length': 4.26856504745952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3984548', 'n_tokens_mistral': 3526, 'n_tokens_neox': 3139, 'n_words': 712}