feat: change from Standart Library to echo framework to simplify development

This commit is contained in:
Patryk Hegenberg 2025-11-05 07:46:18 +01:00
parent d74046522b
commit 4514ce44a2
6 changed files with 254 additions and 275 deletions

View file

@ -2,11 +2,10 @@ package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
@ -14,28 +13,25 @@ type App struct {
DB *sql.DB
}
func (app *App) LoginHandler(w http.ResponseWriter, r *http.Request) {
// Login Handler
func (app *App) LoginHandler(c echo.Context) error {
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
user, err := GetUserByUsername(app.DB, req.Username)
if err != nil {
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
return
return echo.NewHTTPError(http.StatusUnauthorized, "invalid credentials")
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
return
return echo.NewHTTPError(http.StatusUnauthorized, "invalid credentials")
}
token, err := createToken(user.ID, user.Username, user.IsAdmin)
if err != nil {
http.Error(w, "Error creating token", http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, "error creating token")
}
response := LoginResponse{
@ -44,179 +40,125 @@ func (app *App) LoginHandler(w http.ResponseWriter, r *http.Request) {
IsAdmin: user.IsAdmin,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return c.JSON(http.StatusOK, response)
}
func (app *App) GetSchedulesHandler(w http.ResponseWriter, r *http.Request) {
// Schedule Handlers
func (app *App) GetSchedulesHandler(c echo.Context) error {
schedules, err := GetAllSchedules(app.DB)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(schedules)
return c.JSON(http.StatusOK, schedules)
}
func (app *App) CreateScheduleHandler(w http.ResponseWriter, r *http.Request) {
func (app *App) CreateScheduleHandler(c echo.Context) error {
var schedule Schedule
if err := json.NewDecoder(r.Body).Decode(&schedule); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
if err := c.Bind(&schedule); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
if err := CreateSchedule(app.DB, &schedule); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.WriteHeader(http.StatusCreated)
return c.JSON(http.StatusCreated, map[string]string{"message": "schedule created"})
}
func (app *App) DeleteScheduleHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
id, err := strconv.Atoi(idStr)
func (app *App) DeleteScheduleHandler(c echo.Context) error {
id, err := strconv.Atoi(c.QueryParam("id"))
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}
if err := DeleteSchedule(app.DB, id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.WriteHeader(http.StatusOK)
return c.NoContent(http.StatusOK)
}
func (app *App) CreateUserHandler(w http.ResponseWriter, r *http.Request) {
var req struct {
Username string `json:"username"`
Password string `json:"password"`
IsAdmin bool `json:"is_admin"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
// User Handlers
func (app *App) CreateUserHandler(c echo.Context) error {
var req CreateUserRequest
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
http.Error(w, "Error hashing password", http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, "error hashing password")
}
if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.WriteHeader(http.StatusCreated)
return c.JSON(http.StatusCreated, map[string]string{"message": "user created"})
}
func (app *App) GetUsersHandler(w http.ResponseWriter, r *http.Request) {
func (app *App) GetUsersHandler(c echo.Context) error {
users, err := GetAllUsers(app.DB)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
return c.JSON(http.StatusOK, users)
}
// func (app *App) CreateTimeEntryHandler(w http.ResponseWriter, r *http.Request) {
// userIDStr := r.Header.Get("X-User-ID")
// userID, _ := strconv.Atoi(userIDStr)
// var entry TimeEntry
// if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
// entry.UserID = userID
// if err := CreateTimeEntry(app.DB, &entry); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// w.WriteHeader(http.StatusCreated)
// }
func (app *App) GetMyTimeEntriesHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := r.Header.Get("X-User-ID")
userID, _ := strconv.Atoi(userIDStr)
entries, err := GetTimeEntriesByUser(app.DB, userID)
func (app *App) DeleteUserHandler(c echo.Context) error {
id, err := strconv.Atoi(c.QueryParam("id"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(entries)
}
func (app *App) GetAllTimeEntriesHandler(w http.ResponseWriter, r *http.Request) {
entries, err := GetAllTimeEntries(app.DB)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
if err := DeleteUser(app.DB, id); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(entries)
return c.NoContent(http.StatusOK)
}
func (app *App) CreateTimeEntryHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := r.Header.Get("X-User-ID")
userID, _ := strconv.Atoi(userIDStr)
// Time Entry Handlers
func (app *App) CreateTimeEntryHandler(c echo.Context) error {
userID := c.Get("user_id").(int)
var entry TimeEntry
if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
log.Print("Error on Decoding occured")
http.Error(w, err.Error(), http.StatusBadRequest)
return
if err := c.Bind(&entry); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
entry.UserID = userID
if err := CreateTimeEntry(app.DB, &entry); err != nil {
log.Print("Error on creating time entry in Database occured")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.WriteHeader(http.StatusCreated)
return c.JSON(http.StatusCreated, map[string]string{"message": "time entry created"})
}
func (app *App) DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
id, err := strconv.Atoi(idStr)
func (app *App) GetMyTimeEntriesHandler(c echo.Context) error {
userID := c.Get("user_id").(int)
entries, err := GetTimeEntriesByUser(app.DB, userID)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if err := DeleteUser(app.DB, id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
return c.JSON(http.StatusOK, entries)
}
func (app *App) GetWeeklyHoursHandler(w http.ResponseWriter, r *http.Request) {
func (app *App) GetAllTimeEntriesHandler(c echo.Context) error {
entries, err := GetAllTimeEntries(app.DB)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, entries)
}
func (app *App) GetWeeklyHoursHandler(c echo.Context) error {
hours, err := GetWeeklyHours(app.DB)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(hours)
return c.JSON(http.StatusOK, hours)
}