feat(api): delete template endpoint
This commit is contained in:
@@ -174,4 +174,36 @@ func Put(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Delete(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"})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,3 +82,13 @@ func Update(id int, name string, template string) error {
|
|||||||
|
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func SetupRoutes() *gin.Engine {
|
|||||||
templates.GET("/:id", template.GetID)
|
templates.GET("/:id", template.GetID)
|
||||||
templates.POST("", template.Create)
|
templates.POST("", template.Create)
|
||||||
templates.PUT("/:id", template.Put)
|
templates.PUT("/:id", template.Put)
|
||||||
// DELETE (Delete)
|
templates.DELETE("/:id", template.Delete)
|
||||||
|
|
||||||
// Cover letter routes
|
// Cover letter routes
|
||||||
covers := auth.Group("/cover")
|
covers := auth.Group("/cover")
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ function RouteComponent() {
|
|||||||
const a = confirm("Are you sure?");
|
const a = confirm("Are you sure?");
|
||||||
if (!a) return;
|
if (!a) return;
|
||||||
|
|
||||||
requests.delete(`/template/${templateId}`, {
|
requests.delete(`/templates/${templateId}`, {
|
||||||
success() {
|
success() {
|
||||||
navigate({ to: "/" });
|
navigate({ to: "/templates" });
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user