added booking types + working on overtime

This commit is contained in:
2025-09-01 22:41:21 +02:00
parent aa152866d9
commit de6da2906f
16 changed files with 284 additions and 29 deletions

View File

@@ -23,11 +23,12 @@ func (e SameBookingError) Error() string {
}
type Booking struct {
CardUID string `json:"card_uid"`
GeraetID int16 `json:"geraet_id"`
CheckInOut int16 `json:"check_in_out"`
Timestamp time.Time `json:"timestamp"`
CounterId int `json:"counter_id"`
CardUID string `json:"card_uid"`
GeraetID int16 `json:"geraet_id"`
CheckInOut int16 `json:"check_in_out"`
Timestamp time.Time `json:"timestamp"`
CounterId int `json:"counter_id"`
BookingType BookingType `json:"booking_type"`
}
type IDatabase interface {
@@ -37,31 +38,52 @@ type IDatabase interface {
var DB IDatabase
func (b *Booking) New(card_uid string, geraet_id int16, check_in_out int16) Booking {
func (b *Booking) New(cardUid string, gereatId int16, checkInOut int16, typeId int8) Booking {
bookingType, err := GetBookingTypeById(typeId)
if err != nil {
log.Printf("Cannot get booking type %d, from database!", typeId)
}
return Booking{
CardUID: card_uid,
GeraetID: geraet_id,
CheckInOut: check_in_out,
CardUID: cardUid,
GeraetID: gereatId,
CheckInOut: checkInOut,
BookingType: bookingType,
}
}
func (b *Booking) FromUrlParams(params url.Values) Booking {
var booking Booking
if _check_in_out, err := strconv.Atoi(params.Get("check_in_out")); err == nil {
booking.CheckInOut = int16(_check_in_out)
}
if _geraet_id, err := strconv.Atoi(params.Get("geraet_id")); err == nil {
booking.GeraetID = int16(_geraet_id)
}
if _booking_type, err := strconv.Atoi(params.Get("booking_type")); err == nil {
booking.BookingType.Id = int8(_booking_type)
}
booking.CardUID = params.Get("card_uid")
return booking
}
func (b *Booking) Verify() bool {
//check for overlapping time + arbeitszeit verstoß
if b.CardUID == "" { //|| b.GeraetID == 0 || b.CheckInOut == 0 {
log.Println("Booking verify failed invalid CardUID!")
return false
}
if b.CheckInOut == 0 {
log.Println("Booking verify failed invalid CheckInOut!")
return false
}
if bookingType, err := GetBookingTypeById(b.BookingType.Id); err != nil {
log.Println("Booking verify failed invalid BookingType.Id!")
return false
} else {
b.BookingType.Name = bookingType.Name
}
return true
}
@@ -69,11 +91,11 @@ func (b *Booking) Insert() error {
if !checkLastBooking(*b) {
return SameBookingError{}
}
stmt, err := DB.Prepare((`INSERT INTO anwesenheit (card_uid, geraet_id, check_in_out) VALUES ($1, $2, $3) RETURNING counter_id, timestamp`))
stmt, err := DB.Prepare((`INSERT INTO anwesenheit (card_uid, geraet_id, check_in_out, anwesenheit_typ) VALUES ($1, $2, $3, $4) RETURNING counter_id, timestamp`))
if err != nil {
return err
}
err = stmt.QueryRow(b.CardUID, b.GeraetID, b.CheckInOut).Scan(&b.CounterId, &b.Timestamp)
err = stmt.QueryRow(b.CardUID, b.GeraetID, b.CheckInOut, b.BookingType.Id).Scan(&b.CounterId, &b.Timestamp)
if err != nil {
return err
}
@@ -84,11 +106,11 @@ func (b *Booking) InsertWithTimestamp() error {
if b.Timestamp.IsZero() {
return b.Insert()
}
stmt, err := DB.Prepare((`INSERT INTO anwesenheit (card_uid, geraet_id, check_in_out, timestamp) VALUES ($1, $2, $3, $4) RETURNING counter_id`))
stmt, err := DB.Prepare((`INSERT INTO anwesenheit (card_uid, geraet_id, check_in_out, anwesenheit_typ, timestamp) VALUES ($1, $2, $3, $4, $5) RETURNING counter_id`))
if err != nil {
return err
}
err = stmt.QueryRow(b.CardUID, b.GeraetID, b.CheckInOut, b.Timestamp).Scan(&b.CounterId)
err = stmt.QueryRow(b.CardUID, b.GeraetID, b.CheckInOut, b.BookingType.Id, b.Timestamp).Scan(&b.CounterId)
if err != nil {
return err
}
@@ -97,18 +119,15 @@ func (b *Booking) InsertWithTimestamp() error {
func (b *Booking) GetBookingById(booking_id int) (Booking, error) {
var booking Booking
qStr, err := DB.Prepare((`SELECT counter_id, timestamp, card_uid, geraet_id, check_in_out FROM anwesenheit WHERE counter_id = $1`))
qStr, err := DB.Prepare((`SELECT counter_id, timestamp, card_uid, geraet_id, check_in_out, anwesenheit_typ FROM anwesenheit WHERE counter_id = $1`))
if err != nil {
return booking, err
}
err = qStr.QueryRow(booking_id).Scan(&booking.CounterId, &booking.Timestamp, &booking.CardUID, &booking.GeraetID, &booking.CheckInOut)
// TODO: also get booking type name
err = qStr.QueryRow(booking_id).Scan(&booking.CounterId, &booking.Timestamp, &booking.CardUID, &booking.GeraetID, &booking.CheckInOut, &booking.BookingType.Id)
if err != nil {
return booking, err
}
// if !booking.Verify() {
// fmt.Printf("Booking verification failed! %d", )
// return booking, nil
// }
return booking, nil
}
@@ -264,6 +283,7 @@ func (b *Booking) UpdateTime(newTime time.Time) {
}
log.Println("Updating")
b.Update(newBooking)
// TODO Check verify
b.Verify()
b.Save()
}
@@ -272,7 +292,7 @@ func (b *Booking) ToString() string {
return fmt.Sprintf("Booking %d: at: %s, as type: %d", b.CounterId, b.Timestamp.Format("15:04"), b.CheckInOut)
}
func GetBokkingTypes() ([]BookingType, error) {
func GetBookingTypes() ([]BookingType, error) {
var types []BookingType
qStr, err := DB.Prepare("SELECT anwesenheit_id, anwesenheit_name FROM s_anwesenheit_typen;")
if err != nil {
@@ -295,6 +315,21 @@ func GetBokkingTypes() ([]BookingType, error) {
return types, nil
}
func GetBookingTypeById(bookingTypeId int8) (BookingType, error) {
var bookingType BookingType = BookingType{Id: bookingTypeId}
qStr, err := DB.Prepare("SELECT anwesenheit_name FROM s_anwesenheit_typen WHERE anwesenheit_id = $1;")
if err != nil {
return bookingType, err
}
defer qStr.Close()
err = qStr.QueryRow(bookingTypeId).Scan(&bookingType.Name)
if err != nil {
return bookingType, err
}
return bookingType, nil
}
func GetBookingTypesCached() []BookingType {
types, err := definedTypes.Get("s_anwesenheit_typen")
if err != nil {