This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Meal model a record of meals table | |
type Meal struct { | |
ID uint | |
Name string | |
Description string | |
DrinkID uint | |
Drink Drink | |
} | |
type Meals []Meal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Waitress struct { | |
db *sql.DB | |
} | |
// getMeals gets 20 meals from the db | |
func (b Waitress) getMeals() (Meals, error) { | |
// this method has the logic to query from the db, if you want to see | |
// the actual code, you can go the repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (b Waitress) ListMenu() (Meals, error) { | |
// the `1` of our `N+1` | |
meals, err := b.getMeals() | |
if err != nil { | |
return nil, err | |
} | |
// here is our waitress going back and forth | |
// from floor 1 to 10, this will be the `N` of our `N+1` | |
for i, meal := range meals { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT m.id, m.name, m.description, d.name, d.description | |
FROM meals AS m | |
INNER JOIN drinks AS d ON d.id = m.drink_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Solution1 struct { | |
db *sql.DB | |
} | |
func (s Solution1) ListMenu() (Meals, error) { | |
// this method will execute the previous SQL | |
// SELECT | |
// m.id, | |
// m.name, | |
// m.description, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s Solution2) ListMenu() (Meals, error) { | |
meals, err := s.getMeals() | |
if err != nil { | |
return nil, err | |
} | |
// this is our waitress bringing everything in a bussing cart | |
drinks, err := s.getDrinksByIDsIn(meals.GetUniqueDrinkIDs()) | |
if err != nil { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to query the drinks, we need to know their IDs, for that | |
// we add this method to our `Meals` slice | |
func (m Meals) GetUniqueDrinkIDs() []uint { | |
var ids []uint | |
drinks := make(map[uint]struct{}, 0) | |
for _, v := range m { | |
_, ok := drinks[v.DrinkID] | |
if ok { | |
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE users | |
( | |
id SERIAL NOT NULL, | |
first_name VARCHAR(100) NOT NULL, | |
last_name VARCHAR(100), | |
email VARCHAR(100) NOT NULL, | |
password VARCHAR(256) NOT NULL | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func init() { | |
goose.AddMigration(upCreateUsersTable, downCreateUsersTable) | |
} | |
func upCreateUsersTable(tx *sql.Tx) error { | |
_, err := tx.Exec(` | |
CREATE TABLE users | |
( | |
id SERIAL NOT NULL, | |
first_name VARCHAR(100) NOT NULL, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PaymentMethodStrategy will let us interchange | |
// the payment method in the context | |
type PaymentMethodStrategy interface { | |
Pay() error | |
} |
OlderNewer