CHANGE: finalized user auth + added booking edit view
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/models"
|
||||
"arbeitszeitmessung/templates"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -42,15 +43,21 @@ func parseTimestamp(r *http.Request , get_key string, fallback string) (time.Tim
|
||||
|
||||
// Returns bookings from DB with similar card uid -> checks for card uid in http query params
|
||||
func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
card_uid := r.URL.Query().Get("card_uid")
|
||||
if card_uid == "" && Session.Exists(r.Context(), "card_uid"){
|
||||
card_uid = Session.GetString(r.Context(), "user")
|
||||
}
|
||||
if card_uid == "" {
|
||||
http.Error(w, "Missing card_uid query parameter", http.StatusBadRequest)
|
||||
// if(!Session.Exists(r.Context(), "user")){
|
||||
// log.Println("No user in session storage!")
|
||||
// http.Error(w, "Not logged in!", http.StatusForbidden)
|
||||
// return
|
||||
// }
|
||||
|
||||
// user, err := (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
|
||||
user, err := (*models.User).GetByPersonalNummer(nil, 123)
|
||||
if(err != nil){
|
||||
log.Println("No user found with the given personal number!")
|
||||
http.Error(w, "No user found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
tsFrom, err := parseTimestamp(r, "time_from", "2000-01-01")
|
||||
if(err != nil ){
|
||||
log.Println("Error parsing 'from' time", err)
|
||||
@@ -65,19 +72,45 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
tsTo = tsTo.AddDate(0,0,1) // so that today is inside
|
||||
|
||||
bookings, err := (*models.Booking).GetBookingsGrouped(nil, card_uid, tsFrom, tsTo)
|
||||
bookings, err := (*models.Booking).GetBookingsGrouped(nil, user.CardUID, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
log.Println("Error getting bookings: ", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
templates.OverviewPage(bookings).Render(r.Context(), w)
|
||||
ctx := context.WithValue(r.Context(), "user", user)
|
||||
templates.TimeDashboard(bookings).Render(ctx, w)
|
||||
// w.Header().Set("Content-Type", "application/json")
|
||||
// json.NewEncoder(w).Encode(bookings)
|
||||
}
|
||||
|
||||
func updateBooking(w http.ResponseWriter, r *http.Request){
|
||||
r.ParseForm()
|
||||
for index, possibleBooking := range r.PostForm{
|
||||
if(index[:7] == "booking"){
|
||||
booking_id, err := strconv.Atoi(index[8:])
|
||||
if(err != nil){
|
||||
log.Println("Error parsing bookingId", err)
|
||||
continue
|
||||
}
|
||||
booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
||||
if(err != nil){
|
||||
log.Println("Error getting booking!", err)
|
||||
continue
|
||||
}
|
||||
parsedTime, err := time.Parse("15:04", possibleBooking[0])
|
||||
if(err != nil){
|
||||
log.Println("Error parsing time!", err)
|
||||
continue
|
||||
}
|
||||
booking.UpdateTime(parsedTime)
|
||||
}
|
||||
}
|
||||
getBookings(w, r)
|
||||
}
|
||||
|
||||
// Updates a booking form the given json body
|
||||
func updateBooking(w http.ResponseWriter, r *http.Request) {
|
||||
func updateBookingAPI(w http.ResponseWriter, r *http.Request) {
|
||||
_booking_id := r.URL.Query().Get("counter_id")
|
||||
if _booking_id == "" {
|
||||
http.Error(w, "Missing bookingID query parameter", http.StatusBadRequest)
|
||||
|
||||
@@ -45,7 +45,7 @@ func loginUser(w http.ResponseWriter, r *http.Request){
|
||||
log.Println("No card_uid provided!")
|
||||
http.Error(w, "No card_uid provided", http.StatusBadRequest)
|
||||
}
|
||||
user, err := (*models.User).GetById(nil, card_uid)
|
||||
user, err := (*models.User).GetByCardUID(nil, card_uid)
|
||||
if(err != nil){
|
||||
log.Println("No user found under this card_uid!")
|
||||
http.Error(w, "No user found!", http.StatusNotFound)
|
||||
@@ -54,7 +54,7 @@ func loginUser(w http.ResponseWriter, r *http.Request){
|
||||
password := r.FormValue("password")
|
||||
if(user.Login(password)){
|
||||
log.Printf("New succesfull user login from %s %s!\n", user.Vorname, user.Name)
|
||||
Session.Put(r.Context(), "user", user.CardUID)
|
||||
Session.Put(r.Context(), "user", user.PersonalNummer)
|
||||
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user