164 lines
4.4 KiB
Go
164 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type App struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
// Login Handler
|
|
func (app *App) LoginHandler(c echo.Context) error {
|
|
var req LoginRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
|
|
}
|
|
|
|
user, err := GetUserByUsername(app.DB, req.Username)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "invalid credentials")
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "invalid credentials")
|
|
}
|
|
|
|
token, err := createToken(user.ID, user.Username, user.IsAdmin)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "error creating token")
|
|
}
|
|
|
|
response := LoginResponse{
|
|
Token: token,
|
|
Username: user.Username,
|
|
IsAdmin: user.IsAdmin,
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// Schedule Handlers
|
|
func (app *App) GetSchedulesHandler(c echo.Context) error {
|
|
schedules, err := GetAllSchedules(app.DB)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return c.JSON(http.StatusOK, schedules)
|
|
}
|
|
|
|
func (app *App) CreateScheduleHandler(c echo.Context) error {
|
|
var schedule Schedule
|
|
if err := c.Bind(&schedule); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
|
|
}
|
|
|
|
if err := CreateSchedule(app.DB, &schedule); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, map[string]string{"message": "schedule created"})
|
|
}
|
|
|
|
func (app *App) DeleteScheduleHandler(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.QueryParam("id"))
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
|
|
}
|
|
|
|
if err := DeleteSchedule(app.DB, id); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
|
|
// 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 {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "error hashing password")
|
|
}
|
|
|
|
if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, map[string]string{"message": "user created"})
|
|
}
|
|
|
|
func (app *App) GetUsersHandler(c echo.Context) error {
|
|
users, err := GetAllUsers(app.DB)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return c.JSON(http.StatusOK, users)
|
|
}
|
|
|
|
func (app *App) DeleteUserHandler(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.QueryParam("id"))
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
|
|
}
|
|
|
|
if err := DeleteUser(app.DB, id); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
|
|
// Time Entry Handlers
|
|
func (app *App) CreateTimeEntryHandler(c echo.Context) error {
|
|
userID := c.Get("user_id").(int)
|
|
|
|
var entry TimeEntry
|
|
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 {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, map[string]string{"message": "time entry created"})
|
|
}
|
|
|
|
func (app *App) GetMyTimeEntriesHandler(c echo.Context) error {
|
|
userID := c.Get("user_id").(int)
|
|
|
|
entries, err := GetTimeEntriesByUser(app.DB, userID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, entries)
|
|
}
|
|
|
|
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 {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return c.JSON(http.StatusOK, hours)
|
|
}
|