Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
becskasszasch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
JetBrains YouTrack
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
KSZK
DevTeam
becskasszasch
Commits
5603c789
Verified
Commit
5603c789
authored
1 year ago
by
Tóth Miklós Tibor
Browse files
Options
Downloads
Patches
Plain Diff
Add pultosch API
parent
47dfb8e9
No related branches found
No related tags found
1 merge request
!25
Add API endpoints and middleware for user and product manipulation
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/api.go
+32
-12
32 additions, 12 deletions
api/api.go
api/model.go
+6
-13
6 additions, 13 deletions
api/model.go
db/db.go
+45
-0
45 additions, 0 deletions
db/db.go
pultosch/pultosch.go
+4
-2
4 additions, 2 deletions
pultosch/pultosch.go
with
87 additions
and
27 deletions
api/api.go
+
32
−
12
View file @
5603c789
...
@@ -2,6 +2,7 @@ package api
...
@@ -2,6 +2,7 @@ package api
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
"git.sch.bme.hu/disappointment-industries/becskasszasch/homepage"
"git.sch.bme.hu/disappointment-industries/becskasszasch/homepage"
...
@@ -144,26 +145,45 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
...
@@ -144,26 +145,45 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
func
BuyInPult
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
BuyInPult
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
switch
r
.
Method
{
switch
r
.
Method
{
case
http
.
MethodPost
:
case
http
.
MethodPost
:
var
boughtInPult
BoughtInPult
dec
:=
json
.
NewDecoder
(
r
.
Body
)
var
pult
PultAPIData
err
:=
dec
.
Decode
(
&
pult
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
err
:=
r
.
ParseForm
(
)
user
,
err
:=
db
.
GetUser
(
pult
.
UserID
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
return
}
}
for
_
,
productInPult
:=
range
boughtInPult
.
ProductsInPult
{
pultosch
,
err
:=
homepage
.
GetUserInfoBySession
(
r
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
var
spends
[]
*
db
.
Spend
for
_
,
purchase
:=
range
pult
.
Purchases
{
if
purchase
.
Amount
==
0
{
continue
}
spend
:=
db
.
Spend
{
spend
:=
db
.
Spend
{
User
:
boughtInPult
.
U
ser
,
User
:
u
ser
,
Product
:
productInPult
.
Product
,
SchAcc
:
user
.
SchAcc
,
Amount
:
productInPult
.
Amount
,
ProdID
:
purchase
.
ProductID
,
Date
:
boughtInPult
.
Date
,
Amount
:
purchase
.
Amount
,
Notes
:
"Pultosch: "
+
boughtInPult
.
Pultosch
.
Name
,
Notes
:
fmt
.
Sprintf
(
"Pultosch: %s (%s)"
,
pultosch
.
User
.
Name
,
pultosch
.
User
.
SchAcc
)
,
}
}
err
:=
spend
.
Save
()
spends
=
append
(
spends
,
&
spend
)
if
err
!=
nil
{
helpers
.
Logger
.
Println
(
err
)
}
}
err
=
db
.
SaveMultipleSpend
(
spends
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
api/model.go
+
6
−
13
View file @
5603c789
package
api
package
api
import
(
type
PultAPIData
struct
{
"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
UserID
string
`json:"userID"`
"time"
Purchases
[]
PultAPIProduct
`json:"purchases"`
)
type
ProductInPult
struct
{
Product
*
db
.
Product
`json:"product"`
Amount
int64
`json:"amount"`
}
}
type
BoughtInPult
struct
{
type
PultAPIProduct
struct
{
User
*
db
.
User
`json:"user"`
ProductID
string
`json:"productID"`
ProductsInPult
[]
*
ProductInPult
`json:"productsInPult"`
Amount
int64
`json:"amount"`
Date
time
.
Time
`json:"date"`
Pultosch
*
db
.
User
`json:"pultosch"`
}
}
This diff is collapsed.
Click to expand it.
db/db.go
+
45
−
0
View file @
5603c789
package
db
package
db
import
(
import
(
"context"
"encoding/json"
"encoding/json"
"fmt"
//"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
//"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10"
_
"github.com/go-pg/pg/v10"
_
"github.com/go-pg/pg/v10"
...
@@ -327,6 +330,48 @@ func (u *User) Load() error {
...
@@ -327,6 +330,48 @@ func (u *User) Load() error {
return
e
return
e
}
}
const
credit
=
5000
func
SaveMultipleSpend
(
sp
[]
*
Spend
)
error
{
e
:=
db
.
RunInTransaction
(
context
.
Background
(),
func
(
tx
*
pg
.
Tx
)
error
{
for
_
,
s
:=
range
sp
{
p
:=
&
Product
{
ID
:
s
.
ProdID
,
}
e
:=
tx
.
Model
(
p
)
.
WherePK
()
.
First
()
if
e
!=
nil
{
return
e
}
u
:=
&
User
{
SchAcc
:
s
.
SchAcc
,
}
e
=
tx
.
Model
(
u
)
.
WherePK
()
.
First
()
price
:=
p
.
Price
*
s
.
Amount
if
price
>
u
.
Money
+
credit
{
return
fmt
.
Errorf
(
"Not enough funds"
)
}
u
.
Money
-=
price
_
,
e
=
tx
.
Model
(
u
)
.
WherePK
()
.
Update
()
if
e
!=
nil
{
return
e
}
spendsNow
.
With
(
prometheus
.
Labels
{
"product"
:
p
.
Name
})
.
Inc
()
_
,
e
=
tx
.
Model
(
s
)
.
Insert
()
if
e
!=
nil
{
return
e
}
}
return
nil
})
return
e
}
func
(
sp
*
Spend
)
Save
()
error
{
func
(
sp
*
Spend
)
Save
()
error
{
p
:=
&
Product
{
p
:=
&
Product
{
ID
:
sp
.
ProdID
,
ID
:
sp
.
ProdID
,
...
...
This diff is collapsed.
Click to expand it.
pultosch/pultosch.go
+
4
−
2
View file @
5603c789
...
@@ -41,13 +41,15 @@ type pultData struct {
...
@@ -41,13 +41,15 @@ type pultData struct {
Users
[]
*
db
.
User
Users
[]
*
db
.
User
}
}
func
toJson
(
obj
any
)
string
{
func
toJson
(
obj
any
)
template
.
JS
{
by
,
e
:=
json
.
Marshal
(
obj
)
by
,
e
:=
json
.
Marshal
(
obj
)
if
e
!=
nil
{
if
e
!=
nil
{
panic
(
e
)
panic
(
e
)
}
}
return
string
(
by
)
str
:=
string
(
by
)
return
template
.
JS
(
str
)
}
}
func
init
()
{
func
init
()
{
...
...
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