mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-04-26 06:08:12 -05:00
153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
package databases
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
|
"github.com/eduardolat/pgbackweb/internal/validate"
|
|
"github.com/eduardolat/pgbackweb/internal/view/web/component"
|
|
"github.com/eduardolat/pgbackweb/internal/view/web/htmxs"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
nodx "github.com/nodxdev/nodxgo"
|
|
htmx "github.com/nodxdev/nodxgo-htmx"
|
|
lucide "github.com/nodxdev/nodxgo-lucide"
|
|
)
|
|
|
|
func (h *handlers) editDatabaseHandler(c echo.Context) error {
|
|
ctx := c.Request().Context()
|
|
|
|
databaseID, err := uuid.Parse(c.Param("databaseID"))
|
|
if err != nil {
|
|
return htmxs.RespondToastError(c, err.Error())
|
|
}
|
|
|
|
var formData createDatabaseDTO
|
|
if err := c.Bind(&formData); err != nil {
|
|
return htmxs.RespondToastError(c, err.Error())
|
|
}
|
|
if err := validate.Struct(&formData); err != nil {
|
|
return htmxs.RespondToastError(c, err.Error())
|
|
}
|
|
|
|
_, err = h.servs.DatabasesService.UpdateDatabase(
|
|
ctx, dbgen.DatabasesServiceUpdateDatabaseParams{
|
|
ID: databaseID,
|
|
Name: sql.NullString{String: formData.Name, Valid: true},
|
|
PgVersion: sql.NullString{String: formData.Version, Valid: true},
|
|
ConnectionString: sql.NullString{String: formData.ConnectionString, Valid: true},
|
|
},
|
|
)
|
|
if err != nil {
|
|
return htmxs.RespondToastError(c, err.Error())
|
|
}
|
|
|
|
return htmxs.RespondAlertWithRefresh(c, "Database updated")
|
|
}
|
|
|
|
func editDatabaseButton(
|
|
database dbgen.DatabasesServicePaginateDatabasesRow,
|
|
) nodx.Node {
|
|
idPref := "edit-database-" + database.ID.String()
|
|
formID := idPref + "-form"
|
|
btnClass := idPref + "-btn"
|
|
loadingID := idPref + "-loading"
|
|
|
|
htmxAttributes := func(url string) nodx.Node {
|
|
return nodx.Group(
|
|
htmx.HxPost(url),
|
|
htmx.HxInclude("#"+formID),
|
|
htmx.HxDisabledELT("."+btnClass),
|
|
htmx.HxIndicator("#"+loadingID),
|
|
htmx.HxValidate("true"),
|
|
)
|
|
}
|
|
|
|
mo := component.Modal(component.ModalParams{
|
|
Size: component.SizeMd,
|
|
Title: "Edit database",
|
|
Content: []nodx.Node{
|
|
nodx.FormEl(
|
|
nodx.Id(formID),
|
|
nodx.Class("space-y-2"),
|
|
|
|
component.InputControl(component.InputControlParams{
|
|
Name: "name",
|
|
Label: "Name",
|
|
Placeholder: "My database",
|
|
Required: true,
|
|
Type: component.InputTypeText,
|
|
HelpText: "A name to easily identify the database",
|
|
Children: []nodx.Node{
|
|
nodx.Value(database.Name),
|
|
},
|
|
}),
|
|
|
|
component.SelectControl(component.SelectControlParams{
|
|
Name: "version",
|
|
Label: "Version",
|
|
Required: true,
|
|
HelpText: "The version of the database",
|
|
Children: []nodx.Node{
|
|
component.PGVersionSelectOptions(sql.NullString{
|
|
Valid: true,
|
|
String: database.PgVersion,
|
|
}),
|
|
},
|
|
}),
|
|
|
|
component.InputControl(component.InputControlParams{
|
|
Name: "connection_string",
|
|
Label: "Connection string",
|
|
Placeholder: "postgresql://user:password@localhost:5432/mydb",
|
|
Required: true,
|
|
Type: component.InputTypeText,
|
|
HelpText: "It should be a valid PostgreSQL connection string including the database name. It will be stored securely using PGP encryption.",
|
|
Children: []nodx.Node{
|
|
nodx.Value(database.DecryptedConnectionString),
|
|
},
|
|
}),
|
|
),
|
|
|
|
nodx.Div(
|
|
nodx.Class("flex justify-between items-center pt-4"),
|
|
nodx.Div(
|
|
nodx.Button(
|
|
htmxAttributes("/dashboard/databases/test"),
|
|
nodx.ClassMap{
|
|
btnClass: true,
|
|
"btn btn-neutral btn-outline": true,
|
|
},
|
|
nodx.Type("button"),
|
|
component.SpanText("Test connection"),
|
|
lucide.DatabaseZap(),
|
|
),
|
|
),
|
|
nodx.Div(
|
|
nodx.Class("flex justify-end items-center space-x-2"),
|
|
component.HxLoadingMd(loadingID),
|
|
nodx.Button(
|
|
htmxAttributes("/dashboard/databases/"+database.ID.String()+"/edit"),
|
|
nodx.ClassMap{
|
|
btnClass: true,
|
|
"btn btn-primary": true,
|
|
},
|
|
nodx.Type("button"),
|
|
component.SpanText("Save"),
|
|
lucide.Save(),
|
|
),
|
|
),
|
|
),
|
|
},
|
|
})
|
|
|
|
return nodx.Div(
|
|
mo.HTML,
|
|
component.OptionsDropdownButton(
|
|
mo.OpenerAttr,
|
|
lucide.Pencil(),
|
|
component.SpanText("Edit database"),
|
|
),
|
|
)
|
|
}
|