feat: add Logging Middleware, saving Token and better Handling

This commit is contained in:
Patryk Hegenberg 2025-11-05 07:16:38 +01:00
parent 2c4fc7869a
commit d74046522b
8 changed files with 926 additions and 236 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"strconv"
@ -126,25 +127,25 @@ func (app *App) GetUsersHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(users)
}
func (app *App) CreateTimeEntryHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := r.Header.Get("X-User-ID")
userID, _ := strconv.Atoi(userIDStr)
// 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
}
// var entry TimeEntry
// if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
entry.UserID = userID
// entry.UserID = userID
if err := CreateTimeEntry(app.DB, &entry); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// if err := CreateTimeEntry(app.DB, &entry); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
w.WriteHeader(http.StatusCreated)
}
// w.WriteHeader(http.StatusCreated)
// }
func (app *App) GetMyTimeEntriesHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := r.Header.Get("X-User-ID")
@ -170,3 +171,52 @@ func (app *App) GetAllTimeEntriesHandler(w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(entries)
}
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 {
log.Print("Error on Decoding occured")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
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
}
w.WriteHeader(http.StatusCreated)
}
func (app *App) DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
if err := DeleteUser(app.DB, id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (app *App) GetWeeklyHoursHandler(w http.ResponseWriter, r *http.Request) {
hours, err := GetWeeklyHours(app.DB)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(hours)
}