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 {

View File

@@ -2,6 +2,7 @@ package models
import (
"fmt"
"strings"
)
type User struct {
@@ -64,3 +65,21 @@ func (u *User) Logout() error {
}
return nil
}
func (u *User) GetById(card_uid string) (User, error) {
var user User
qStr, err := DB.Prepare((`SELECT card_uid, vorname, nachname, hauptbeschaeftigung_ort FROM personal_daten WHERE card_uid = $1;`))
if err != nil {
return user, err
}
err = qStr.QueryRow(card_uid).Scan(&user.CardUID, &user.Vorname, &user.Name, &user.HauptbeschaeftigungsOrt)
if err != nil{
return user, err
}
return user, nil
}
func (u *User) Login(password string) bool {
userPassword := strings.ToLower(fmt.Sprintf("%s_%s", u.Vorname, u.Name)) //temp password: "max_mustermann"
return userPassword == password
}

46
Backend/models/workDay.go Normal file
View File

@@ -0,0 +1,46 @@
package models
import "time"
type WorkDay struct {
Day time.Time
workTime time.Duration
Bookings []Booking
}
func (d WorkDay) GetWorkTime() time.Duration{
if(d.workTime > 0) {
return d.workTime
}
var lastCame Booking
for _, booking := range d.Bookings{
if(booking.CheckInOut == 1){//kommen
lastCame = booking
}else{
d.workTime = d.workTime + booking.Timestamp.Sub(lastCame.Timestamp)
}
}
if(time.Since(d.Day) < 24 * time.Hour && len(d.Bookings) % 2 == 1){
d.workTime += time.Since(lastCame.Timestamp)
}
return d.workTime
}
func (d WorkDay) GetWorkTimeString() string {
str := d.GetWorkTime().Abs().Round(time.Minute).String()
str = str[:len(str)-2] + "in"
if(len(str) > 2){
return str
}
return ""
}
func (d WorkDay) RequiresAction() bool {
return d.Bookings[len(d.Bookings)-1].CheckInOut == 255
}
func (d WorkDay) GetWorkDayProgress() uint8 {
defaultWorkTime, _ := time.ParseDuration("7h42m")
progress := (d.GetWorkTime().Seconds()/defaultWorkTime.Seconds())*100
return uint8(progress)
}