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
meskio
cicer
Commits
4618bd72
Commit
4618bd72
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Set the total of purchases negative
parent
57a5cc9c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/purchase.go
+25
-19
25 additions, 19 deletions
api/purchase.go
api/purchase_test.go
+1
-1
1 addition, 1 deletion
api/purchase_test.go
src/TransactionList.js
+1
-1
1 addition, 1 deletion
src/TransactionList.js
with
27 additions
and
21 deletions
api/purchase.go
+
25
−
19
View file @
4618bd72
...
...
@@ -40,24 +40,9 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
purchase
[
i
]
.
Price
=
product
.
Price
}
var
member
Member
err
=
a
.
db
.
Where
(
"num = ?"
,
num
)
.
Find
(
&
member
)
.
Error
if
err
!=
nil
{
log
.
Printf
(
"Can't find member %d: %v"
,
num
,
err
)
w
.
WriteHeader
(
http
.
StatusNotAcceptable
)
return
}
if
member
.
Balance
<
total
{
log
.
Printf
(
"Member %d don't have enough money (%d-%d)"
,
num
,
member
.
Balance
,
total
)
w
.
WriteHeader
(
http
.
StatusBadRequest
)
return
}
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
.
StatusNotAcceptable
)
httpStatus
:=
a
.
substractMemberBalance
(
num
,
total
)
if
httpStatus
!=
http
.
StatusOK
{
w
.
WriteHeader
(
httpStatus
)
return
}
...
...
@@ -66,7 +51,7 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
Date
:
time
.
Now
(),
Purchase
:
purchase
,
Type
:
"purchase"
,
Total
:
total
,
Total
:
-
total
,
}
err
=
a
.
db
.
Create
(
&
transaction
)
.
Error
if
err
!=
nil
{
...
...
@@ -93,3 +78,24 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
}
}
func
(
a
*
api
)
substractMemberBalance
(
num
int
,
total
int
)
int
{
var
member
Member
err
:=
a
.
db
.
Where
(
"num = ?"
,
num
)
.
Find
(
&
member
)
.
Error
if
err
!=
nil
{
log
.
Printf
(
"Can't find member %d: %v"
,
num
,
err
)
return
http
.
StatusNotAcceptable
}
if
member
.
Balance
<
total
{
log
.
Printf
(
"Member %d don't have enough money (%d-%d)"
,
num
,
member
.
Balance
,
total
)
return
http
.
StatusBadRequest
}
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
)
return
http
.
StatusNotAcceptable
}
return
http
.
StatusOK
}
This diff is collapsed.
Click to expand it.
api/purchase_test.go
+
1
−
1
View file @
4618bd72
...
...
@@ -30,7 +30,7 @@ func TestPurchaseAddListMine(t *testing.T) {
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
transactions
),
transactions
)
}
if
transactions
[
0
]
.
Total
!=
testProduct
.
Price
*
products
[
0
]
.
Ammount
{
if
transactions
[
0
]
.
Total
!=
-
testProduct
.
Price
*
products
[
0
]
.
Ammount
{
t
.
Error
(
"Wrong total:"
,
transactions
[
0
]
.
Total
)
}
if
len
(
transactions
[
0
]
.
Purchase
)
!=
1
{
...
...
This diff is collapsed.
Click to expand it.
src/TransactionList.js
+
1
−
1
View file @
4618bd72
...
...
@@ -16,7 +16,7 @@ const columns = [
};
}},
{
dataField
:
'
date
'
,
text
:
'
Fecha
'
,
formatter
:
printDate
},
{
dataField
:
'
total
'
,
text
:
'
Total
'
,
formatter
:
cell
=>
printMoney
(
cell
)
+
"
€
"
},
{
dataField
:
'
total
'
,
text
:
'
Cantidad
'
,
formatter
:
cell
=>
printMoney
(
cell
)
+
"
€
"
},
]
function
TransactionList
()
{
...
...
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