From 04359ea161aefdde821d55a900482751d53e5d1a Mon Sep 17 00:00:00 2001 From: s441433 Date: Fri, 4 Jan 2019 17:58:41 +0100 Subject: [PATCH] Update main.go --- main.go | 151 +++++++++++++++----------------------------------------- 1 file changed, 41 insertions(+), 110 deletions(-) diff --git a/main.go b/main.go index afaa193..4bf8ada 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,7 @@ import ( ) var ( - //localhost------------- - ID int - localHostDB string - localHost string + KosztBiletuSUMA int //logowanie ------------ loginFROMsite string @@ -56,8 +53,8 @@ var ( //############################################################################# func opisStołówkaZPM(w http.ResponseWriter, r *http.Request) { //fmt.Println(localHostDB) - t, _ := template.ParseFiles("StronaGlowna.gtpl") - t.Execute(w, localHost) + t, _ := template.ParseFiles("StronaGlowna.gohtml") + t.Execute(w, nil) } //############################################################################# @@ -99,11 +96,9 @@ func panel(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) t, _ := template.ParseFiles("panel.gtpl") d := struct { - UserView string - localHostView string + UserView string }{ - UserView: User, - localHostView: localHost} + UserView: User} t.ExecuteTemplate(w, "panel.gtpl", d) } @@ -113,7 +108,6 @@ func wylogowano(w http.ResponseWriter, r *http.Request) { //-------------------------------------------------- t, _ := template.ParseFiles("wylogowano.gtpl") d := struct { - localHostView string loginFROMsiteView string passwordFROMsiteView string IDusersView int @@ -132,7 +126,6 @@ func wylogowano(w http.ResponseWriter, r *http.Request) { NazwaDaniaView string KosztDaniaView int }{ - localHostView: localHost, loginFROMsiteView: "", passwordFROMsiteView: "", IDusersView: 0, @@ -156,21 +149,12 @@ func wylogowano(w http.ResponseWriter, r *http.Request) { //############################################################################# func login(w http.ResponseWriter, r *http.Request) { - //fmt.Println(localHost) - //cookies start=================================================================== - //type Cookie struct { - // Login string - // Password string - //} - //cookies end=================================================================== - //************************************************************************** loginFROMsite := r.FormValue("loginFROMsite") passwordFROMsite := r.FormValue("passwordFROMsite") db, err := sql.Open("sqlite3", "stolowkaZPM.db") checkErr(err) - // query rows0, err := db.Query("SELECT * FROM users") checkErr(err) @@ -182,64 +166,35 @@ func login(w http.ResponseWriter, r *http.Request) { //fmt.Fprint(w, ". ") //fmt.Fprint(w, User) //fmt.Fprint(w, " \n") - //if r.Method == "POST" { - if Login == loginFROMsite { - if Password == passwordFROMsite { - //fmt.Fprintln(w, zaloguj) - //fmt.Fprintln(w, usernameFROMsite) - //time.Sleep(3 * time.Second) - //if zaloguj == "Zalogowano!" - http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther) - panel(w, r) - break + if r.Method == "POST" { + if Login == loginFROMsite { + if Password == passwordFROMsite { + http.Redirect(w, r, "http://localhost:9197/panel", http.StatusSeeOther) + panel(w, r) + break + } } } } - //fmt.Println("method:", r.Method) - //r.Method = "GET" if r.Method == "GET" { t, _ := template.ParseFiles("login.gtpl") - d := struct { - localHost string - }{ - localHost: localHost} - t.ExecuteTemplate(w, "login.gtpl", d) + t.Execute(w, nil) r.ParseForm() } - //} else { - //fmt.Println("usernameFROMsite: ", r.Form["usernameFROMsite"]) - //fmt.Println("passwordFROMsite: ", r.Form["passwordFROMsite"]) - //if len(r.Form["usernameFROMsite"][0]) == 0 { - // goto Here - //} - //if len(r.Form["passwordFROMsite"][0]) == 0 { - // goto Here - //} - - //obsługa blędnego wpisania loginu lub hasła - if r.Method == "POST" { if loginFROMsite != Login { if loginFROMsite == "" { if passwordFROMsite != Password { t, _ := template.ParseFiles("login.gtpl") - d := struct { - localHostView string - }{ - localHostView: localHost} - t.ExecuteTemplate(w, "login.gtpl", d) + t.Execute(w, nil) fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***") } } else { if passwordFROMsite != Password { t, _ := template.ParseFiles("login.gtpl") - d := struct { - localHostView string - }{ - localHostView: localHost} - t.ExecuteTemplate(w, "login.gtpl", d) + t.Execute(w, nil) fmt.Fprint(w, "*** UWAGA! *** Błąd logowania! Login lub hasło nieprawidłowe. *** Spróbuj ponownie. ***") } } @@ -255,11 +210,9 @@ func login(w http.ResponseWriter, r *http.Request) { func menuDan(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("menuDanNaglowek.gtpl") d := struct { - localHostView string - UserView string + UserView string }{ - localHostView: localHost, - UserView: User} + UserView: User} t.ExecuteTemplate(w, "menuDanNaglowek.gtpl", d) //dania ---------------------------------- db, err := sql.Open("sqlite3", "stolowkaZPM.db") @@ -272,12 +225,10 @@ func menuDan(w http.ResponseWriter, r *http.Request) { //fmt.Printf("%d. %s, %d \n", IDdania, NazwaDania, KosztDania) t, _ := template.ParseFiles("menuDan.gtpl") d := struct { - localHostView string IDdaniaView int NazwaDaniaView string KosztDaniaView int }{ - localHostView: localHost, IDdaniaView: IDdania, NazwaDaniaView: NazwaDania, KosztDaniaView: KosztDania} @@ -292,11 +243,9 @@ func menuDan(w http.ResponseWriter, r *http.Request) { func historiaPosilkow(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("historiaPosilkowNaglowek.gtpl") d := struct { - localHostView string - UserView string + UserView string }{ - localHostView: localHost, - UserView: User} + UserView: User} t.ExecuteTemplate(w, "historiaPosilkowNaglowek.gtpl", d) //bilety ---------------------------------- @@ -310,14 +259,12 @@ func historiaPosilkow(w http.ResponseWriter, r *http.Request) { if KtoZabral == User { t, _ := template.ParseFiles("historiaPosilkow.gtpl") d := struct { - localHostView string IDbiletuView int KtoZabralView string DataCzasView string KosztBiletuView int CzyZaplaconyView bool }{ - localHostView: localHost, IDbiletuView: IDbiletu, KtoZabralView: KtoZabral, DataCzasView: DataCzas, @@ -338,33 +285,29 @@ func platnosci(w http.ResponseWriter, r *http.Request) { //bilety ---------------------------------- db, err := sql.Open("sqlite3", "stolowkaZPM.db") checkErr(err) - rows5, err := db.Query("SELECT SUM(KosztBiletu)FROM bilety WHERE KtoZabral=?", User) - if err != nil { - fmt.Println(err) - // os.Exit(1) - } - /* - //for rows5.Next() { - //err = rows5.Scan(&KtoZabral, &KosztBiletu) - //checkErr3(err) - if KtoZabral == User { - t, _ := template.ParseFiles("platnosci.gtpl") - d := struct { - UserView string - KosztBiletuViewSuma int - }{ - UserView: User, - KosztBiletuViewSuma: } - t.ExecuteTemplate(w, "platnosci.gtpl", d) - //break - } - //---------------------------------- - //} - */ + rows5, err := db.Query("SELECT * FROM bilety WHERE KtoZabral=?", User) + checkErr(err) + KosztBiletuSUMA = 0 for rows5.Next() { - rows5.Scan(&KtoZabral, &KosztBiletu) - fmt.Printf("%v %v\n", KtoZabral, KosztBiletu) + err = rows5.Scan(&IDbiletu, &KtoZabral, &DataCzas, &KosztBiletu, &CzyZaplacony) + checkErr3(err) + //if KtoZabral == User { + KosztBiletuSUMA = KosztBiletu + KosztBiletuSUMA + //} + //---------------------------------- } + fmt.Println(KosztBiletuSUMA) + t, _ := template.ParseFiles("platnosci.gtpl") + checkErr(err) + type zaplac struct { + UserView string + Zaplac int + } + z := zaplac{ + UserView: User, + Zaplac: KosztBiletuSUMA} + + t.ExecuteTemplate(w, "platnosci.gtpl", z) rows5.Close() db.Close() } @@ -393,18 +336,6 @@ func checkErr3(err error) { //funkcja główna //*************************************************************************** func main() { - db, err := sql.Open("sqlite3", "stolowkaZPM.db") - checkErr(err) - rowsHost, err := db.Query("SELECT * FROM localhost") - checkErr2(err) - for rowsHost.Next() { - err = rowsHost.Scan(&ID, &localHostDB) - checkErr3(err) - } - localHost = localHostDB - rowsHost.Close() - db.Close() - http.HandleFunc("/", opisStołówkaZPM) http.HandleFunc("/login", login) http.HandleFunc("/panel", panel) @@ -413,7 +344,7 @@ func main() { http.HandleFunc("/wylogowano", wylogowano) http.HandleFunc("/menuDan", menuDan) - err = http.ListenAndServe(":9197", nil) + err := http.ListenAndServe(":9197", nil) if err != nil { log.Fatal("ListenAndServe: ", err) }