CHANGE: added Cors

This commit is contained in:
2024-09-07 08:54:52 +02:00
parent b582e5d1c2
commit dd2fb9a79f
3 changed files with 47 additions and 21 deletions

View File

@@ -29,23 +29,26 @@ func main() {
} }
func timeCreateHandler(w http.ResponseWriter, r *http.Request) { func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
setCors(w)
switch r.Method { switch r.Method {
case "PUT": case "PUT":
createBooking(w, r) createBooking(w, r)
case "OPTIONS":
w.WriteHeader(http.StatusOK)
default: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} }
} }
func timeHandler(w http.ResponseWriter, r *http.Request) { func timeHandler(w http.ResponseWriter, r *http.Request) {
if os.Getenv("DEBUG") == "true" { setCors(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
}
switch r.Method { switch r.Method {
case "GET": case "GET":
getBookings(w, r) getBookings(w, r)
case "PUT": case "PUT":
updateBooking(w, r) updateBooking(w, r)
case "OPTIONS":
w.WriteHeader(http.StatusOK)
default: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} }
@@ -65,6 +68,7 @@ func createBooking(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(booking) json.NewEncoder(w).Encode(booking)
} }
@@ -98,29 +102,30 @@ func updateBooking(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest) http.Error(w, "Invalid bookingID query parameter", http.StatusBadRequest)
return return
} }
booking, err := (*models.Booking).GetBookingById(nil, booking_id) _booking, err := (*models.Booking).GetBookingById(nil, booking_id)
if err != nil { if err != nil {
log.Println("Error getting booking: ", err) log.Println("Error getting booking: ", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if card_id := r.URL.Query().Get("cardID"); card_id != "" { var booking models.Booking
booking.CardID = card_id dec := json.NewDecoder(r.Body)
} dec.DisallowUnknownFields()
if reader_id := r.URL.Query().Get("readerID"); reader_id != "" { err = dec.Decode(&booking)
booking.ReaderID = reader_id
}
if _booking_type := r.URL.Query().Get("bookingType"); _booking_type != "" {
booking_type, err := strconv.Atoi(_booking_type)
if err != nil { if err != nil {
http.Error(w, "Invalid booking_type query parameter", http.StatusInternalServerError) log.Println("Error parsing booking: ", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
booking.BookingType = booking_type if booking.Id != 0 && booking.Id != _booking.Id {
log.Println("Booking Ids do not match")
http.Error(w, "Booking Ids do not match", http.StatusBadRequest)
return
} }
booking.Save() _booking.Update(booking)
w.WriteHeader(http.StatusOK) _booking.Save()
json.NewEncoder(w).Encode(booking) w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(_booking)
} }
// func getBooking(w http.ResponseWriter, r *http.Request) // func getBooking(w http.ResponseWriter, r *http.Request)
@@ -131,3 +136,11 @@ func getEnv(key, fallback string) string {
} }
return fallback return fallback
} }
func setCors(w http.ResponseWriter) {
if os.Getenv("DEBUG") == "true" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
}
}

View File

@@ -16,9 +16,9 @@ func (e SameBookingError) Error() string {
} }
type Booking struct { type Booking struct {
CardID string `json:"cradID"` CardID string `json:"cardID"`
ReaderID string `json:"readerID"` ReaderID string `json:"readerID"`
BookingType int `json:"bookingTyp"` BookingType int `json:"bookingType"`
LoggedTime time.Time `json:"loggedTime"` LoggedTime time.Time `json:"loggedTime"`
Id int `json:"id"` Id int `json:"id"`
} }
@@ -123,6 +123,18 @@ func (b Booking) Save() {
} }
} }
func (b *Booking) Update(nb Booking) {
if b.BookingType != nb.BookingType && nb.BookingType != 0 {
b.BookingType = nb.BookingType
}
if b.CardID != nb.CardID && nb.CardID != "" {
b.CardID = nb.CardID
}
if b.ReaderID != nb.ReaderID && nb.ReaderID != "" {
b.ReaderID = nb.ReaderID
}
}
func checkLastBooking(b Booking) bool { func checkLastBooking(b Booking) bool {
var booking_type int var booking_type int
stmt, err := DB.Prepare((`SELECT booking_type FROM "zeiten" WHERE "card_id" = $1 ORDER BY "logged_time" DESC LIMIT 1;`)) stmt, err := DB.Prepare((`SELECT booking_type FROM "zeiten" WHERE "card_id" = $1 ORDER BY "logged_time" DESC LIMIT 1;`))

View File

@@ -14,6 +14,7 @@ docker compose up -d
## API ## API
Nutzung der API Nutzung der API
wenn die `dev-docker-compose.yml` Datei gestartet wird, ist direkt ein SwaggerUI Server mit entsprechender Datei inbegriffen.
### Buchungen [/time] ### Buchungen [/time]