Skip to content
Snippets Groups Projects
Verified Commit 5603c789 authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

Add pultosch API

parent 47dfb8e9
No related branches found
No related tags found
1 merge request!25Add API endpoints and middleware for user and product manipulation
...@@ -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.User, User: user,
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
} }
} }
} }
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"`
} }
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,
......
...@@ -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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment