Skip to content

Instantly share code, notes, and snippets.

@KevinStrong
KevinStrong / getCheckins.go
Created November 16, 2021 19:06
A golang function to query for a kioskEvent's checkins using gorm
func getKioskEvent(kioskID, eventID uint, db *gorm.DB) (KioskEvent, error) {
kioskEvent := KioskEvent{}
result := db.Preload("Checkins").Where(&KioskEvent{KioskID: kioskID, EventID: eventID}).First(&kioskEvent)
return kioskEvent, result.Error
}
@KevinStrong
KevinStrong / Checkin.go
Created November 16, 2021 19:04
A golang struct for a checkin
type Checkin struct {
gorm.Model
EventID uint
KioskID uint
CheckinDatetime time.Time
Name string
}
@KevinStrong
KevinStrong / KioskEvent.go
Created November 16, 2021 19:03
The golang model of a KioskEvent, it supports adding, querying and deleting checkins.
type KioskEvent struct {
Checkins []Checkin `gorm:"foreignKey:KioskID,EventID;References:KioskID,EventID"`
KioskID uint `gorm:"primaryKey"`
EventID uint `gorm:"primarykey"`
}
@KevinStrong
KevinStrong / checkins.yaml
Last active January 7, 2022 23:03
Liquibase yaml file to create a checkins table that links to kiosk_events table
databaseChangeLog:
- changeSet:
- id: 4-create-checkins-table
author: [email protected]
changes:
- createTable:
tableName: checkins
remarks: Where checkins are recorded.
schemaName: checkin
columns:
@KevinStrong
KevinStrong / read.go
Created November 8, 2021 22:21
Golang code that uses gorm to read events and kiosks from the database
func getEvent(eventID uint, db *gorm.DB) (Event, error) {
event := Event{ Model: gorm.Model{ID: eventID}}
result := db.Preload("Kiosks").First(&event)
return event, result.Error
}
func getKiosk( kioskID uint, db *gorm.DB) (Kiosk, error) {
kiosk := Kiosk{ Model: gorm.Model{ID: kioskID}}
result := db.Preload("Events").First(&kiosk)
return kiosk, result.Error
@KevinStrong
KevinStrong / link_kiosk_and_event.go
Created November 8, 2021 22:19
A golang func to link an event to a kiosk in the db using gorm
@KevinStrong
KevinStrong / create.go
Created November 8, 2021 22:13
Create a kiosk or event
func createKiosk(db *gorm.DB, name string) error {
kiosk := Kiosk{
Name: name,
}
result := db.Create(&kiosk)
return result.Error
}
func createEvent(db *gorm.DB, name string) error {
event := Event{
@KevinStrong
KevinStrong / model.go
Last active November 29, 2021 16:25
The golang model for kiosk, events and kioskEvents
package join
import "gorm.io/gorm"
type Event struct {
gorm.Model
Kiosks []Kiosk `gorm:"many2many:kiosk_events"`
Name string
}
@KevinStrong
KevinStrong / 3_join.yaml
Last active February 15, 2022 00:39
Create a join table between kiosks and events
databaseChangeLog:
- changeSet:
- id: 3-create-join-table
author: [email protected]
changes:
- createTable:
tableName: kiosk_events
remarks: A join table between kiosks and events
schemaName: checkin
columns:
@KevinStrong
KevinStrong / 2_events_and_kiosks.yaml
Last active November 8, 2021 21:45
A liquibase yaml file to create kiosk and event tables
databaseChangeLog:
- changeSet:
- id: 2-create-events-and-kiosks-table
author: [email protected]
changes:
- createTable:
tableName: events
schemaName: checkin
columns:
- column: