fix: fix wrong date calculation
This commit is contained in:
parent
4514ce44a2
commit
c8b7666971
4 changed files with 815 additions and 62 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
|
@ -147,6 +148,44 @@ func (app *App) GetMyTimeEntriesHandler(c echo.Context) error {
|
|||
return c.JSON(http.StatusOK, entries)
|
||||
}
|
||||
|
||||
// GetWeekDates - Gibt die Daten einer Woche zurück (Montag-Freitag)
|
||||
func (app *App) GetWeekDates(c echo.Context) error {
|
||||
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")
|
||||
}
|
||||
|
||||
dates := calculateWeekDates(year, week)
|
||||
return c.JSON(http.StatusOK, dates)
|
||||
}
|
||||
|
||||
// CheckWeekHasEntries - Prüft ob User Einträge für eine Woche hat
|
||||
func (app *App) CheckWeekHasEntries(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")
|
||||
}
|
||||
|
||||
hasEntries, err := CheckUserHasEntriesForWeek(app.DB, userID, year, week)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]bool{"has_entries": hasEntries})
|
||||
}
|
||||
|
||||
func (app *App) GetAllTimeEntriesHandler(c echo.Context) error {
|
||||
entries, err := GetAllTimeEntries(app.DB)
|
||||
if err != nil {
|
||||
|
|
@ -162,3 +201,72 @@ func (app *App) GetWeeklyHoursHandler(c echo.Context) error {
|
|||
}
|
||||
return c.JSON(http.StatusOK, hours)
|
||||
}
|
||||
|
||||
func (app *App) DeleteWeekEntries(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")
|
||||
}
|
||||
|
||||
if err := DeleteTimeEntriesByUserAndWeek(app.DB, userID, year, week); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
type WeekDates struct {
|
||||
Year int `json:"year"`
|
||||
Week int `json:"week"`
|
||||
Dates map[string]string `json:"dates"` // dayOfWeek -> date
|
||||
Range string `json:"range"` // "2025-11-03 bis 2025-11-07"
|
||||
}
|
||||
|
||||
func calculateWeekDates(year, week int) WeekDates {
|
||||
// ISO 8601: Woche 1 ist die Woche mit dem ersten Donnerstag
|
||||
// Finde den ersten Donnerstag des Jahres
|
||||
jan4 := time.Date(year, time.January, 4, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// Finde Montag der Woche 1
|
||||
weekday := int(jan4.Weekday())
|
||||
if weekday == 0 {
|
||||
weekday = 7 // Sonntag -> 7
|
||||
}
|
||||
daysToMonday := weekday - 1
|
||||
mondayWeek1 := jan4.AddDate(0, 0, -daysToMonday)
|
||||
|
||||
// Berechne Montag der gewünschten Woche
|
||||
targetMonday := mondayWeek1.AddDate(0, 0, (week-1)*7)
|
||||
|
||||
dates := make(map[string]string)
|
||||
weekDays := []string{"0", "1", "2", "3", "4"} // Montag bis Freitag
|
||||
|
||||
var firstDate, lastDate time.Time
|
||||
for i, day := range weekDays {
|
||||
date := targetMonday.AddDate(0, 0, i)
|
||||
dates[day] = date.Format("2006-01-02")
|
||||
|
||||
if i == 0 {
|
||||
firstDate = date
|
||||
}
|
||||
if i == 4 {
|
||||
lastDate = date
|
||||
}
|
||||
}
|
||||
|
||||
rangeStr := firstDate.Format("2006-01-02") + " bis " + lastDate.Format("2006-01-02")
|
||||
|
||||
return WeekDates{
|
||||
Year: year,
|
||||
Week: week,
|
||||
Dates: dates,
|
||||
Range: rangeStr,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue