116 lines
3.4 KiB
Go
116 lines
3.4 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"arbeitszeitmessung/helper"
|
|
"arbeitszeitmessung/models"
|
|
"arbeitszeitmessung/templates"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// Frontend relevant backend functionality -> not used by the arduino devices
|
|
func TimeHandler(w http.ResponseWriter, r *http.Request) {
|
|
helper.SetCors(w)
|
|
switch r.Method {
|
|
case "GET":
|
|
getBookings(w, r)
|
|
case "POST":
|
|
updateBooking(w, r)
|
|
case "OPTIONS":
|
|
// just support options header for non GET Requests from SWAGGER
|
|
w.WriteHeader(http.StatusOK)
|
|
default:
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func parseTimestamp(r *http.Request , get_key string, fallback string) (time.Time, error) {
|
|
_timestamp_get := r.URL.Query().Get(get_key)
|
|
if(_timestamp_get == "") {
|
|
_timestamp_get = fallback
|
|
}
|
|
timestamp_get, err := time.Parse("2006-01-02", _timestamp_get)
|
|
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) {
|
|
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)
|
|
return
|
|
}
|
|
|
|
tsFrom, err := parseTimestamp(r, "time_from", "2000-01-01")
|
|
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 ){
|
|
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
|
|
|
|
bookings, err := (*models.Booking).GetBookingsGrouped(nil, card_uid, 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)
|
|
// w.Header().Set("Content-Type", "application/json")
|
|
// json.NewEncoder(w).Encode(bookings)
|
|
}
|
|
|
|
// Updates a booking form the given json body
|
|
func updateBooking(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)
|
|
return
|
|
}
|
|
booking_id, err := strconv.Atoi(_booking_id)
|
|
if err != nil {
|
|
http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
bookingDB, err := (*models.Booking).GetBookingById(nil, booking_id)
|
|
if err != nil {
|
|
log.Println("Error getting booking: ", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var booking models.Booking
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
err = dec.Decode(&booking)
|
|
if err != nil {
|
|
log.Println("Error parsing booking: ", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if booking.CounterId != 0 && booking.CounterId != bookingDB.CounterId {
|
|
log.Println("Booking Ids do not match")
|
|
http.Error(w, "Booking Ids do not match", http.StatusBadRequest)
|
|
return
|
|
}
|
|
bookingDB.Update(booking)
|
|
bookingDB.Save()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(bookingDB)
|
|
}
|