Verified Commit ac4bca62 authored by blint's avatar blint 😶
Browse files

update admin page, create balance queryer

parent cd877c78
Pipeline #8652 passed with stage
in 1 minute and 13 seconds
package admin
import (
"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
"git.sch.bme.hu/disappointment-industries/becskasszasch/homepage"
"html/template"
"net/http"
)
var adminTemplate *template.Template
var balanceTemplate *template.Template
var Router = http.NewServeMux()
type Between struct {
User *homepage.User
Counts map[string]*int64
Products map[string]*db.Product
From string
To string
}
func init() {
var err error
adminTemplate, err = template.New("admin.template.html").ParseFiles("admin/admin.template.html", "homepage/navbar.template.html", "homepage/header.template.html", "homepage/footer.template.html")
adminTemplate, err = template.New("admin.template.html").ParseFiles("admin/admin.template.html", "admin/between.template.html", "homepage/navbar.template.html", "homepage/header.template.html", "homepage/footer.template.html")
if err != nil {
panic(err)
}
balanceTemplate, err = template.New("balance.template.html").Funcs(template.FuncMap{
"mul": func(a, b int64) int64 {
return a * b
},
}).ParseFiles("admin/balance.template.html", "homepage/navbar.template.html", "homepage/header.template.html", "homepage/footer.template.html")
if err != nil {
panic(err)
}
Router.HandleFunc("/balance/", balance)
Router.HandleFunc("/", handler)
}
func Handler(w http.ResponseWriter, r *http.Request) {
er := adminTemplate.Execute(w, nil)
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
admin, err := homepage.GetUserInfoBySession(r)
if err != nil {
return
}
er := adminTemplate.Execute(w, admin)
if er != nil {
helpers.Logger.Println(er)
}
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func balance(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if e := r.ParseForm(); e != nil {
helpers.Logger.Println("Something fucked here...")
return
}
post := r.PostForm
usr, err := homepage.GetUserInfoBySession(r)
if err != nil {
return
}
from := post["balanceFromDate"][0]
to := post["balanceToDate"][0]
counts, products, err := db.GetSpendsBetween(from, to)
if err != nil {
return
}
betw := &Between{
User: usr,
Counts: counts,
Products: products,
From: from,
To: to,
}
er := balanceTemplate.Execute(w, betw)
if er != nil {
helpers.Logger.Println(er)
}
......
......@@ -2,10 +2,10 @@
{{ template "navbar" . }}
{{ if .User.IsAdmin }}
{{- if .IsAdmin }}
yolo
{{ template "between" . }}
{{ end }}
{{- end }}
{{ template "footer" . }}
\ No newline at end of file
{{ template "header" .User}}
{{ template "navbar" .User }}
{{- if .User.IsAdmin }}
<h2>{{ .From}}-tól {{ .To }}-ig</h2>
<table>
<tr>
<th>Termék neve</th>
<th>Egységár</th>
<th>Darabszám</th>
<th>Összes bevétel</th>
</tr>
{{ $prod := .Products }}
{{ range $k, $v := .Counts }}
{{ $p := index $prod $k }}
<tr>
<td>{{ $p.Name }}</td>
<td>{{ $p.Price }} JMF</td>
<td>{{$v}} db</td>
<td>{{mul $v $p.Price}} JMF</td>
</tr>
{{ end }}
</table>
{{- end }}
{{ template "footer" . }}
\ No newline at end of file
{{ define "between" }}
<div class="mx-auto text-center form">
<form action="/admin/balance/" method="post" id="balance" class="justify-content-center">
<div class="form-group">
<label for="balanceFromDate">Mikortól:</label><br>
<input type="date" id="balanceFromDate" name="balanceFromDate">
</div>
<div class="form-group">
<label for="balanceToDate">Meddig:</label><br>
<input type="date" id="balanceToDate" name="balanceToDate">
</div>
<div class="form-group">
<button type="submit" class="btn bg-kszk text-white">Go</button>
</div>
</form>
</div>
<script>
document.getElementById("balanceFromDate").valueAsDate = new Date()
document.getElementById("balanceToDate").valueAsDate = new Date()
</script>
{{ end }}
\ No newline at end of file
......@@ -136,6 +136,7 @@ func init() {
spendsNow.With(prometheus.Labels{"product": p.Name}).Add(a)
}
//GetSpendsBetween(time.Date(2021,7,19, 0, 0, 0, 0, time.Local), time.Date(2021,8,18, 0, 0, 0, 0, time.Local))
}
func StoreUserAndGiveCookie(SchAcc, Name string) (string, error) {
......@@ -226,6 +227,42 @@ func GetProducts() ([]*Product, error) {
return products, e
}
/*
select product_id,sum(amount*price)
from spends
join products p on p.id = spends.product_id
where date between '2021-07-19' and '2021-08-18'
group by product_id;
*/
func GetSpendsBetween(from, to string) (map[string]*int64, map[string]*Product, error) {
var res []*Spend
e := db.Model(&res).Relation("Product").
Where("Date between ? and ?", from, to).Select()
if e != nil {
return nil, nil, e
}
m := make(map[string]*int64)
products := make(map[string]*Product)
for _, sp := range res {
val, ok := m[sp.Product.ID]
if !ok {
val = new(int64)
m[sp.Product.ID] = val
}
*val += sp.Amount
if _, ok := products[sp.Product.ID]; !ok {
products[sp.Product.ID] = sp.Product
}
}
return m, products, nil
}
func GetSpends(typ string) ([]*Spend, error) {
var spends []*Spend
......
......@@ -10,7 +10,7 @@ import (
var home_template *template.Template
type homePage struct {
type User struct {
User *db.User
Dark bool
LoginURL string
......@@ -48,7 +48,7 @@ func ThemeHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}
func Handler(w http.ResponseWriter, r *http.Request) {
func GetUserInfoBySession(r *http.Request) (*User, error) {
s := auth.GetFullSession(r)
if s == nil {
s = &db.Session{}
......@@ -62,16 +62,25 @@ func Handler(w http.ResponseWriter, r *http.Request) {
ps, e := db.GetProducts()
if e != nil {
//TODO: error handling
return
return nil, e
}
h := &homePage{
user := &User{
User: s.User,
Dark: s.DarkTheme,
LoginURL: auth.GetLoginURL(),
Products: ps,
IsAdmin: admin,
}
err := home_template.Execute(w, h)
return user, nil
}
func Handler(w http.ResponseWriter, r *http.Request) {
user, err := GetUserInfoBySession(r)
if err != nil {
return
}
err = home_template.Execute(w, user)
if err != nil {
helpers.Logger.Println(err)
}
......
......@@ -38,7 +38,7 @@ func main() {
//mux.HandleFunc("/topup/api/", topup.PayPalHandler)
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/metrics/", promhttp.Handler())
mux.HandleFunc("/admin/", admin.Handler)
mux.Handle("/admin/", http.StripPrefix("/admin", admin.Router))
mux.HandleFunc("/history/", history.Handler)
mux.HandleFunc("/", homepage.Handler)
//mux.HandleFunc("/new/", homepage.NewHandler)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment