feat: update all and add all features for version 1.0
clean up in codebase needed
This commit is contained in:
parent
20ba24001a
commit
5001cc1147
8 changed files with 1497 additions and 129 deletions
|
|
@ -79,24 +79,24 @@ func (app *App) DeleteScheduleHandler(c echo.Context) 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")
|
||||
}
|
||||
// // 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")
|
||||
}
|
||||
// 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())
|
||||
}
|
||||
// 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"})
|
||||
}
|
||||
// return c.JSON(http.StatusCreated, map[string]string{"message": "user created"})
|
||||
// }
|
||||
|
||||
func (app *App) GetUsersHandler(c echo.Context) error {
|
||||
users, err := GetAllUsers(app.DB)
|
||||
|
|
@ -308,3 +308,137 @@ func (app *App) CreateBatchTimeEntriesHandler(c echo.Context) error {
|
|||
|
||||
return c.JSON(http.StatusCreated, map[string]string{"message": "entries created"})
|
||||
}
|
||||
|
||||
func (app *App) UpdateUserHandler(c echo.Context) error {
|
||||
userID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user ID")
|
||||
}
|
||||
|
||||
var req UpdateUserRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if err := UpdateUser(app.DB, userID, req.WeeklyHours); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (app *App) ResetPasswordHandler(c echo.Context) error {
|
||||
userID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user ID")
|
||||
}
|
||||
|
||||
var req ResetPasswordRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error hashing password")
|
||||
}
|
||||
|
||||
if err := ResetUserPassword(app.DB, userID, string(hashedPassword)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (app *App) UpdateTimeEntryHandler(c echo.Context) error {
|
||||
entryID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid entry ID")
|
||||
}
|
||||
|
||||
var req UpdateTimeEntryRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if err := UpdateTimeEntry(app.DB, entryID, req.Date, req.StartTime, req.EndTime, req.Type); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (app *App) DeleteTimeEntryHandler(c echo.Context) error {
|
||||
entryID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid entry ID")
|
||||
}
|
||||
|
||||
if err := DeleteTimeEntry(app.DB, entryID); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (app *App) GetMyWeeklySummaryHandler(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,
|
||||
})
|
||||
}
|
||||
|
||||
func (app *App) CreateUserHandler(c echo.Context) error {
|
||||
var req CreateUserRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error hashing password")
|
||||
}
|
||||
|
||||
if req.WeeklyHours == 0 {
|
||||
req.WeeklyHours = 40.0
|
||||
}
|
||||
|
||||
if err := CreateUser(app.DB, req.Username, string(hashedPassword), req.IsAdmin, req.WeeklyHours); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue