Skip to content

Commit

Permalink
馃 refactor: update product scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
shurco committed Jul 28, 2023
1 parent b7b71ef commit 7516ec1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
18 changes: 9 additions & 9 deletions fixtures/migration/20230714135999_migration_name.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ UPDATE setting SET value = '48' WHERE key = 'jwt_secret_expire_hours';
UPDATE setting SET value = 'sk_test_000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' WHERE key = 'stripe_secret_key';
UPDATE setting SET value = '' WHERE key = 'stripe_webhook_secret_key';

INSERT INTO product (id, name, desc, url, metadata, attribute) VALUES
('fv6c9s9cqzf36sc', 'name1', 'description1', 'url1', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('xrtb1b919t2nuj9', 'name2', 'description2', 'url2', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('7mweb67t8xv9pzx', 'name3', 'description3', 'url3', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('k4pkxqhn4p0xhoc', 'name4', 'description4', 'url4', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('2wdx6k7b3lywc2o', 'name5', 'description5', 'url5', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('zlfpc6b17gte0ot', 'name6', 'description6', 'url6', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('ktorsk0xj8w5zab', 'name7', 'description7', 'url7', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('6bn739vrvfp6zaw', 'name8', 'description8', 'url8', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]');
INSERT INTO product (id, stripe_id, name, desc, url, metadata, attribute) VALUES
('fv6c9s9cqzf36sc', 'prod_OGdTsykDrQSkmA', 'name1', 'description1', 'url1', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('xrtb1b919t2nuj9', '', 'name2', 'description2', 'url2', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('7mweb67t8xv9pzx', 'prod_OGdTsykDrQSkmb', 'name3', 'description3', 'url3', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('k4pkxqhn4p0xhoc', '', 'name4', 'description4', 'url4', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('2wdx6k7b3lywc2o', '', 'name5', 'description5', 'url5', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('zlfpc6b17gte0ot', '', 'name6', 'description6', 'url6', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('ktorsk0xj8w5zab', '', 'name7', 'description7', 'url7', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]'),
('6bn739vrvfp6zaw', '', 'name8', 'description8', 'url8', '{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4", "key5":"value5", "key6":"value6"}', '["atribute1" ,"atribute2", "atribute3"]');

INSERT INTO product_price (id, stripe_id, product_id, currency, amount) VALUES
('ohrozxu47cwnaup', 'price_1NU6CfBDuthUZlLWNncOPN53', 'fv6c9s9cqzf36sc', 'EUR', 2000),
Expand Down
2 changes: 1 addition & 1 deletion internal/migrations/20230714135923_init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CREATE TABLE product (
metadata JSON DEFAULT "{}" NOT NULL,
attribute JSON DEFAULT "[]" NOT NULL,
created TIMESTAMP DEFAULT (datetime('now')),
updated TIMESTAMP DEFAULT (datetime('now'))
updated TIMESTAMP
);

CREATE TABLE product_price (
Expand Down
1 change: 1 addition & 0 deletions internal/models/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

type Product struct {
ID string `json:"id"`
StripeID string `json:"stripe_id"`
Name string `json:"name" validate:"required"`
Description string `json:"description" validate:"required"`
Price Price `json:"price"`
Expand Down
33 changes: 21 additions & 12 deletions internal/queries/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (q *ProductQueries) ListProducts() ([]models.Product, error) {
products := []models.Product{}

query := `
SELECT product.id, product.name, product.desc, product.url, strftime('%s', product.created), price.id, price.stripe_id, price.currency, price.amount
SELECT product.id, product.stripe_id, product.name, product.desc, product.url, strftime('%s', product.created), price.id, price.stripe_id, price.currency, price.amount
FROM product
JOIN product_price AS price ON product.id = price.product_id
`
Expand All @@ -30,7 +30,9 @@ func (q *ProductQueries) ListProducts() ([]models.Product, error) {

for rows.Next() {
product := models.Product{}
err := rows.Scan(&product.ID,
err := rows.Scan(
&product.ID,
&product.StripeID,
&product.Name,
&product.Description,
&product.URL,
Expand Down Expand Up @@ -61,6 +63,7 @@ func (q *ProductQueries) Product(id string) (*models.Product, error) {

query := `
SELECT
product.stripe_id,
product.name,
product.desc,
product.url,
Expand All @@ -79,11 +82,12 @@ func (q *ProductQueries) Product(id string) (*models.Product, error) {
WHERE product.id = ?
GROUP BY product.id
`
var updated int64
var images, metadata, attributes string
var stripeID, images, metadata, attributes sql.NullString
var updated sql.NullInt64

err := q.DB.QueryRow(query, id).
Scan(
&stripeID,
&product.Name,
&product.Description,
&product.URL,
Expand All @@ -103,20 +107,25 @@ func (q *ProductQueries) Product(id string) (*models.Product, error) {
}
return nil, err
}
if updated != product.Created {
product.Updated = updated

if stripeID.Valid {
product.StripeID = stripeID.String
}

if updated.Valid {
product.Updated = updated.Int64
}

if images != "" {
product.Images = strings.Split(images, ",")
if images.Valid {
product.Images = strings.Split(images.String, ",")
}

if attributes != "" {
json.Unmarshal([]byte(attributes), &product.Attributes)
if attributes.Valid {
json.Unmarshal([]byte(attributes.String), &product.Attributes)
}

if metadata != "" {
json.Unmarshal([]byte(metadata), &product.Metadata)
if metadata.Valid {
json.Unmarshal([]byte(metadata.String), &product.Metadata)
}

return product, nil
Expand Down
6 changes: 3 additions & 3 deletions scripts/migration
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ elif [[ $MIGRATION == "dev" ]]; then
if [ -d "${UPLOADS_DIR}" ]; then
rm -rf ${UPLOADS_DIR}
fi
if [ -d "${LC_BASE_DIR}" ]; then
rm -rf ${LC_BASE_DIR}
fi
#if [ -d "${LC_BASE_DIR}" ]; then
# rm -rf ${LC_BASE_DIR}
#fi
fi
else
print_header "Migrations sql"
Expand Down

0 comments on commit 7516ec1

Please sign in to comment.