CHANGE: added Frontend + auth

This commit is contained in:
2025-02-19 21:16:58 +01:00
parent f2e9eaf092
commit 35778e58b3
25 changed files with 2160 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/url"
"sort"
"strconv"
"time"
)
@@ -84,14 +85,17 @@ func (b *Booking) GetBookingById(booking_id int) (Booking, error) {
return booking, nil
}
func (b *Booking) GetBookingsByCardID(card_id string) ([]Booking, error) {
qStr, err := DB.Prepare((`SELECT counter_id, timestamp, card_uid, geraet_id, check_in_out FROM anwesenheit WHERE card_uid = $1`))
// Gets all booking based on a card uid
//
// optional filter parameter
func (b *Booking) GetBookingsByCardID(card_uid string, tsFrom time.Time, tsTo time.Time) ([]Booking, error) {
qStr, err := DB.Prepare((`SELECT counter_id, timestamp, card_uid, geraet_id, check_in_out FROM anwesenheit WHERE card_uid = $1 AND timestamp BETWEEN $2 AND $3 ORDER BY timestamp`))
if err != nil {
return nil, err
}
defer qStr.Close()
var bookings []Booking
rows, err := qStr.Query(card_id)
rows, err := qStr.Query(card_uid, tsFrom, tsTo)
if err == sql.ErrNoRows {
return bookings, err
}
@@ -112,19 +116,58 @@ func (b *Booking) GetBookingsByCardID(card_id string) ([]Booking, error) {
return bookings, nil
}
func (b *Booking) GetBookingsGrouped(card_uid string, tsFrom time.Time, tsTo time.Time) ([]WorkDay, error){
var grouped = make(map[string][]Booking)
bookings, err := b.GetBookingsByCardID(card_uid, tsFrom, tsTo)
if (err != nil){
log.Println("Failed to get bookings",err)
return []WorkDay{}, nil
}
for _, booking := range bookings {
day := booking.Timestamp.Truncate(24 * time.Hour)
key := day.Format("2006-01-02")
grouped[key] = append(grouped[key], booking)
}
var result []WorkDay
for key, bookings := range grouped {
day, _ := time.Parse("2006-01-02", key)
result = append(result, WorkDay{Day: day, Bookings: bookings})
}
sort.Slice(result, func(i, j int) bool {
return result[i].Day.After(result[j].Day)
})
return result, nil
}
func (b Booking) Save() {
qStr, err := DB.Prepare((`UPDATE "anwesenheit" SET "id" = $1, "card_uid" = $2, "reader_id" = $3, "booking_type" = $4 WHERE "id" = $1;`))
qStr, err := DB.Prepare((`UPDATE "anwesenheit" SET "card_uid" = $2, "geraet_id" = $3, "check_in_out" = $4, "timestamp" = $5 WHERE "counter_id" = $1;`))
if err != nil {
log.Fatalf("Error preparing query: %v", err)
return
}
_, err = qStr.Query(b.CounterId, b.CardUID, b.GeraetID, b.CheckInOut)
_, err = qStr.Query(b.CounterId, b.CardUID, b.GeraetID, b.CheckInOut, b.Timestamp)
if err != nil {
log.Fatalf("Error executing query: %v", err)
return
}
}
func (b *Booking) GetBookingType() string {
switch b.CheckInOut{
case (1):
return "kommen"
case (2):
return "gehen"
case(255):
return "abgemeldet"
default:
return "Buchungs Typ unbekannt"
}
}
func (b *Booking) Update(nb Booking) {
if b.CheckInOut != nb.CheckInOut && nb.CheckInOut != 0 {
b.CheckInOut = nb.CheckInOut
@@ -135,6 +178,9 @@ func (b *Booking) Update(nb Booking) {
if b.GeraetID != nb.GeraetID && nb.GeraetID != 0 {
b.GeraetID = nb.GeraetID
}
if(b.Timestamp != nb.Timestamp){
b.Timestamp = nb.Timestamp
}
}
func checkLastBooking(b Booking) bool {