-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,479 additions
and
613 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS product; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS product | ||
( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
status INTEGER NOT NULL DEFAULT 0 | ||
); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE IF EXISTS sale_order_product; | ||
|
||
DROP TABLE IF EXISTS sale_order; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE TABLE IF NOT EXISTS sale_order | ||
( | ||
id INTEGER PRIMARY KEY, | ||
number TEXT UNIQUE NOT NULL, | ||
date TIMESTAMP WITH TIME ZONE NOT NULL, | ||
status INTEGER NOT NULL DEFAULT 0 | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS sale_order_product | ||
( | ||
id INTEGER PRIMARY KEY, | ||
parent_id INTEGER, | ||
product_id INTEGER NOT NULL, | ||
quantity DECIMAL NOT NULL, | ||
price DECIMAL NOT NULL, | ||
FOREIGN KEY(parent_id) REFERENCES sale_order(id), | ||
FOREIGN KEY(product_id) REFERENCES product(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
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
189 changes: 189 additions & 0 deletions
189
internal/adapters/repositories/document/sale_order/sale_order.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package sale_order | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/kiaplayer/clean-architecture-example/internal/domain/entity/document" | ||
"github.com/kiaplayer/clean-architecture-example/internal/domain/entity/reference" | ||
"github.com/kiaplayer/clean-architecture-example/pkg/helpers" | ||
"github.com/kiaplayer/clean-architecture-example/pkg/storage/db" | ||
) | ||
|
||
type Repository struct { | ||
*db.TransactionalRepository | ||
} | ||
|
||
func NewRepository(qe db.QueryExecutor) *Repository { | ||
return &Repository{ | ||
TransactionalRepository: db.NewTransactionalRepository(qe), | ||
} | ||
} | ||
|
||
func (r *Repository) CreateOrder(ctx context.Context, order *document.SaleOrder) (*document.SaleOrder, error) { | ||
insertResult, err := r.DB(ctx).ExecContext( | ||
ctx, | ||
"INSERT INTO sale_order (number, date, status) VALUES (?, ?, ?)", | ||
order.Number, | ||
helpers.TimeToString(order.Date), | ||
order.Status, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lastID, err := insertResult.LastInsertId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
order.ID = uint64(lastID) | ||
|
||
for i, product := range order.Products { | ||
insertResult, err := r.DB(ctx).ExecContext( | ||
ctx, | ||
"INSERT INTO sale_order_product (parent_id, product_id, quantity, price) VALUES (?, ?, ?, ?)", | ||
order.ID, | ||
product.Product.ID, | ||
product.Quantity, | ||
product.Price, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lastID, err := insertResult.LastInsertId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
order.Products[i].ID = uint64(lastID) | ||
} | ||
|
||
return order, nil | ||
} | ||
|
||
func (r *Repository) GetByID(ctx context.Context, id uint64) (*document.SaleOrder, error) { | ||
saleOrderDTO := struct { | ||
ID uint64 | ||
Number string | ||
Date string | ||
Status int | ||
}{} | ||
|
||
queryResult, err := r.DB(ctx).QueryContext( | ||
ctx, | ||
"SELECT id, date, number, status FROM sale_order WHERE id = ?", | ||
id, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func(queryResult *sql.Rows) { | ||
_ = queryResult.Close() | ||
}(queryResult) | ||
|
||
if queryResult.Next() { | ||
err = queryResult.Scan(&saleOrderDTO.ID, &saleOrderDTO.Date, &saleOrderDTO.Number, &saleOrderDTO.Status) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
if queryResult.Err() != nil { | ||
return nil, queryResult.Err() | ||
} | ||
return nil, nil | ||
} | ||
|
||
date, err := helpers.StringToTime(saleOrderDTO.Date) | ||
if err != nil { | ||
return nil, fmt.Errorf("bad date: %s", saleOrderDTO.Date) | ||
} | ||
|
||
status := document.Status(saleOrderDTO.Status) | ||
if !slices.Contains(document.ValidStatuses, status) { | ||
return nil, fmt.Errorf("bad status: %d", status) | ||
} | ||
|
||
result := &document.SaleOrder{ | ||
Document: document.Document{ | ||
ID: saleOrderDTO.ID, | ||
Number: saleOrderDTO.Number, | ||
Date: date, | ||
Status: status, | ||
}, | ||
} | ||
|
||
saleOrderProductDTO := struct { | ||
ID uint64 | ||
ProductID uint64 | ||
ProductName string | ||
ProductStatus int | ||
Quantity int | ||
Price float32 | ||
}{} | ||
|
||
queryResult, err = r.DB(ctx).QueryContext( | ||
ctx, | ||
` | ||
SELECT | ||
sop.id, | ||
sop.product_id, | ||
sop.quantity, | ||
sop.price, | ||
p.name, | ||
p.status | ||
FROM sale_order_product AS sop | ||
LEFT JOIN product AS p ON p.id = sop.product_id | ||
WHERE sop.parent_id = ? | ||
`, | ||
result.ID, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func(queryResult *sql.Rows) { | ||
_ = queryResult.Close() | ||
}(queryResult) | ||
|
||
for queryResult.Next() { | ||
err = queryResult.Scan( | ||
&saleOrderProductDTO.ID, | ||
&saleOrderProductDTO.ProductID, | ||
&saleOrderProductDTO.Quantity, | ||
&saleOrderProductDTO.Price, | ||
&saleOrderProductDTO.ProductName, | ||
&saleOrderProductDTO.ProductStatus, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
productStatus := reference.Status(saleOrderProductDTO.ProductStatus) | ||
if !slices.Contains(reference.ValidStatuses, productStatus) { | ||
return nil, fmt.Errorf("bad status: %d", productStatus) | ||
} | ||
|
||
result.Products = append(result.Products, document.SaleOrderProduct{ | ||
ID: saleOrderProductDTO.ID, | ||
Product: reference.Product{ | ||
Reference: reference.Reference{ | ||
ID: saleOrderProductDTO.ProductID, | ||
Name: saleOrderProductDTO.ProductName, | ||
Status: productStatus, | ||
}, | ||
}, | ||
Quantity: saleOrderProductDTO.Quantity, | ||
Price: saleOrderProductDTO.Price, | ||
}) | ||
} | ||
|
||
if queryResult.Err() != nil { | ||
return nil, queryResult.Err() | ||
} | ||
|
||
return result, 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
Oops, something went wrong.