From e0dcc211e4e2e752fa6d821454ac4f3f50cd1c84 Mon Sep 17 00:00:00 2001 From: MarkCherepovskyi Date: Thu, 4 Jan 2024 11:52:34 +0200 Subject: [PATCH] update migrations --- docs/spec/components/schemas/Template.yaml | 7 +++++-- internal/assets/migrations/002_default_template.sql | 5 +++++ internal/service/api/handlers/create_template.go | 13 +++++++------ resources/model_template_attributes.go | 3 ++- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 internal/assets/migrations/002_default_template.sql diff --git a/docs/spec/components/schemas/Template.yaml b/docs/spec/components/schemas/Template.yaml index 184721ae..769dcefb 100644 --- a/docs/spec/components/schemas/Template.yaml +++ b/docs/spec/components/schemas/Template.yaml @@ -21,7 +21,8 @@ allOf: - is_completed - template_name - template_short_name - - template-id + - template_id + - is_default_template properties: template: type: object @@ -36,5 +37,7 @@ allOf: type: string template_id: type: int - format: int64 + format: int64 + is_default_template: + type: boolean diff --git a/internal/assets/migrations/002_default_template.sql b/internal/assets/migrations/002_default_template.sql new file mode 100644 index 00000000..ad1e6c71 --- /dev/null +++ b/internal/assets/migrations/002_default_template.sql @@ -0,0 +1,5 @@ +-- +migrate Up + +ALTER TABLE template ADD is_default_template INTEGER; + +ALTER TABLE template ADD CONSTRAINT ck_testbool_ischk CHECK (is_default_template IN (1,0)); \ No newline at end of file diff --git a/internal/service/api/handlers/create_template.go b/internal/service/api/handlers/create_template.go index 8e5f450d..7a2ad7ac 100644 --- a/internal/service/api/handlers/create_template.go +++ b/internal/service/api/handlers/create_template.go @@ -17,6 +17,7 @@ func CreateTemplate(w http.ResponseWriter, r *http.Request) { ape.RenderErr(w, problems.BadRequest(err)...) return } + client, err := MasterQ(r).ClientQ().FilterByName(req.Data.Relationships.User.Data.ID).Get() if err != nil { Log(r).WithError(err).Error("failed to get client") @@ -38,12 +39,12 @@ func CreateTemplate(w http.ResponseWriter, r *http.Request) { } err = MasterQ(r).TemplateQ().Insert(&data.Template{ - Template: templateBytes, - ImgBytes: []byte(strings.Replace(req.Data.Attributes.BackgroundImg, "data:image/png;base64,", "", 1)), - Name: req.Data.Attributes.TemplateName, - ShortName: req.Data.Attributes.TemplateShortName, - UserID: client.ID, - //IsDefaultTemplate: TODO update + Template: templateBytes, + ImgBytes: []byte(strings.Replace(req.Data.Attributes.BackgroundImg, "data:image/png;base64,", "", 1)), + Name: req.Data.Attributes.TemplateName, + ShortName: req.Data.Attributes.TemplateShortName, + UserID: client.ID, + IsDefaultTemplate: req.Data.Attributes.IsDefaultTemplate, }) if err != nil { Log(r).WithError(err).Error("failed to insert template") diff --git a/resources/model_template_attributes.go b/resources/model_template_attributes.go index 3d97a3bf..4f711fe4 100644 --- a/resources/model_template_attributes.go +++ b/resources/model_template_attributes.go @@ -9,8 +9,9 @@ import "encoding/json" type TemplateAttributes struct { BackgroundImg string `json:"background_img"` IsCompleted bool `json:"is_completed"` + IsDefaultTemplate bool `json:"is_default_template"` Template json.RawMessage `json:"template"` - TemplateId int64 `json:"template_id,omitempty"` + TemplateId int64 `json:"template_id"` TemplateName string `json:"template_name"` TemplateShortName string `json:"template_short_name"` }