feat(api): delete template endpoint

This commit is contained in:
Leons Aleksandrovs
2025-07-13 14:26:09 +03:00
parent fa095def44
commit e969d56ab6
4 changed files with 45 additions and 3 deletions

View File

@@ -174,4 +174,36 @@ func Put(c *gin.Context) {
}
func Delete(c *gin.Context) {
// Get request data, with id, and user
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
res.Error(c, err.Error(), http.StatusBadRequest)
return
}
user, err := jwt.GetUser(c)
if err != nil {
res.NeedsToLogin(c)
return
}
// Check if template exists
templates, err := template.Get("id = $1 AND user_id = $2", id, user.Id)
if err != nil {
res.Error(c, err.Error(), http.StatusInternalServerError)
return
}
if len(templates) == 0 {
res.Error(c, "Template not found", http.StatusNotFound)
return
}
// Delete template, and return success
if err := template.Delete(id); err != nil {
res.Error(c, err.Error(), http.StatusInternalServerError)
return
}
res.Success(c, gin.H{"message": "Successfully deleted template"})
}

View File

@@ -82,3 +82,13 @@ func Update(id int, name string, template string) error {
return err
}
func Delete(id int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `DELETE FROM templates WHERE id = $1`
_, err := db.Pool.Exec(ctx, query, id)
return err
}

View File

@@ -29,7 +29,7 @@ func SetupRoutes() *gin.Engine {
templates.GET("/:id", template.GetID)
templates.POST("", template.Create)
templates.PUT("/:id", template.Put)
// DELETE (Delete)
templates.DELETE("/:id", template.Delete)
// Cover letter routes
covers := auth.Group("/cover")

View File

@@ -33,9 +33,9 @@ function RouteComponent() {
const a = confirm("Are you sure?");
if (!a) return;
requests.delete(`/template/${templateId}`, {
requests.delete(`/templates/${templateId}`, {
success() {
navigate({ to: "/" });
navigate({ to: "/templates" });
},
});
};