feat(api): update template
This commit is contained in:
@@ -2,6 +2,7 @@ package template
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/models/template"
|
"backend/models/template"
|
||||||
|
"backend/utils"
|
||||||
"backend/utils/jwt"
|
"backend/utils/jwt"
|
||||||
res "backend/utils/responses"
|
res "backend/utils/responses"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -124,7 +125,52 @@ func GetID(c *gin.Context) {
|
|||||||
res.Success(c, gin.H{"template": templates[0]})
|
res.Success(c, gin.H{"template": templates[0]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(c *gin.Context) {
|
type PutData struct {
|
||||||
|
Name string `json:"name" validate:"required,min=1,max=50"`
|
||||||
|
Content string `json:"content" validate:"required,min=50"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Put(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
|
||||||
|
}
|
||||||
|
|
||||||
|
var data PutData
|
||||||
|
if err := utils.BindAndValidate(&data, c); err != nil {
|
||||||
|
res.Error(c, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if template already exists
|
||||||
|
templates, err := template.Get("user_id = $1 AND id = $2", user.Id, id)
|
||||||
|
if err != nil {
|
||||||
|
res.Error(c, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if template is found
|
||||||
|
if len(templates) == 0 {
|
||||||
|
res.Error(c, "Template not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update template
|
||||||
|
err = template.Update(id, data.Name, data.Content)
|
||||||
|
if err != nil {
|
||||||
|
res.Error(c, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Success(c, gin.H{"message": "Successfully updated template"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(c *gin.Context) {
|
func Delete(c *gin.Context) {
|
||||||
|
|||||||
@@ -72,3 +72,13 @@ func Get(where string, args ...any) ([]Template, error) {
|
|||||||
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Update(id int, name string, template string) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
query := `UPDATE templates SET name = $1, template = $2 WHERE id = $3`
|
||||||
|
_, err := db.Pool.Exec(ctx, query, name, template, id)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ func SetupRoutes() *gin.Engine {
|
|||||||
// Template routes (REST FUCKING GOOOOO)
|
// Template routes (REST FUCKING GOOOOO)
|
||||||
templates := auth.Group("/templates")
|
templates := auth.Group("/templates")
|
||||||
templates.GET("", template.Get)
|
templates.GET("", template.Get)
|
||||||
templates.GET(":id", template.GetID)
|
templates.GET("/:id", template.GetID)
|
||||||
templates.POST("", template.Create)
|
templates.POST("", template.Create)
|
||||||
// PUT (Edit)
|
templates.PUT("/:id", template.Put)
|
||||||
// DELETE (Delete)
|
// DELETE (Delete)
|
||||||
|
|
||||||
// Cover letter routes
|
// Cover letter routes
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export const Route = createFileRoute("/templates/edit/$templateId")({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const editSchema = z.object({
|
const editSchema = z.object({
|
||||||
name: z.string().min(1, "Name is required"),
|
name: z.string().min(1, "Name is required").max(50, "Name is too long"),
|
||||||
content: z.string().min(50, "Template is too short"),
|
content: z.string().min(50, "Template is too short"),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ function RouteComponent() {
|
|||||||
|
|
||||||
const template = useQuery({
|
const template = useQuery({
|
||||||
queryKey: ["template", templateId],
|
queryKey: ["template", templateId],
|
||||||
queryFn: () => requests.get<{ template: Template }>(`/template/${templateId}`, {}),
|
queryFn: () => requests.get<{ template: Template }>(`/templates/${templateId}`, {}),
|
||||||
});
|
});
|
||||||
const templateState = renderQueryState({
|
const templateState = renderQueryState({
|
||||||
query: template,
|
query: template,
|
||||||
@@ -39,13 +39,13 @@ function RouteComponent() {
|
|||||||
const edit = useAppForm({
|
const edit = useAppForm({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
name: template.data?.template.name || "",
|
name: template.data?.template.name || "",
|
||||||
content: template.data?.template.content || null,
|
content: template.data?.template.template || null,
|
||||||
},
|
},
|
||||||
validators: {
|
validators: {
|
||||||
onBlur: editSchema,
|
onBlur: editSchema,
|
||||||
},
|
},
|
||||||
onSubmit({ value }) {
|
onSubmit({ value }) {
|
||||||
requests.put(`/template/${templateId}`, {
|
requests.put(`/templates/${templateId}`, {
|
||||||
data: value,
|
data: value,
|
||||||
before() {
|
before() {
|
||||||
loading[1](true);
|
loading[1](true);
|
||||||
|
|||||||
Reference in New Issue
Block a user