Skip to content

Commit

Permalink
Using pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Jul 19, 2024
1 parent ca29a70 commit 433bc2c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
8 changes: 7 additions & 1 deletion internal/config/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Forms struct {
Period time.Duration
MinAbnormalPeriod time.Duration
MaxAbnormalPeriod time.Duration
ResendFormsCount uint64
db *sql.DB
}

Expand All @@ -27,6 +28,7 @@ type formsConfig struct {
Period time.Duration `fig:"period,required"`
MinAbnormalPeriod time.Duration `fig:"min_abnormal_period,required"`
MaxAbnormalPeriod time.Duration `fig:"max_abnormal_period,required"`
ResendFormsCount uint64 `fig:"resend_forms_count,required"`
URL string `fig:"url,required"`
}

Expand All @@ -51,12 +53,13 @@ func (c *config) Forms() *Forms {
Period: cfg.Period,
MinAbnormalPeriod: cfg.MinAbnormalPeriod,
MaxAbnormalPeriod: cfg.MaxAbnormalPeriod,
ResendFormsCount: cfg.ResendFormsCount,
db: db,
}
}).(*Forms)
}

func (f *Forms) SendForms(forms ...data.Form) error {
func (f *Forms) SendForms(forms ...*data.Form) error {
if len(forms) == 0 {
return nil
}
Expand All @@ -79,6 +82,9 @@ func (f *Forms) SendForms(forms ...data.Form) error {
)

for _, form := range forms {
if form == nil {
continue
}
stmt = stmt.Values(
form.Name,
form.Surname,
Expand Down
7 changes: 4 additions & 3 deletions internal/data/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ type Form struct {
Postal string `db:"postal"`
Phone string `db:"phone"`
Email string `db:"email"`
Image string `db:"image"`
Image *string `db:"image"`
CreatedAt time.Time `db:"created_at"`
}

type FormsQ interface {
New() FormsQ
Insert(Form) (*Form, error)
Insert(*Form) (string, error)
Update(status string) error

Select() ([]Form, error)
Select() ([]*Form, error)
Get() (*Form, error)
// last returns the most recent form
Last() (*Form, error)
Limit(uint64) FormsQ

FilterByID(ids ...string) FormsQ
FilterByNullifier(nullifier string) FormsQ
Expand Down
19 changes: 12 additions & 7 deletions internal/data/pg/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (q *formsQ) New() data.FormsQ {
return NewForms(q.db)
}

func (q *formsQ) Insert(form data.Form) (*data.Form, error) {
var res data.Form
func (q *formsQ) Insert(form *data.Form) (string, error) {
var res string
stmt := squirrel.Insert(formsTable).SetMap(map[string]interface{}{
"nullifier": form.Nullifier,
"status": form.Status,
Expand All @@ -51,13 +51,13 @@ func (q *formsQ) Insert(form data.Form) (*data.Form, error) {
"phone": form.Phone,
"email": form.Email,
"image": form.Image,
}).Suffix("RETURNING *")
}).Suffix("RETURNING id")

if err := q.db.Get(&res, stmt); err != nil {
return nil, fmt.Errorf("insert form [%+v]: %w", form, err)
return "", fmt.Errorf("insert form [%+v]: %w", form, err)
}

return &res, nil
return res, nil
}

func (q *formsQ) Update(status string) error {
Expand All @@ -68,8 +68,8 @@ func (q *formsQ) Update(status string) error {
return nil
}

func (q *formsQ) Select() ([]data.Form, error) {
var res []data.Form
func (q *formsQ) Select() ([]*data.Form, error) {
var res []*data.Form

if err := q.db.Select(&res, q.selector); err != nil {
return nil, fmt.Errorf("select forms: %w", err)
Expand Down Expand Up @@ -104,6 +104,11 @@ func (q *formsQ) Last() (*data.Form, error) {
return &res, nil
}

func (q *formsQ) Limit(limit uint64) data.FormsQ {
q.selector = q.selector.Limit(limit)
return q
}

func (q *formsQ) FilterByID(ids ...string) data.FormsQ {
return q.applyCondition(squirrel.Eq{"id": ids})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/get_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func GetForm(w http.ResponseWriter, r *http.Request) {
return
}

ape.Render(w, newFormResponse(form))
ape.Render(w, newFormResponse(form.ID))
}
29 changes: 6 additions & 23 deletions internal/service/handlers/submit_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,36 @@ func SubmitForm(w http.ResponseWriter, r *http.Request) {
Postal: userData.Postal,
Phone: userData.Phone,
Email: userData.Email,
Image: userData.Image,
Image: &userData.Image,
}

Log(r).Debug("Start mysql insert query")
err = Forms(r).SendForms(*form)
err = Forms(r).SendForms(form)
if err != nil {
Log(r).WithError(err).Error("failed to send form")
form.Status = data.AcceptedStatus
}
Log(r).Debug("Finished mysql insert query")

Log(r).Debug("Start postgresql insert query")
form, err = FormsQ(r).Insert(*form)
formID, err := FormsQ(r).Insert(form)
if err != nil {
Log(r).WithError(err).Error("failed to insert form")
ape.RenderErr(w, problems.InternalError())
return
}
Log(r).Debug("Finished postgresql insert query")

ape.Render(w, newFormResponse(form))
ape.Render(w, newFormResponse(formID))
}

func newFormResponse(form *data.Form) resources.FormResponse {
func newFormResponse(formID string) resources.FormResponse {
return resources.FormResponse{
Data: resources.Form{
Key: resources.Key{
ID: form.ID,
ID: formID,
Type: resources.FORM,
},
Attributes: resources.FormAttributes{
Name: form.Name,
Surname: form.Surname,
IdNum: form.IDNum,
Birthday: form.Birthday,
Citizen: form.Citizen,
Visited: form.Visited,
Purpose: form.Purpose,
Country: form.Country,
City: form.City,
Address: form.Address,
Postal: form.Postal,
Phone: form.Phone,
Email: form.Email,
Image: "",
Status: form.Status,
},
},
}
}
2 changes: 1 addition & 1 deletion internal/service/workers/formsender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Run(ctx context.Context, cfg config.Config) {
log,
"resender",
func(context.Context) error {
forms, err := db.FormsQ().FilterByStatus(data.AcceptedStatus).Select()
forms, err := db.FormsQ().FilterByStatus(data.AcceptedStatus).Limit(formsCfg.ResendFormsCount).Select()
if err != nil {
return fmt.Errorf("failed to get unsended forms: %w", err)
}
Expand Down

0 comments on commit 433bc2c

Please sign in to comment.