feat: added all features for seconde release candidate

This commit is contained in:
Patryk Hegenberg 2025-11-05 23:39:51 +01:00
parent 9c25956711
commit e65ba85c43
7 changed files with 1947 additions and 850 deletions

View file

@ -14,7 +14,6 @@ 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 {
@ -44,7 +43,6 @@ func (app *App) LoginHandler(c echo.Context) error {
return c.JSON(http.StatusOK, response)
}
// Schedule Handlers
func (app *App) GetSchedulesHandler(c echo.Context) error {
schedules, err := GetAllSchedules(app.DB)
if err != nil {
@ -76,33 +74,62 @@ func (app *App) DeleteScheduleHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
return c.NoContent(http.StatusNoContent)
}
// // 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")
// }
func (app *App) GetYearlyHoursSummaryHandler(c echo.Context) error {
hours, err := GetYearlyHoursSummary(app.DB)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if hours == nil {
hours = []WeeklyHours{}
}
return c.JSON(http.StatusOK, hours)
}
// hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, "error hashing password")
// }
func (app *App) AdminCreateTimeEntryHandler(c echo.Context) error {
isAdmin, _ := c.Get("is_admin").(bool)
// if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin); err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// }
if !isAdmin {
return echo.NewHTTPError(http.StatusForbidden, "Only admins can create entries for others")
}
// return c.JSON(http.StatusCreated, map[string]string{"message": "user created"})
// }
var req struct {
UserID int `json:"user_id"`
Date string `json:"date"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Type string `json:"type"`
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
entry := TimeEntry{
UserID: req.UserID,
Date: req.Date,
StartTime: req.StartTime,
EndTime: req.EndTime,
Type: req.Type,
}
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) GetUsersHandler(c echo.Context) error {
users, err := GetAllUsers(app.DB)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if users == nil {
users = []User{}
}
return c.JSON(http.StatusOK, users)
}
@ -116,10 +143,9 @@ func (app *App) DeleteUserHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
return c.NoContent(http.StatusNoContent)
}
// Time Entry Handlers
func (app *App) CreateTimeEntryHandler(c echo.Context) error {
userID := c.Get("user_id").(int)
@ -144,6 +170,9 @@ func (app *App) GetMyTimeEntriesHandler(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if entries == nil {
entries = []TimeEntry{}
}
return c.JSON(http.StatusOK, entries)
}
@ -189,6 +218,9 @@ func (app *App) GetAllTimeEntriesHandler(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if entries == nil {
entries = []TimeEntry{}
}
return c.JSON(http.StatusOK, entries)
}
@ -197,6 +229,9 @@ func (app *App) GetWeeklyHoursHandler(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if hours == nil {
hours = []WeeklyHours{}
}
return c.JSON(http.StatusOK, hours)
}
@ -217,7 +252,7 @@ func (app *App) DeleteWeekEntries(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
return c.NoContent(http.StatusNoContent)
}
type WeekDates struct {
@ -320,7 +355,7 @@ func (app *App) UpdateUserHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := UpdateUser(app.DB, userID, req.WeeklyHours); err != nil {
if err := UpdateUser(app.DB, userID, req.YearlyHours); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
@ -378,47 +413,18 @@ func (app *App) DeleteTimeEntryHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
return c.NoContent(http.StatusNoContent)
}
func (app *App) GetMyWeeklySummaryHandler(c echo.Context) error {
func (app *App) GetMyInfoHandler(c echo.Context) error {
userID := c.Get("user_id").(int)
year, err := strconv.Atoi(c.QueryParam("year"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid year")
}
week, err := strconv.Atoi(c.QueryParam("week"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid week")
}
user, err := GetUserByID(app.DB, userID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
allHours, err := GetWeeklyHours(app.DB)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
for _, h := range allHours {
if h.UserID == userID && h.Year == year && h.Week == week {
return c.JSON(http.StatusOK, h)
}
}
return c.JSON(http.StatusOK, WeeklyHours{
UserID: userID,
Username: user.Username,
Year: year,
Week: week,
TotalHours: 0,
ExpectedHours: user.WeeklyHours,
RemainingHours: user.WeeklyHours,
})
return c.JSON(http.StatusOK, user)
}
func (app *App) CreateUserHandler(c echo.Context) error {
@ -432,11 +438,11 @@ func (app *App) CreateUserHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Error hashing password")
}
if req.WeeklyHours == 0 {
req.WeeklyHours = 40.0
if req.YearlyHours == 0 {
req.YearlyHours = 1800.0
}
if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin, req.WeeklyHours); err != nil {
if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin, req.YearlyHours); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}