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
cb2bff18
Commit
cb2bff18
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Add purchases from admin for other members
parent
be1bae9f
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/api.go
+2
-1
2 additions, 1 deletion
api/api.go
api/purchase.go
+8
-0
8 additions, 0 deletions
api/purchase.go
api/purchase_test.go
+58
-0
58 additions, 0 deletions
api/purchase_test.go
with
68 additions
and
1 deletion
api/api.go
+
2
−
1
View file @
cb2bff18
...
...
@@ -39,7 +39,8 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error {
r
.
HandleFunc
(
"/member/{num:[0-9]+}"
,
a
.
authAdmin
(
a
.
GetMember
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/member/{num:[0-9]+}"
,
a
.
authAdmin
(
a
.
UpdateMember
))
.
Methods
(
"PUT"
)
r
.
HandleFunc
(
"/member/{num:[0-9]+}"
,
a
.
authAdmin
(
a
.
DeleteMember
))
.
Methods
(
"DELETE"
)
r
.
HandleFunc
(
"/member/{num:[0-9]+}/purchase"
,
a
.
authAdmin
(
a
.
GetMemberTransactions
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/member/{num:[0-9]+}/transactions"
,
a
.
authAdmin
(
a
.
GetMemberTransactions
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/member/{num:[0-9]+}/purchase"
,
a
.
authAdmin
(
a
.
AddMemberPurchase
))
.
Methods
(
"POST"
)
r
.
HandleFunc
(
"/product"
,
a
.
auth
(
a
.
ListProducts
))
.
Methods
(
"GET"
)
r
.
HandleFunc
(
"/product"
,
a
.
authAdmin
(
a
.
AddProduct
))
.
Methods
(
"POST"
)
...
...
This diff is collapsed.
Click to expand it.
api/purchase.go
+
8
−
0
View file @
cb2bff18
...
...
@@ -5,8 +5,10 @@ import (
"errors"
"log"
"net/http"
"strconv"
"0xacab.org/meskio/cicer/api/db"
"github.com/gorilla/mux"
)
func
(
a
*
api
)
AddPurchase
(
num
int
,
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
...
...
@@ -40,3 +42,9 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) {
}
}
func
(
a
*
api
)
AddMemberPurchase
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
req
)
num
,
_
:=
strconv
.
Atoi
(
vars
[
"num"
])
a
.
AddPurchase
(
num
,
w
,
req
)
}
This diff is collapsed.
Click to expand it.
api/purchase_test.go
+
58
−
0
View file @
cb2bff18
package
api
import
(
"fmt"
"net/http"
"testing"
...
...
@@ -63,3 +64,60 @@ func TestPurchaseAddListMine(t *testing.T) {
t
.
Error
(
"Wrong product balance:"
,
member
.
Balance
)
}
}
func
TestMemberPurchase
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
tapi
.
addTestMember
()
tapi
.
addTestProducts
()
products
:=
[]
db
.
Purchase
{
{
ProductCode
:
testProduct
.
Code
,
Amount
:
5
,
},
}
resp
:=
tapi
.
doAdmin
(
"POST"
,
fmt
.
Sprintf
(
"/member/%d/purchase"
,
testMember
.
Num
),
products
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
t
.
Fatal
(
"Can't create purchase:"
,
resp
.
Status
)
}
var
transactions
[]
db
.
Transaction
resp
=
tapi
.
do
(
"GET"
,
"/transaction/mine"
,
nil
,
&
transactions
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get transactions:"
,
resp
.
Status
)
}
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
transactions
),
transactions
)
}
if
transactions
[
0
]
.
Total
!=
-
testProduct
.
Price
*
products
[
0
]
.
Amount
{
t
.
Error
(
"Wrong total:"
,
transactions
[
0
]
.
Total
)
}
if
len
(
transactions
[
0
]
.
Purchase
)
!=
1
{
t
.
Fatal
(
"Wrong number of products"
,
len
(
transactions
[
0
]
.
Purchase
),
transactions
[
0
]
.
Purchase
)
}
if
transactions
[
0
]
.
Purchase
[
0
]
.
ProductCode
!=
testProduct
.
Code
{
t
.
Error
(
"Wrong product code:"
,
transactions
[
0
]
.
Purchase
[
0
]
.
ProductCode
)
}
if
transactions
[
0
]
.
Purchase
[
0
]
.
Price
!=
testProduct
.
Price
{
t
.
Error
(
"Wrong product price:"
,
transactions
[
0
]
.
Purchase
[
0
]
.
Price
)
}
var
product
db
.
Product
resp
=
tapi
.
do
(
"GET"
,
"/product/234"
,
nil
,
&
product
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Error
(
"Can't find the product:"
,
resp
.
Status
)
}
if
product
.
Stock
!=
testProduct
.
Stock
-
products
[
0
]
.
Amount
{
t
.
Error
(
"Wrong product stock:"
,
product
)
}
var
member
db
.
Member
resp
=
tapi
.
do
(
"GET"
,
"/member/me"
,
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
]
.
Amount
)
{
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