CHANGE: refractor + refined user routes, added change pw form and function
This commit is contained in:
@@ -28,27 +28,26 @@ func TimeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func parseTimestamp(r *http.Request , get_key string, fallback string) (time.Time, error) {
|
||||
func parseTimestamp(r *http.Request, get_key string, fallback string) (time.Time, error) {
|
||||
_timestamp_get := r.URL.Query().Get(get_key)
|
||||
if(_timestamp_get == "") {
|
||||
if _timestamp_get == "" {
|
||||
_timestamp_get = fallback
|
||||
}
|
||||
timestamp_get, err := time.Parse("2006-01-02", _timestamp_get)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
return time.Now(), err
|
||||
}
|
||||
return timestamp_get, nil
|
||||
}
|
||||
|
||||
|
||||
// Returns bookings from DB with similar card uid -> checks for card uid in http query params
|
||||
func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
var user models.User
|
||||
var err error
|
||||
if(helper.GetEnv("GO_ENV", "production") == "debug"){
|
||||
if helper.GetEnv("GO_ENV", "production") == "debug" {
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, 123)
|
||||
}else{
|
||||
if(!Session.Exists(r.Context(), "user")){
|
||||
} else {
|
||||
if !Session.Exists(r.Context(), "user") {
|
||||
log.Println("No user in session storage!")
|
||||
http.Error(w, "Not logged in!", http.StatusForbidden)
|
||||
return
|
||||
@@ -56,7 +55,7 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
|
||||
}
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("No user found with the given personal number!")
|
||||
http.Error(w, "No user found", http.StatusNotFound)
|
||||
return
|
||||
@@ -64,18 +63,18 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// TODO add config for timeoffset
|
||||
tsFrom, err := parseTimestamp(r, "time_from", time.Now().AddDate(0, -1, 0).Format("2006-01-02"))
|
||||
if(err != nil ){
|
||||
if err != nil {
|
||||
log.Println("Error parsing 'from' time", err)
|
||||
http.Error(w, "Timestamp 'from' cannot be parsed!", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
tsTo, err := parseTimestamp(r, "time_to", time.Now().Format("2006-01-02"))
|
||||
if(err != nil ){
|
||||
if err != nil {
|
||||
log.Println("Error parsing 'to' time", err)
|
||||
http.Error(w, "Timestamp 'to' cannot be parsed!", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
tsTo = tsTo.AddDate(0,0,1) // so that today is inside
|
||||
tsTo = tsTo.AddDate(0, 0, 1) // so that today is inside
|
||||
|
||||
bookings, err := (*models.Booking).GetBookingsGrouped(nil, user.CardUID, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
@@ -87,22 +86,22 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
templates.TimeDashboard(bookings).Render(ctx, w)
|
||||
}
|
||||
|
||||
func updateBooking(w http.ResponseWriter, r *http.Request){
|
||||
func updateBooking(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
for index, possibleBooking := range r.PostForm{
|
||||
if(index[:7] == "booking"){
|
||||
for index, possibleBooking := range r.PostForm {
|
||||
if index[:7] == "booking" {
|
||||
booking_id, err := strconv.Atoi(index[8:])
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("Error parsing bookingId", err)
|
||||
continue
|
||||
}
|
||||
booking, err := (*models.Booking).GetBookingById(nil, booking_id)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("Error getting booking!", err)
|
||||
continue
|
||||
}
|
||||
parsedTime, err := time.ParseInLocation("15:04", possibleBooking[0], time.Local)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("Error parsing time!", err)
|
||||
continue
|
||||
}
|
||||
@@ -113,17 +112,17 @@ func updateBooking(w http.ResponseWriter, r *http.Request){
|
||||
getBookings(w, r)
|
||||
}
|
||||
|
||||
func getBookingsAPI(w http.ResponseWriter, r *http.Request){
|
||||
func getBookingsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
_user_pn := r.URL.Query().Get("personal_nummer")
|
||||
user_pn, err := strconv.Atoi(_user_pn)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("No personal numver found!")
|
||||
http.Error(w, "No personal number found", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := (*models.User).GetByPersonalNummer(nil, user_pn)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
log.Println("No user found with the given personal number!")
|
||||
http.Error(w, "No user found", http.StatusNotFound)
|
||||
return
|
||||
@@ -131,18 +130,18 @@ func getBookingsAPI(w http.ResponseWriter, r *http.Request){
|
||||
|
||||
// TODO add config for timeoffset
|
||||
tsFrom, err := parseTimestamp(r, "time_from", time.Now().AddDate(0, -1, 0).Format("2006-01-02"))
|
||||
if(err != nil ){
|
||||
if err != nil {
|
||||
log.Println("Error parsing 'from' time", err)
|
||||
http.Error(w, "Timestamp 'from' cannot be parsed!", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
tsTo, err := parseTimestamp(r, "time_to", time.Now().Format("2006-01-02"))
|
||||
if(err != nil ){
|
||||
if err != nil {
|
||||
log.Println("Error parsing 'to' time", err)
|
||||
http.Error(w, "Timestamp 'to' cannot be parsed!", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
tsTo = tsTo.AddDate(0,0,1) // so that today is inside
|
||||
tsTo = tsTo.AddDate(0, 0, 1) // so that today is inside
|
||||
|
||||
bookings, err := (*models.Booking).GetBookingsGrouped(nil, user.CardUID, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user