DeleteProject Handler + API function, ber till gud att denna funkar first try
This commit is contained in:
parent
a5e3d4259d
commit
67723bfccc
7 changed files with 172 additions and 0 deletions
|
@ -46,6 +46,7 @@ type Database interface {
|
|||
RemoveProject(projectname string) error
|
||||
GetUserName(id int) (string, error)
|
||||
UnsignWeeklyReport(reportId int, projectManagerId int) error
|
||||
DeleteReport(reportID int) error
|
||||
}
|
||||
|
||||
// This struct is a wrapper type that holds the database connection
|
||||
|
@ -654,3 +655,8 @@ func (d *Db) GetUserName(id int) (string, error) {
|
|||
err := d.Get(&username, "SELECT username FROM users WHERE id = ?", id)
|
||||
return username, err
|
||||
}
|
||||
|
||||
func (d *Db) DeleteReport(reportID int) error {
|
||||
_, err := d.Exec("DELETE FROM weekly_reports WHERE report_id = ?", reportID)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1052,3 +1052,41 @@ func TestRemoveProject(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDeleteReport(t *testing.T) {
|
||||
db, err := setupAdvancedState()
|
||||
if err != nil {
|
||||
t.Error("setupState failed:", err)
|
||||
}
|
||||
|
||||
// Promote user to Admin
|
||||
err = db.PromoteToAdmin("demouser")
|
||||
if err != nil {
|
||||
t.Error("PromoteToAdmin failed:", err)
|
||||
}
|
||||
|
||||
// create a weekly report
|
||||
err = db.AddWeeklyReport("projecttest", "demouser", 16, 1, 1, 1, 1, 1, 1)
|
||||
if err != nil {
|
||||
t.Error("AddWeeklyReport failed:", err)
|
||||
}
|
||||
|
||||
// Check if the report was added
|
||||
report, err := db.GetWeeklyReport("demouser", "projecttest", 16)
|
||||
if err != nil {
|
||||
t.Error("GetWeeklyReport failed:", err)
|
||||
}
|
||||
|
||||
// Remove report
|
||||
err = db.DeleteReport(report.ReportId,)
|
||||
if err != nil {
|
||||
t.Error("RemoveReport failed:", err)
|
||||
}
|
||||
|
||||
// Check if the report was removed
|
||||
report, err = db.GetWeeklyReport("demouser", "projecttest", 16)
|
||||
if err == nil {
|
||||
t.Error("RemoveReport failed: report not removed")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
22
backend/internal/handlers/reports/DeleteReport.go
Normal file
22
backend/internal/handlers/reports/DeleteReport.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package reports
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
db "ttime/internal/database"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func DeleteReport(c *fiber.Ctx) error {
|
||||
reportID := c.Params("reportID")
|
||||
reportIDInt, err := strconv.Atoi(reportID)
|
||||
if err != nil {
|
||||
return c.Status(400).SendString("Invalid report ID")
|
||||
}
|
||||
|
||||
if err := db.GetDb(c).DeleteReport(reportIDInt); err != nil {
|
||||
return c.Status(500).SendString((err.Error()))
|
||||
}
|
||||
|
||||
return c.Status(200).SendString("Weekly report deleted")
|
||||
}
|
|
@ -136,6 +136,7 @@ func main() {
|
|||
api.Put("/signReport/:reportId", reports.SignReport)
|
||||
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
|
||||
api.Put("/unsignReport/:reportId", reports.UnsignReport)
|
||||
api.Delete("/deleteReport/:reportId", reports.DeleteReport)
|
||||
|
||||
// Announce the port we are listening on and start the server
|
||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue