Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cicer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Quique
cicer
Commits
606b6392
Commit
606b6392
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Use 'normal' products for orders
parent
ee1ac1d4
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/api.go
+2
-2
2 additions, 2 deletions
api/api.go
api/member_test.go
+1
-1
1 addition, 1 deletion
api/member_test.go
api/order.go
+48
-41
48 additions, 41 deletions
api/order.go
api/order_test.go
+11
-12
11 additions, 12 deletions
api/order_test.go
with
62 additions
and
56 deletions
api/api.go
+
2
−
2
View file @
606b6392
...
...
@@ -20,7 +20,7 @@ func initDB(dbPath string) (*gorm.DB, error) {
}
db
.
AutoMigrate
(
&
Member
{},
&
Product
{},
&
Purchase
{},
&
Topup
{},
&
Transaction
{},
&
OrderPurchase
{},
&
OrderProduct
{},
&
Order
{})
&
OrderPurchase
{},
&
Order
{})
return
db
,
err
}
...
...
@@ -62,7 +62,7 @@ func Init(dbPath string, signKey string, r *mux.Router) error {
r
.
HandleFunc
(
"/order"
,
a
.
auth
(
a
.
ListOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order"
,
a
.
authNum
(
a
.
AddOrder
))
.
Methods
(
"POST"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}"
,
a
.
auth
(
a
.
GetOrder
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}"
,
a
.
auth
Num
(
a
.
GetOrder
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/active"
,
a
.
auth
(
a
.
ListActiveOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/purchase"
,
a
.
authNum
(
a
.
AddOrderPurchase
))
.
Methods
(
"POST"
)
// TODO: r.HandleFunc("/order/purchase", a.authNum(a.UpdateOrderPurchase)).Methods("PUT")
...
...
This diff is collapsed.
Click to expand it.
api/member_test.go
+
1
−
1
View file @
606b6392
...
...
@@ -28,7 +28,7 @@ var testMemberAdmin = struct {
Name
:
"bar"
,
Email
:
"bar@example.com"
,
Role
:
"admin"
,
Balance
:
5000
,
Balance
:
1
5000
,
},
Password
:
"password"
,
}
...
...
This diff is collapsed.
Click to expand it.
api/order.go
+
48
−
41
View file @
606b6392
...
...
@@ -20,25 +20,20 @@ type Order struct {
Deadline
time
.
Time
`json:"deadline"`
Active
bool
`json:"active" gorm:"index"`
Products
[]
OrderProduct
`json:"products"`
TransactionID
*
uint
`json:"transaction" gorm:"column:transaction"`
}
type
OrderProduct
struct
{
gorm
.
Model
Name
string
`json:"name"`
Price
int
`json:"price"`
OrderID
uint
`json:"-"`
Purchases
[]
OrderPurchase
`json:"purchases"`
Products
[]
Product
`json:"products" gorm:"many2many:order_products;References:Code;JoinReferences:ProductCode"`
Purchases
[]
OrderPurchase
`json:"purchases"`
TransactionID
*
uint
`json:"-" gorm:"column:transaction"`
}
type
OrderPurchase
struct
{
gorm
.
Model
OrderProductID
uint
`json:"product_id"`
OrderProduct
Order
`json:"product"`
TransactionID
uint
`json:"-"`
Ammount
int
`json:"ammount"`
gorm
.
Model
`json:"-"`
TransactionID
uint
`json:"-"`
ProductCode
int
`json:"product_code"`
Product
*
Product
`json:"product" gorm:"foreignKey:ProductCode;references:Code"`
OrderID
uint
`json:"order_id"`
Order
*
Order
`json:"-"`
Price
int
`json:"price"`
Ammount
int
`json:"ammount"`
}
func
(
a
*
api
)
refundOrders
()
{
...
...
@@ -54,7 +49,7 @@ func (a *api) deactivateOrders() {
now
:=
time
.
Now
()
t
:=
time
.
Date
(
now
.
Year
(),
now
.
Month
(),
now
.
Day
(),
0
,
0
,
0
,
0
,
time
.
Local
)
err
:=
a
.
db
.
Where
(
"active = ? AND deadline < ?"
,
true
,
t
)
.
Preload
(
"
Products.
Purchases"
)
.
Preload
(
"Purchases"
)
.
Find
(
&
orders
)
.
Error
if
err
!=
nil
{
log
.
Println
(
"Error refunding orders:"
,
err
)
...
...
@@ -62,13 +57,9 @@ func (a *api) deactivateOrders() {
}
for
_
,
order
:=
range
orders
{
log
.
Println
(
"Refund order"
,
order
.
Name
)
total
:=
0
for
_
,
product
:=
range
order
.
Products
{
for
_
,
purchase
:=
range
product
.
Purchases
{
total
+=
product
.
Price
*
purchase
.
Ammount
}
for
_
,
purchase
:=
range
order
.
Purchases
{
total
+=
purchase
.
Price
*
purchase
.
Ammount
}
transaction
:=
Transaction
{
...
...
@@ -93,6 +84,8 @@ func (a *api) deactivateOrders() {
log
.
Printf
(
"Can't create refund: %v
\n
%v"
,
err
,
order
)
continue
}
log
.
Println
(
"Refund order"
,
order
.
Name
,
total
)
}
}
...
...
@@ -106,8 +99,7 @@ func (a *api) ListActiveOrders(w http.ResponseWriter, req *http.Request) {
func
(
a
*
api
)
listOrders
(
active
bool
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
var
orders
[]
Order
query
:=
a
.
db
.
Preload
(
"Products.Purchases"
)
.
Preload
(
clause
.
Associations
)
query
:=
a
.
db
.
Preload
(
clause
.
Associations
)
if
active
{
query
=
query
.
Where
(
"active = ?"
,
true
)
}
...
...
@@ -127,11 +119,10 @@ func (a *api) listOrders(active bool, w http.ResponseWriter, req *http.Request)
}
}
func
(
a
*
api
)
GetOrder
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
(
a
*
api
)
GetOrder
(
num
int
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
req
)
var
order
Order
err
:=
a
.
db
.
Preload
(
"Products.Purchases"
)
.
Preload
(
clause
.
Associations
)
.
err
:=
a
.
db
.
Preload
(
clause
.
Associations
)
.
First
(
&
order
,
vars
[
"id"
])
.
Error
if
err
!=
nil
{
if
err
.
Error
()
==
"record not found"
{
...
...
@@ -142,10 +133,29 @@ func (a *api) GetOrder(w http.ResponseWriter, req *http.Request) {
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
return
}
var
body
struct
{
Order
Order
`json:"order"`
Transaction
*
Transaction
`json:"transaction"`
}
body
.
Order
=
order
var
transaction
Transaction
err
=
a
.
db
.
Joins
(
"OrderPurchase"
)
.
Where
(
"member = ? AND type = 'order' AND order_purchases.order_id = ?"
,
num
,
order
.
ID
)
.
Find
(
&
transaction
)
.
Error
if
err
!=
nil
{
if
err
.
Error
()
!=
"record not found"
{
log
.
Printf
(
"Can't get order transaction %s: %v"
,
vars
[
"id"
],
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
return
}
}
else
{
body
.
Transaction
=
&
transaction
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
WriteHeader
(
http
.
StatusOK
)
err
=
json
.
NewEncoder
(
w
)
.
Encode
(
order
)
err
=
json
.
NewEncoder
(
w
)
.
Encode
(
body
)
if
err
!=
nil
{
log
.
Printf
(
"Can't encode order: %v"
,
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
...
...
@@ -196,15 +206,11 @@ func (a *api) AddOrderPurchase(num int, w http.ResponseWriter, req *http.Request
}
var
order
Order
err
=
a
.
db
.
Where
(
"id = (?)"
,
a
.
db
.
Table
(
"order_products"
)
.
Where
(
"id = ?"
,
purchase
[
0
]
.
OrderProductID
)
.
Select
(
"order_id"
))
.
Preload
(
"Products"
)
.
First
(
&
order
)
.
Error
err
=
a
.
db
.
Preload
(
"Products"
)
.
First
(
&
order
,
purchase
[
0
]
.
OrderID
)
.
Error
if
err
!=
nil
{
log
.
Printf
(
"Can't get order
from product
%d: %v"
,
purchase
[
0
]
.
Order
Product
ID
,
err
)
w
.
WriteHeader
(
http
.
Status
NotAcceptable
)
log
.
Printf
(
"Can't get order %d: %v"
,
purchase
[
0
]
.
OrderID
,
err
)
w
.
WriteHeader
(
http
.
Status
InternalServerError
)
return
}
if
!
order
.
Active
{
...
...
@@ -214,19 +220,20 @@ func (a *api) AddOrderPurchase(num int, w http.ResponseWriter, req *http.Request
}
total
:=
0
for
_
,
p
:=
range
purchase
{
for
i
,
p
:=
range
purchase
{
found
:=
false
for
_
,
product
:=
range
order
.
Products
{
if
product
.
ID
==
p
.
Order
Product
ID
{
if
product
.
Code
==
p
.
Product
Code
{
total
+=
product
.
Price
*
p
.
Ammount
purchase
[
i
]
.
Price
=
product
.
Price
found
=
true
break
}
}
if
!
found
{
log
.
Printf
(
"Order purchase product %d not in order: %v"
,
p
.
Order
Product
ID
,
purchase
)
w
.
WriteHeader
(
http
.
Status
NotAcceptable
)
log
.
Printf
(
"Order purchase product %d not in order: %v"
,
p
.
Product
Code
,
purchase
)
w
.
WriteHeader
(
http
.
Status
BadRequest
)
return
}
}
...
...
This diff is collapsed.
Click to expand it.
api/order_test.go
+
11
−
12
View file @
606b6392
...
...
@@ -11,11 +11,8 @@ var testOrder = Order{
Name
:
"huevos"
,
Description
:
"huevos frescos"
,
Deadline
:
time
.
Now
()
.
Add
(
24
*
time
.
Hour
),
Products
:
[]
OrderProduct
{
{
Name
:
"huevos"
,
Price
:
234
,
},
Products
:
[]
Product
{
testProduct
,
},
}
...
...
@@ -40,7 +37,7 @@ func TestOrderAddList(t *testing.T) {
if
len
(
orders
[
0
]
.
Products
)
!=
1
{
t
.
Fatal
(
"Wrong number of products"
,
len
(
orders
[
0
]
.
Products
),
orders
[
0
]
.
Products
)
}
if
orders
[
0
]
.
Products
[
0
]
.
Price
!=
test
Order
.
Product
s
[
0
]
.
Price
{
if
orders
[
0
]
.
Products
[
0
]
.
Price
!=
testProduct
.
Price
{
t
.
Error
(
"Wrong product price:"
,
orders
[
0
]
.
Products
[
0
]
.
Price
)
}
}
...
...
@@ -79,8 +76,9 @@ func TestOrderPurchase(t *testing.T) {
purchase
:=
[]
OrderPurchase
{
{
OrderProductID
:
orders
[
0
]
.
Products
[
0
]
.
ID
,
Ammount
:
3
,
ProductCode
:
testProduct
.
Code
,
Ammount
:
3
,
OrderID
:
orders
[
0
]
.
ID
,
},
}
resp
=
tapi
.
do
(
"POST"
,
"/order/purchase"
,
purchase
,
nil
)
...
...
@@ -96,7 +94,7 @@ func TestOrderPurchase(t *testing.T) {
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
orders
),
orders
)
}
total
:=
3
*
test
Order
.
Product
s
[
0
]
.
Price
total
:=
3
*
testProduct
.
Price
if
transactions
[
0
]
.
Total
!=
-
total
{
t
.
Fatal
(
"Wrong total"
,
transactions
[
0
]
.
Total
)
}
...
...
@@ -173,8 +171,9 @@ func TestOrderDeactivation(t *testing.T) {
purchase
:=
[]
OrderPurchase
{
{
OrderProductID
:
orders
[
0
]
.
Products
[
0
]
.
ID
,
Ammount
:
3
,
ProductCode
:
testProduct
.
Code
,
Ammount
:
3
,
OrderID
:
orders
[
0
]
.
ID
,
},
}
resp
=
tapi
.
doAdmin
(
"POST"
,
"/order/purchase"
,
purchase
,
nil
)
...
...
@@ -198,7 +197,7 @@ func TestOrderDeactivation(t *testing.T) {
t
.
Fatal
(
"I found some orders"
)
}
total
:=
3
*
test
Order
.
Product
s
[
0
]
.
Price
total
:=
3
*
testProduct
.
Price
var
transactions
[]
Transaction
resp
=
tapi
.
do
(
"GET"
,
"/transaction/mine"
,
nil
,
&
transactions
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment