From d1592e8a8a2892f6981016728e6573e2f6846fa9 Mon Sep 17 00:00:00 2001 From: Nic Johnson Date: Sun, 24 Jul 2022 16:30:16 -0500 Subject: [PATCH 1/2] feat: add ability to mark drink as "under development" --- pkg/db/migrations.go | 21 +++++++++++++++++++++ pkg/drink/conversions.go | 13 +++++++++++++ pkg/drink/db.go | 19 ++++++++++--------- pkg/drink/drink_data.go | 25 ++++++++++++++++++------- pkg/drink/drink_test.go | 2 ++ 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/pkg/db/migrations.go b/pkg/db/migrations.go index 03f4ea5..c81a60f 100644 --- a/pkg/db/migrations.go +++ b/pkg/db/migrations.go @@ -82,6 +82,27 @@ func createMigrations() *migrate.MemoryMigrationSource { `}, Down: []string{"DROP TABLE user_setting;"}, }, + { + Id: nextId(), + Up: []string{` + ALTER TABLE + drink + ADD COLUMN + under_development + INTEGER + NOT NULL + DEFAULT + FALSE + ; + `}, + Down: []string{` + ALTER TABLE + drink + DROP COLUMN + under_development + ; + `}, + }, //{ // Id: nextId(), // Up: []string{}, diff --git a/pkg/drink/conversions.go b/pkg/drink/conversions.go index 5bd81b1..d589b94 100644 --- a/pkg/drink/conversions.go +++ b/pkg/drink/conversions.go @@ -12,6 +12,11 @@ func fromDb(d Model) (Drink, error) { return Drink{}, err } + underDevelopment := false + if d.UnderDevelopment == 1 { + underDevelopment = true + } + return Drink{ ID: d.ID, Username: d.Username, @@ -23,6 +28,7 @@ func fromDb(d Model) (Drink, error) { Instructions: d.Instructions, Notes: d.Notes, Publicity: d.Publicity, + UnderDevelopment: underDevelopment, }, }, nil } @@ -33,6 +39,11 @@ func toDb(d Drink) (Model, error) { return Model{}, err } + underDevelopment := 0 + if d.UnderDevelopment { + underDevelopment = 1 + } + return Model{ ID: d.ID, Name: d.Name, @@ -43,6 +54,7 @@ func toDb(d Drink) (Model, error) { Instructions: d.Instructions, Notes: d.Notes, Publicity: d.Publicity, + UnderDevelopment: underDevelopment, }, nil } @@ -70,4 +82,5 @@ func setDrinkDataAttributes(obj DrinkDataSetter, data DrinkDataGetter) { obj.SetInstructions(data.GetInstructions()) obj.SetNotes(data.GetNotes()) obj.SetPublicity(data.GetPublicity()) + obj.SetUnderDevelopment(data.GetUnderDevelopment()) } diff --git a/pkg/drink/db.go b/pkg/drink/db.go index aa22af2..4408311 100644 --- a/pkg/drink/db.go +++ b/pkg/drink/db.go @@ -14,15 +14,16 @@ const ( ) type Model struct { - ID int64 `db:"id"` - Name string `db:"name" fieldtag:"required_insert"` - Username string `db:"username" fieldtag:"required_insert"` - PrimaryAlcohol string `db:"primary_alcohol" fieldtag:"required_insert"` - PreferredGlass string `db:"preferred_glass" fieldtag:"required_insert"` - Ingredients string `db:"ingredients" fieldtag:"required_insert"` - Instructions string `db:"instructions" fieldtag:"required_insert"` - Notes string `db:"notes" fieldtag:"required_insert"` - Publicity string `db:"publicity" fieldtag:"required_insert"` + ID int64 `db:"id"` + Name string `db:"name" fieldtag:"required_insert"` + Username string `db:"username" fieldtag:"required_insert"` + PrimaryAlcohol string `db:"primary_alcohol" fieldtag:"required_insert"` + PreferredGlass string `db:"preferred_glass" fieldtag:"required_insert"` + Ingredients string `db:"ingredients" fieldtag:"required_insert"` + Instructions string `db:"instructions" fieldtag:"required_insert"` + Notes string `db:"notes" fieldtag:"required_insert"` + Publicity string `db:"publicity" fieldtag:"required_insert"` + UnderDevelopment int `db:"under_development" fieldtag:"required_insert"` } func getByID(id int64, db *sql.DB) (*Model, error) { diff --git a/pkg/drink/drink_data.go b/pkg/drink/drink_data.go index f30b880..ac3b8bd 100644 --- a/pkg/drink/drink_data.go +++ b/pkg/drink/drink_data.go @@ -1,13 +1,14 @@ package drink type DrinkData struct { - Name string `json:"name" validate:"required"` - PrimaryAlcohol string `json:"primary_alcohol" validate:"required"` - PreferredGlass string `json:"preferred_glass,omitempty"` - Ingredients []string `json:"ingredients" validate:"required"` - Instructions string `json:"instructions,omitempty"` - Notes string `json:"notes,omitempty"` - Publicity string `json:"publicity" validate:"required"` + Name string `json:"name" validate:"required"` + PrimaryAlcohol string `json:"primary_alcohol" validate:"required"` + PreferredGlass string `json:"preferred_glass,omitempty"` + Ingredients []string `json:"ingredients" validate:"required"` + Instructions string `json:"instructions,omitempty"` + Notes string `json:"notes,omitempty"` + Publicity string `json:"publicity" validate:"required"` + UnderDevelopment bool `json:"under_development"` } func (d *DrinkData) SetName(v string) { @@ -66,6 +67,14 @@ func (d DrinkData) GetPublicity() string { return d.Publicity } +func (d *DrinkData) SetUnderDevelopment(v bool) { + d.UnderDevelopment = v +} + +func (d DrinkData) GetUnderDevelopment() bool { + return d.UnderDevelopment +} + type DrinkDataOperator interface { DrinkDataSetter DrinkDataGetter @@ -79,6 +88,7 @@ type DrinkDataSetter interface { SetInstructions(string) SetNotes(string) SetPublicity(string) + SetUnderDevelopment(bool) } type DrinkDataGetter interface { @@ -89,4 +99,5 @@ type DrinkDataGetter interface { GetInstructions() string GetNotes() string GetPublicity() string + GetUnderDevelopment() bool } diff --git a/pkg/drink/drink_test.go b/pkg/drink/drink_test.go index cebd465..581134b 100644 --- a/pkg/drink/drink_test.go +++ b/pkg/drink/drink_test.go @@ -185,6 +185,7 @@ func TestFullCRUDLoop(t *testing.T) { "1 oz lime", }, Publicity: DrinkPublicityPrivate, + UnderDevelopment: true, } updatedDrinkData := DrinkData{ Name: "Daquari", @@ -196,6 +197,7 @@ func TestFullCRUDLoop(t *testing.T) { "0.75 oz lime", }, Publicity: DrinkPublicityPrivate, + UnderDevelopment: false, } body := CreateDrinkRequest{DrinkData: origDrinkData} From 8c3b812bc222d30b06caeb9a93ef34dfbb0d9f20 Mon Sep 17 00:00:00 2001 From: Nic Johnson Date: Sun, 24 Jul 2022 16:30:30 -0500 Subject: [PATCH 2/2] chore: go fmt --- cmd/mixer-server/main.go | 2 +- cmd/register/main.go | 22 +++++++++++----------- pkg/drink/conversions.go | 36 ++++++++++++++++++------------------ pkg/drink/drink_test.go | 4 ++-- pkg/slow/slowdown.go | 3 +-- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/cmd/mixer-server/main.go b/cmd/mixer-server/main.go index 8ebac2a..3d941da 100644 --- a/cmd/mixer-server/main.go +++ b/cmd/mixer-server/main.go @@ -8,8 +8,8 @@ import ( "github.com/nicjohnson145/mixer-service/pkg/drink" "github.com/nicjohnson145/mixer-service/pkg/health" "github.com/nicjohnson145/mixer-service/pkg/settings" - "github.com/nicjohnson145/mixer-service/pkg/user" "github.com/nicjohnson145/mixer-service/pkg/slow" + "github.com/nicjohnson145/mixer-service/pkg/user" log "github.com/sirupsen/logrus" ) diff --git a/cmd/register/main.go b/cmd/register/main.go index 542ede0..500fafe 100644 --- a/cmd/register/main.go +++ b/cmd/register/main.go @@ -1,21 +1,21 @@ package main import ( + "context" "flag" - "os" "github.com/carlmjohnson/requests" - log "github.com/sirupsen/logrus" "github.com/nicjohnson145/mixer-service/pkg/auth" "github.com/nicjohnson145/mixer-service/pkg/common" "github.com/nicjohnson145/mixer-service/pkg/jwt" - "context" + log "github.com/sirupsen/logrus" + "os" ) var ( - username string - newUser string + username string + newUser string newPassword string - host string + host string ) func init() { @@ -63,11 +63,11 @@ func main() { } err = requests. - URL(host + common.AuthV1 + "/register-user"). - Header(jwt.AuthenticationHeader, loginResp.AccessToken). - BodyJSON(®isterRequest). - Fetch(context.Background()) - + URL(host+common.AuthV1+"/register-user"). + Header(jwt.AuthenticationHeader, loginResp.AccessToken). + BodyJSON(®isterRequest). + Fetch(context.Background()) + if err != nil { log.Fatalf("Error registering user: %v", err) } diff --git a/pkg/drink/conversions.go b/pkg/drink/conversions.go index d589b94..0c0f903 100644 --- a/pkg/drink/conversions.go +++ b/pkg/drink/conversions.go @@ -21,14 +21,14 @@ func fromDb(d Model) (Drink, error) { ID: d.ID, Username: d.Username, DrinkData: DrinkData{ - Name: d.Name, - PrimaryAlcohol: d.PrimaryAlcohol, - PreferredGlass: d.PreferredGlass, - Ingredients: ingredients, - Instructions: d.Instructions, - Notes: d.Notes, - Publicity: d.Publicity, - UnderDevelopment: underDevelopment, + Name: d.Name, + PrimaryAlcohol: d.PrimaryAlcohol, + PreferredGlass: d.PreferredGlass, + Ingredients: ingredients, + Instructions: d.Instructions, + Notes: d.Notes, + Publicity: d.Publicity, + UnderDevelopment: underDevelopment, }, }, nil } @@ -45,16 +45,16 @@ func toDb(d Drink) (Model, error) { } return Model{ - ID: d.ID, - Name: d.Name, - Username: d.Username, - PrimaryAlcohol: d.PrimaryAlcohol, - PreferredGlass: d.PreferredGlass, - Ingredients: ingredients, - Instructions: d.Instructions, - Notes: d.Notes, - Publicity: d.Publicity, - UnderDevelopment: underDevelopment, + ID: d.ID, + Name: d.Name, + Username: d.Username, + PrimaryAlcohol: d.PrimaryAlcohol, + PreferredGlass: d.PreferredGlass, + Ingredients: ingredients, + Instructions: d.Instructions, + Notes: d.Notes, + Publicity: d.Publicity, + UnderDevelopment: underDevelopment, }, nil } diff --git a/pkg/drink/drink_test.go b/pkg/drink/drink_test.go index 581134b..f21bdf4 100644 --- a/pkg/drink/drink_test.go +++ b/pkg/drink/drink_test.go @@ -184,7 +184,7 @@ func TestFullCRUDLoop(t *testing.T) { "0.5 oz simple syrup", "1 oz lime", }, - Publicity: DrinkPublicityPrivate, + Publicity: DrinkPublicityPrivate, UnderDevelopment: true, } updatedDrinkData := DrinkData{ @@ -196,7 +196,7 @@ func TestFullCRUDLoop(t *testing.T) { "0.5 oz simple syrup", "0.75 oz lime", }, - Publicity: DrinkPublicityPrivate, + Publicity: DrinkPublicityPrivate, UnderDevelopment: false, } diff --git a/pkg/slow/slowdown.go b/pkg/slow/slowdown.go index 86c6db2..6aef948 100644 --- a/pkg/slow/slowdown.go +++ b/pkg/slow/slowdown.go @@ -2,12 +2,11 @@ package slow import ( "github.com/gofiber/fiber/v2" - "os" log "github.com/sirupsen/logrus" + "os" "time" ) - func SlowDown(app *fiber.App) { val, ok := os.LookupEnv("SLOWDOWN_AMOUNT") if !ok {