From b636427074d705c3df46a7dbb903cf3d3225dd15 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Wed, 24 Jul 2024 08:49:00 +0300 Subject: [PATCH] Code style improvements for PR #5 --- .../spec/paths/integrations@forms-svc@v1@form.yaml | 2 +- internal/data/forms.go | 1 - internal/data/pg/forms.go | 8 -------- internal/service/handlers/submit_form.go | 2 +- internal/service/workers/formsender/main.go | 5 ++++- internal/storage/main.go | 14 +++++++------- 6 files changed, 13 insertions(+), 19 deletions(-) diff --git a/docs/spec/paths/integrations@forms-svc@v1@form.yaml b/docs/spec/paths/integrations@forms-svc@v1@form.yaml index 7b290b6..80632f8 100644 --- a/docs/spec/paths/integrations@forms-svc@v1@form.yaml +++ b/docs/spec/paths/integrations@forms-svc@v1@form.yaml @@ -33,7 +33,7 @@ post: 401: $ref: '#/components/responses/invalidAuth' 403: - description: "Empty form absent for user, but already exists processed or accepted forms" + description: "Empty form absent for user, but processed or accepted one exists" content: application/vnd.api+json: schema: diff --git a/internal/data/forms.go b/internal/data/forms.go index 816061c..995da25 100644 --- a/internal/data/forms.go +++ b/internal/data/forms.go @@ -64,7 +64,6 @@ type FormStatus struct { type FormsQ interface { New() FormsQ Insert(*Form) (*FormStatus, error) - UpdateStatus(status string) error Update(map[string]interface{}) error diff --git a/internal/data/pg/forms.go b/internal/data/pg/forms.go index f3d29e0..d1999cd 100644 --- a/internal/data/pg/forms.go +++ b/internal/data/pg/forms.go @@ -71,14 +71,6 @@ func (q *formsQ) Insert(form *data.Form) (*data.FormStatus, error) { return &res, nil } -func (q *formsQ) UpdateStatus(status string) error { - if err := q.db.Exec(q.updater.Set("status", status)); err != nil { - return fmt.Errorf("update forms status: %w", err) - } - - return nil -} - func (q *formsQ) Update(fields map[string]any) error { if err := q.db.Exec(q.updater.SetMap(fields)); err != nil { return fmt.Errorf("update forms: %w", err) diff --git a/internal/service/handlers/submit_form.go b/internal/service/handlers/submit_form.go index 4b9a531..6bea2cd 100644 --- a/internal/service/handlers/submit_form.go +++ b/internal/service/handlers/submit_form.go @@ -41,7 +41,7 @@ func SubmitForm(w http.ResponseWriter, r *http.Request) { } if lastForm.Status != data.CreatedStatus { - Log(r).Debugf("User last form don't have created status") + Log(r).Debugf("User last form must have %s status, got %s", data.CreatedStatus, lastForm.Status) ape.RenderErr(w, problems.Forbidden()) return } diff --git a/internal/service/workers/formsender/main.go b/internal/service/workers/formsender/main.go index 7760f2d..41280d3 100644 --- a/internal/service/workers/formsender/main.go +++ b/internal/service/workers/formsender/main.go @@ -55,7 +55,10 @@ func Run(ctx context.Context, cfg config.Config) { ids[i] = v.ID } - if err = db.FormsQ().FilterByID(ids...).UpdateStatus(data.ProcessedStatus); err != nil { + err = db.FormsQ().FilterByID(ids...).Update(map[string]any{ + data.ColStatus: data.ProcessedStatus, + }) + if err != nil { return fmt.Errorf("failed to update form status: %w", err) } diff --git a/internal/storage/main.go b/internal/storage/main.go index 99ef9ec..13544df 100644 --- a/internal/storage/main.go +++ b/internal/storage/main.go @@ -13,7 +13,7 @@ import ( ) func (s *Storage) GetImageBase64(object *url.URL) (*string, error) { - spacesURL, err := ParseDOSpacesURL(object) + spacesURL, err := parseDOSpacesURL(object) if err != nil { return nil, fmt.Errorf("failed to parse url [%s]: %w", object.String(), err) } @@ -38,7 +38,7 @@ func (s *Storage) GetImageBase64(object *url.URL) (*string, error) { } func (s *Storage) ValidateImage(object *url.URL, id string) error { - spacesURL, err := ParseDOSpacesURL(object) + spacesURL, err := parseDOSpacesURL(object) if err != nil { return fmt.Errorf("failed to parse url [%s]: %w", object.String(), err) } @@ -91,7 +91,7 @@ func (s *Storage) GeneratePutURL(fileName, contentType string, contentLength int return signedURL, key, nil } -func ParseDOSpacesURL(object *url.URL) (*SpacesURL, error) { +func parseDOSpacesURL(object *url.URL) (*SpacesURL, error) { spacesURL := &SpacesURL{ URL: object, } @@ -110,10 +110,10 @@ func ParseDOSpacesURL(object *url.URL) (*SpacesURL, error) { } func IsBadRequestError(err error) bool { - if errors.Is(err, ErrImageTooLarge) && - errors.Is(err, ErrIncorrectImageType) && - errors.Is(err, ErrURLRegexp) && - errors.Is(err, ErrInvalidBucket) && + if errors.Is(err, ErrImageTooLarge) || + errors.Is(err, ErrIncorrectImageType) || + errors.Is(err, ErrURLRegexp) || + errors.Is(err, ErrInvalidBucket) || errors.Is(err, ErrInvalidKey) { return true }