package main import ( "database/sql" "encoding/json" "log" "net/http" "strconv" "golang.org/x/crypto/bcrypt" ) type App struct { DB *sql.DB } func (app *App) LoginHandler(w http.ResponseWriter, r *http.Request) { var req LoginRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } user, err := GetUserByUsername(app.DB, req.Username) if err != nil { http.Error(w, "Invalid credentials", http.StatusUnauthorized) return } if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil { http.Error(w, "Invalid credentials", http.StatusUnauthorized) return } token, err := createToken(user.ID, user.Username, user.IsAdmin) if err != nil { http.Error(w, "Error creating token", http.StatusInternalServerError) return } response := LoginResponse{ Token: token, Username: user.Username, IsAdmin: user.IsAdmin, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) } func (app *App) GetSchedulesHandler(w http.ResponseWriter, r *http.Request) { schedules, err := GetAllSchedules(app.DB) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(schedules) } func (app *App) CreateScheduleHandler(w http.ResponseWriter, r *http.Request) { var schedule Schedule if err := json.NewDecoder(r.Body).Decode(&schedule); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } if err := CreateSchedule(app.DB, &schedule); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) } func (app *App) DeleteScheduleHandler(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 := DeleteSchedule(app.DB, id); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(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 } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost) if err != nil { http.Error(w, "Error hashing password", http.StatusInternalServerError) return } if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) } func (app *App) GetUsersHandler(w http.ResponseWriter, r *http.Request) { users, err := GetAllUsers(app.DB) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") 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) // 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) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } 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 } 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) }