Skip to content
Snippets Groups Projects
Select Git revision
  • 06d4da2dea77ad1ef23335f23aa5cc5c4a9c695b
  • master default protected
  • reinstall-2023
3 results

ansible.cfg

Blame
  • admin.go 2.55 KiB
    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()
    var Handler = AdminMW(router)
    
    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", "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 AdminMW(h http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		admin, err := homepage.GetUserInfoBySession(r)
    		if err != nil {
    			helpers.Logger.Println(err)
    			return
    		}
    		if admin.IsAdmin {
    			h.ServeHTTP(w, r)
    		} else {
    			w.WriteHeader(http.StatusForbidden)
    		}
    	})
    }
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	if r.Method == http.MethodGet {
    		admin, err := homepage.GetUserInfoBySession(r)
    		if err != nil {
    			helpers.Logger.Println(err)
    			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)
    	}
    }