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
e88e0b04
Commit
e88e0b04
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Update member balance when purchase
parent
74889f9d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/purchase.go
+14
-3
14 additions, 3 deletions
api/purchase.go
api/purchase_test.go
+9
-0
9 additions, 0 deletions
api/purchase_test.go
with
23 additions
and
3 deletions
api/purchase.go
+
14
−
3
View file @
e88e0b04
...
@@ -56,11 +56,14 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
...
@@ -56,11 +56,14 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
}
}
total
:=
0
total
:=
0
for
i
,
p
:=
range
products
{
for
i
,
p
:=
range
products
{
if
p
.
ProductCode
==
0
{
continue
}
var
product
Product
var
product
Product
err
:
=
a
.
db
.
Where
(
"code = ?"
,
p
.
ProductCode
)
.
First
(
&
product
)
.
Error
err
=
a
.
db
.
Where
(
"code = ?"
,
p
.
ProductCode
)
.
First
(
&
product
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Can't get product %d: %v"
,
p
.
ProductCode
,
err
)
log
.
Printf
(
"Can't get product %d: %v"
,
p
.
ProductCode
,
err
)
w
.
WriteHeader
(
http
.
Status
InternalServerError
)
w
.
WriteHeader
(
http
.
Status
BadRequest
)
return
return
}
}
...
@@ -68,6 +71,14 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
...
@@ -68,6 +71,14 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
products
[
i
]
.
Price
=
product
.
Price
products
[
i
]
.
Price
=
product
.
Price
}
}
err
=
a
.
db
.
Model
(
&
Member
{})
.
Where
(
"num = ?"
,
num
)
.
Update
(
"balance"
,
gorm
.
Expr
(
"balance - ?"
,
total
))
.
Error
if
err
!=
nil
{
log
.
Printf
(
"Can't update update member balance %d-%d: %v"
,
num
,
total
,
err
)
w
.
WriteHeader
(
http
.
StatusBadRequest
)
return
}
purchase
:=
Purchase
{
purchase
:=
Purchase
{
MemberNum
:
num
,
MemberNum
:
num
,
Date
:
time
.
Now
(),
Date
:
time
.
Now
(),
...
@@ -86,7 +97,7 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
...
@@ -86,7 +97,7 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
Where
(
"code = ?"
,
p
.
ProductCode
)
.
Where
(
"code = ?"
,
p
.
ProductCode
)
.
Update
(
"stock"
,
gorm
.
Expr
(
"stock - ?"
,
p
.
Ammount
))
.
Error
Update
(
"stock"
,
gorm
.
Expr
(
"stock - ?"
,
p
.
Ammount
))
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"Can't update product stock %d: %v"
,
p
.
ProductCode
,
err
)
log
.
Printf
(
"Can't update product stock
%d-
%d: %v"
,
p
.
ProductCode
,
p
.
Ammount
,
err
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
api/purchase_test.go
+
9
−
0
View file @
e88e0b04
...
@@ -51,4 +51,13 @@ func TestPurchaseAddListMine(t *testing.T) {
...
@@ -51,4 +51,13 @@ func TestPurchaseAddListMine(t *testing.T) {
if
product
.
Stock
!=
testProduct
.
Stock
-
products
[
0
]
.
Ammount
{
if
product
.
Stock
!=
testProduct
.
Stock
-
products
[
0
]
.
Ammount
{
t
.
Error
(
"Wrong product stock:"
,
product
)
t
.
Error
(
"Wrong product stock:"
,
product
)
}
}
var
member
Member
resp
=
tapi
.
do
(
"GET"
,
"/member/10"
,
nil
,
&
member
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Error
(
"Can't find the member:"
,
resp
.
Status
)
}
if
member
.
Balance
!=
testMember
.
Balance
-
(
testProduct
.
Price
*
products
[
0
]
.
Ammount
)
{
t
.
Error
(
"Wrong product balance:"
,
member
.
Balance
)
}
}
}
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