feat: dialog to change template metadata in editor

This commit is contained in:
2024-12-03 18:34:23 +00:00
parent 4e34570792
commit e5bbd15837
10 changed files with 369 additions and 72 deletions

View File

@@ -19,6 +19,7 @@ type createTemplateRequestBody struct {
}
type postTemplateRequestBody struct {
Name *string `json:"name"`
Description *string `json:"description"`
Files []templateFile `json:"files"`
@@ -114,13 +115,22 @@ func updateTemplate(c echo.Context, body postTemplateRequestBody) error {
mgr := templateManagerFrom(c)
ctx := c.Request().Context()
updatedTemplate, err := mgr.updateTemplate(ctx, name, updateTemplateOptions{
description: *body.Description,
})
var opts updateTemplateOptions
if body.Name != nil {
opts.name = *body.Name
}
if body.Description != nil {
opts.description = *body.Description
}
updatedTemplate, err := mgr.updateTemplate(ctx, name, opts)
if err != nil {
if errors.Is(err, errTemplateNotFound) {
return echo.NewHTTPError(http.StatusNotFound)
}
if errors.Is(err, errTemplateExists) {
return echo.NewHTTPError(http.StatusConflict)
}
return err
}

View File

@@ -20,7 +20,7 @@ type template struct {
IsBuilt bool `json:"isBuilt"`
Files []*templateFile `bun:"rel:has-many,join:id=template_id" json:"-"`
FileMap map[string]*templateFile `bun:"-" json:"files"`
FileMap map[string]*templateFile `bun:"-" json:"files,omitempty"`
}
type templateFile struct {

View File

@@ -30,7 +30,12 @@ type createTemplateOptions struct {
}
type updateTemplateOptions struct {
tx *bun.Tx
tx *bun.Tx
// name is the new name for the template
name string
// description is the new description for the template
description string
}
@@ -183,10 +188,29 @@ func (mgr *templateManager) updateTemplate(ctx context.Context, name string, opt
tx = &_tx
}
if opts.name != "" {
exists, err := tx.NewSelect().
Table("templates").
Where("name = ?", opts.name).
Exists(ctx)
if err != nil {
return nil, err
}
if exists {
return nil, errTemplateExists
}
}
var template template
err := tx.NewUpdate().Model(&template).
Where("Name = ?", name).
Set("description = ?", opts.description).
q := tx.NewUpdate().Model(&template).Where("name = ?", name)
if opts.name != "" {
q = q.Set("name = ?", opts.name)
}
if opts.description != "" {
q = q.Set("description = ?", opts.description)
}
err := q.
Returning("*").
Scan(ctx)
if err != nil {