-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
81 lines (66 loc) · 1.95 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"hmcalister/EventTrackerApp/backend/database"
"hmcalister/EventTrackerApp/backend/models"
"log"
"gorm.io/gorm"
)
// App struct
type App struct {
ctx context.Context
databaseConnection *gorm.DB
allEventsList []*models.Event
allEventsMap map[uint]*models.Event
}
// InitApp creates a new App application struct
func InitApp() *App {
databaseConnection, err := database.CreateDatabase(database.DEFAULT_DATABASE_FILE)
if err != nil {
log.Fatalf("error during creation of database: %v\n", err)
}
app := &App{
databaseConnection: databaseConnection,
}
app.allEventsList, app.allEventsMap = app.initAllEventsFromDatabase()
return app
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) initAllEventsFromDatabase() ([]*models.Event, map[uint]*models.Event) {
var allEvents []*models.Event
a.databaseConnection.Find(&allEvents)
allEventsMap := make(map[uint]*models.Event)
for _, event := range allEvents {
allEventsMap[event.ID] = event
}
return allEvents, allEventsMap
}
func (a *App) GetAllEvents() []*models.Event {
var allEvents []*models.Event
a.databaseConnection.Find(&allEvents)
return allEvents
}
func (a *App) CreateEvent(newEvent *models.Event) *models.Event {
models.RegisterNewEventInDatabase(newEvent, a.databaseConnection)
return newEvent
}
func (a *App) UpdateEvent(updatedEvent *models.Event) *models.Event {
updatedEvent.UpdateEventInDatabase(a.databaseConnection)
return updatedEvent
}
func (a *App) DismissEvent(event *models.Event) *models.Event {
dismissedEvent := event.DismissEvent(a.databaseConnection)
return dismissedEvent
}
func (a *App) DeleteEvent(event *models.Event) *models.Event {
event.IsRecurring = false
return a.DismissEvent(event)
}
func (a *App) PrintEventStruct(event *models.Event) {
fmt.Printf("%+v\n", event)
}