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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Quique
cicer
Commits
4f058ca1
Commit
4f058ca1
authored
Oct 30, 2020
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Delete orders
parent
1fe9a34f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
api/api.go
+1
-0
1 addition, 0 deletions
api/api.go
api/db/order.go
+47
-2
47 additions, 2 deletions
api/db/order.go
api/db/transaction.go
+2
-2
2 additions, 2 deletions
api/db/transaction.go
api/order.go
+23
-0
23 additions, 0 deletions
api/order.go
api/order_test.go
+79
-0
79 additions, 0 deletions
api/order_test.go
with
152 additions
and
4 deletions
api/api.go
+
1
−
0
View file @
4f058ca1
...
@@ -57,6 +57,7 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error {
...
@@ -57,6 +57,7 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error {
r
.
HandleFunc
(
"/order"
,
a
.
auth
(
a
.
ListOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order"
,
a
.
auth
(
a
.
ListOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order"
,
a
.
authNum
(
a
.
AddOrder
))
.
Methods
(
"POST"
)
r
.
HandleFunc
(
"/order"
,
a
.
authNum
(
a
.
AddOrder
))
.
Methods
(
"POST"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}"
,
a
.
authNum
(
a
.
GetOrder
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}"
,
a
.
authNum
(
a
.
GetOrder
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}"
,
a
.
authNumRole
(
a
.
DeleteOrder
))
.
Methods
(
"DELETE"
)
r
.
HandleFunc
(
"/order/active"
,
a
.
auth
(
a
.
ListActiveOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/active"
,
a
.
auth
(
a
.
ListActiveOrders
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}/purchase"
,
a
.
authNum
(
a
.
AddOrderPurchase
))
.
Methods
(
"POST"
)
r
.
HandleFunc
(
"/order/{id:[0-9]+}/purchase"
,
a
.
authNum
(
a
.
AddOrderPurchase
))
.
Methods
(
"POST"
)
return
nil
return
nil
...
...
This diff is collapsed.
Click to expand it.
api/db/order.go
+
47
−
2
View file @
4f058ca1
...
@@ -48,10 +48,13 @@ func (d *DB) GetOrder(memberNum int, id int) (order Order, transaction Transacti
...
@@ -48,10 +48,13 @@ func (d *DB) GetOrder(memberNum int, id int) (order Order, transaction Transacti
Preload
(
"Transactions.OrderPurchase"
)
.
Preload
(
"Transactions.OrderPurchase"
)
.
Preload
(
"Transactions.Member"
)
.
Preload
(
"Transactions.Member"
)
.
First
(
&
order
,
id
)
.
Error
First
(
&
order
,
id
)
.
Error
if
err
!=
nil
{
if
errors
.
Is
(
err
,
gorm
.
ErrRecordNotFound
)
{
if
errors
.
Is
(
err
,
gorm
.
ErrRecordNotFound
)
{
err
=
ErrorNotFound
err
=
ErrorNotFound
return
return
}
}
return
}
err
=
d
.
db
.
Where
(
"member = ? AND type = 'order' AND order_id = ?"
,
memberNum
,
id
)
.
err
=
d
.
db
.
Where
(
"member = ? AND type = 'order' AND order_id = ?"
,
memberNum
,
id
)
.
Preload
(
"OrderPurchase.Product"
)
.
Preload
(
"OrderPurchase.Product"
)
.
Find
(
&
transaction
)
.
Error
Find
(
&
transaction
)
.
Error
...
@@ -62,6 +65,48 @@ func (d *DB) AddOrder(order *Order) error {
...
@@ -62,6 +65,48 @@ func (d *DB) AddOrder(order *Order) error {
return
d
.
db
.
Create
(
&
order
)
.
Error
return
d
.
db
.
Create
(
&
order
)
.
Error
}
}
func
(
d
*
DB
)
DeleteOrder
(
memberNum
int
,
id
int
)
error
{
var
order
Order
err
:=
d
.
db
.
Preload
(
clause
.
Associations
)
.
Preload
(
"Transactions.OrderPurchase"
)
.
Preload
(
"Transactions.Member"
)
.
First
(
&
order
,
id
)
.
Error
if
err
!=
nil
{
if
errors
.
Is
(
err
,
gorm
.
ErrRecordNotFound
)
{
return
ErrorNotFound
}
return
err
}
if
memberNum
!=
0
&&
order
.
MemberNum
!=
memberNum
{
return
ErrorInvalidRequest
}
return
d
.
db
.
Transaction
(
func
(
tx
*
gorm
.
DB
)
error
{
if
order
.
TransactionID
!=
nil
{
var
transaction
Transaction
err
=
tx
.
First
(
&
transaction
,
order
.
TransactionID
)
.
Error
if
err
!=
nil
{
return
err
}
order
.
Transactions
=
append
(
order
.
Transactions
,
transaction
)
}
for
_
,
transaction
:=
range
order
.
Transactions
{
err
:=
updateMemberBalance
(
tx
,
transaction
.
MemberNum
,
-
transaction
.
Total
)
if
err
!=
nil
{
return
err
}
err
=
tx
.
Select
(
"OrderPurchase"
)
.
Delete
(
&
transaction
)
.
Error
if
err
!=
nil
{
return
err
}
}
return
tx
.
Delete
(
&
order
)
.
Error
})
}
func
(
d
*
DB
)
AddOrderPurchase
(
memberNum
int
,
orderID
int
,
purchase
[]
OrderPurchase
)
(
transaction
Transaction
,
err
error
)
{
func
(
d
*
DB
)
AddOrderPurchase
(
memberNum
int
,
orderID
int
,
purchase
[]
OrderPurchase
)
(
transaction
Transaction
,
err
error
)
{
order
,
transaction
,
err
:=
d
.
GetOrder
(
memberNum
,
orderID
)
order
,
transaction
,
err
:=
d
.
GetOrder
(
memberNum
,
orderID
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
api/db/transaction.go
+
2
−
2
View file @
4f058ca1
...
@@ -20,8 +20,8 @@ type Transaction struct {
...
@@ -20,8 +20,8 @@ type Transaction struct {
Purchase
[]
Purchase
`json:"purchase,omitempty"`
Purchase
[]
Purchase
`json:"purchase,omitempty"`
Topup
*
Topup
`json:"topup,omitempty"`
Topup
*
Topup
`json:"topup,omitempty"`
OrderPurchase
[]
OrderPurchase
`json:"order_purchase,omitempty" gorm:"foreignKey:TransactionID"`
OrderPurchase
[]
OrderPurchase
`json:"order_purchase,omitempty" gorm:"foreignKey:TransactionID
;constraint:OnDelete:CASCADE
"`
Order
*
Order
`json:"order,omitempty"`
Order
*
Order
`json:"order,omitempty"
gorm:"constraint:OnDelete:CASCADE"
`
OrderID
*
uint
`json:"-"`
OrderID
*
uint
`json:"-"`
Refund
*
Order
`json:"refund,omitempty" gorm:"foreignKey:TransactionID"`
Refund
*
Order
`json:"refund,omitempty" gorm:"foreignKey:TransactionID"`
}
}
...
...
This diff is collapsed.
Click to expand it.
api/order.go
+
23
−
0
View file @
4f058ca1
...
@@ -86,6 +86,29 @@ func (a *api) GetOrder(num int, w http.ResponseWriter, req *http.Request) {
...
@@ -86,6 +86,29 @@ func (a *api) GetOrder(num int, w http.ResponseWriter, req *http.Request) {
}
}
}
}
func
(
a
*
api
)
DeleteOrder
(
num
int
,
role
string
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
req
)
id
,
_
:=
strconv
.
Atoi
(
vars
[
"id"
])
if
role
==
"admin"
{
num
=
0
}
err
:=
a
.
db
.
DeleteOrder
(
num
,
id
)
if
err
!=
nil
{
if
errors
.
Is
(
err
,
db
.
ErrorNotFound
)
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
return
}
if
errors
.
Is
(
err
,
db
.
ErrorInvalidRequest
)
{
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
return
}
log
.
Printf
(
"Can't get order %s: %v"
,
vars
[
"id"
],
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
return
}
w
.
WriteHeader
(
http
.
StatusOK
)
}
func
(
a
*
api
)
AddOrder
(
num
int
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
(
a
*
api
)
AddOrder
(
num
int
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
var
order
db
.
Order
var
order
db
.
Order
err
:=
json
.
NewDecoder
(
req
.
Body
)
.
Decode
(
&
order
)
err
:=
json
.
NewDecoder
(
req
.
Body
)
.
Decode
(
&
order
)
...
...
This diff is collapsed.
Click to expand it.
api/order_test.go
+
79
−
0
View file @
4f058ca1
...
@@ -66,6 +66,85 @@ func TestOrderActive(t *testing.T) {
...
@@ -66,6 +66,85 @@ func TestOrderActive(t *testing.T) {
}
}
}
}
func
TestOrderDelete
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
tapi
.
addTestMember
()
order
:=
testOrder
order
.
Deadline
=
time
.
Now
()
.
Add
(
-
24
*
time
.
Hour
)
resp
:=
tapi
.
do
(
"POST"
,
"/order"
,
order
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
t
.
Fatal
(
"Can't create order:"
,
resp
.
Status
)
}
var
orders
[]
db
.
Order
resp
=
tapi
.
do
(
"GET"
,
"/order"
,
nil
,
&
orders
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
purchase
:=
[]
db
.
OrderPurchase
{
{
ProductCode
:
testProduct
.
Code
,
Amount
:
3
,
},
}
resp
=
tapi
.
doAdmin
(
"POST"
,
fmt
.
Sprintf
(
"/order/%d/purchase"
,
orders
[
0
]
.
ID
),
purchase
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
t
.
Fatal
(
"Can't create order purchase:"
,
resp
.
Status
)
}
dbPath
:=
path
.
Join
(
tapi
.
testPath
,
"test.db"
)
database
,
err
:=
db
.
Init
(
dbPath
)
if
err
!=
nil
{
t
.
Fatal
(
"Can't initialize the db:"
,
err
)
}
orders
=
database
.
DeactivateOrders
()
if
len
(
orders
)
!=
1
{
t
.
Error
(
"Deactivated wrong orders:"
,
orders
)
}
resp
=
tapi
.
do
(
"DELETE"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
nil
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't delete order:"
,
resp
.
Status
)
}
resp
=
tapi
.
do
(
"GET"
,
"/order"
,
nil
,
&
orders
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
if
len
(
orders
)
!=
0
{
t
.
Error
(
"The order didn't get removed"
,
orders
)
}
var
transactions
[]
db
.
Transaction
resp
=
tapi
.
doAdmin
(
"GET"
,
"/transaction"
,
nil
,
&
transactions
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
if
len
(
transactions
)
!=
0
{
t
.
Error
(
"The transactions didn't get removed"
,
transactions
)
}
var
member
db
.
Member
resp
=
tapi
.
doAdmin
(
"GET"
,
fmt
.
Sprintf
(
"/member/%d"
,
testMember
.
Num
),
nil
,
&
member
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
if
member
.
Balance
!=
testMember
.
Balance
{
t
.
Error
(
"Wrong balance on test member:"
,
member
.
Balance
)
}
resp
=
tapi
.
doAdmin
(
"GET"
,
fmt
.
Sprintf
(
"/member/%d"
,
testMemberAdmin
.
Num
),
nil
,
&
member
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
if
member
.
Balance
!=
testMemberAdmin
.
Balance
{
t
.
Error
(
"Wrong balance on admin member:"
,
member
.
Balance
)
}
}
func
TestOrderPurchase
(
t
*
testing
.
T
)
{
func
TestOrderPurchase
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
defer
tapi
.
close
()
...
...
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