CHANGE: changed API endpoints
This commit is contained in:
@@ -2,39 +2,47 @@ package main
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var DB *sql.DB
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
DB, err = OpenDatabase()
|
||||
models.DB, err = OpenDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer DB.Close()
|
||||
defer models.DB.Close()
|
||||
|
||||
http.HandleFunc("/time/new", timeCreateHandler)
|
||||
http.HandleFunc("/time", timeHandler)
|
||||
|
||||
fmt.Println("Server is running at http://localhost:8080")
|
||||
fmt.Printf("Server is running at http://localhost:8080 exposed to port %s\n", getEnv("EXPOSED_PORT", "8000"))
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func timeCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "PUT":
|
||||
createBooking(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func timeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "PUT":
|
||||
createBooking(w, r)
|
||||
case "GET":
|
||||
getBookings(w, r)
|
||||
case "PUT":
|
||||
updateBooking(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -43,7 +51,7 @@ func timeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func createBooking(w http.ResponseWriter, r *http.Request) {
|
||||
booking := (*models.Booking).FromUrlParams(nil, r.URL.Query())
|
||||
if booking.Verify() {
|
||||
err := booking.Insert(DB)
|
||||
err := booking.Insert()
|
||||
if errors.Is(models.SameBookingError{}, err) {
|
||||
http.Error(w, "Booking already exists", http.StatusConflict)
|
||||
return
|
||||
@@ -61,7 +69,7 @@ func createBooking(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
card_id := r.URL.Query().Get("cardID")
|
||||
bookings, err := GetBookingsByCardID(DB, card_id)
|
||||
bookings, err := (*models.Booking).GetBookingsByCardID(nil, card_id)
|
||||
if err != nil {
|
||||
log.Println("Error getting bookings: ", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
@@ -69,7 +77,42 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(bookings)
|
||||
}
|
||||
|
||||
func updateBooking(w http.ResponseWriter, r *http.Request) {
|
||||
_booking_id := r.URL.Query().Get("bookingID")
|
||||
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
|
||||
}
|
||||
booking, 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
|
||||
}
|
||||
if card_id := r.URL.Query().Get("cardID"); card_id != "" {
|
||||
booking.CardID = card_id
|
||||
}
|
||||
if reader_id := r.URL.Query().Get("readerID"); reader_id != "" {
|
||||
booking.ReaderID = reader_id
|
||||
}
|
||||
if _booking_type := r.URL.Query().Get("bookingType"); _booking_type != "" {
|
||||
booking_type, err := strconv.Atoi(_booking_type)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid booking_type query parameter", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
booking.BookingType = booking_type
|
||||
}
|
||||
booking.Save()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(booking)
|
||||
}
|
||||
|
||||
// func getBooking(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
Reference in New Issue
Block a user