Added handler for GetWeeklyReport

This commit is contained in:
dDogge 2024-03-17 18:05:54 +01:00
parent a77e57e496
commit 3e00a532cf
2 changed files with 29 additions and 0 deletions

View file

@ -15,6 +15,7 @@ type GlobalState interface {
CreateProject(c *fiber.Ctx) error // To create a new project
GetUserProjects(c *fiber.Ctx) error // To get all projects
SubmitWeeklyReport(c *fiber.Ctx) error
GetWeeklyReport(c *fiber.Ctx) error
// GetProject(c *fiber.Ctx) error // To get a specific project
// UpdateProject(c *fiber.Ctx) error // To update a project
// DeleteProject(c *fiber.Ctx) error // To delete a project

View file

@ -1,6 +1,7 @@
package handlers
import (
"strconv"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
@ -32,3 +33,30 @@ func (gs *GState) SubmitWeeklyReport(c *fiber.Ctx) error {
return c.Status(200).SendString("Time report added")
}
// Handler for retrieving weekly report
func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the request
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract project name and week from query parameters
projectName := c.Query("projectName")
week := c.Query("week")
// Convert week to integer
weekInt, err := strconv.Atoi(week)
if err != nil {
return c.Status(400).SendString("Invalid week number")
}
// Call the database function to get the weekly report
report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt)
if err != nil {
return c.Status(500).SendString(err.Error())
}
// Return the retrieved weekly report
return c.JSON(report)
}