V2
This commit is contained in:
@ -42,6 +42,9 @@ func (db *PgDatabase) migrate() {
|
||||
db.Db.AutoMigrate(&models.PlanDay{})
|
||||
db.Db.AutoMigrate(&models.PlanWeek{})
|
||||
db.Db.AutoMigrate(&models.PlanMonth{})
|
||||
db.Db.AutoMigrate(&models.TrackingCategory{})
|
||||
db.Db.AutoMigrate(&models.TrackingItem{})
|
||||
db.Db.AutoMigrate(&models.Inbox{})
|
||||
}
|
||||
|
||||
func (db *PgDatabase) WriteJournalEntry(entry interface{}) error {
|
||||
@ -111,7 +114,80 @@ func (db *PgDatabase) WritePlanMonth(entry interface{}) error {
|
||||
|
||||
func (db *PgDatabase) GetPlanMonthForDate(date time.Time) (interface{}, error) {
|
||||
entry := models.PlanMonth{Date: datatypes.Date(date)}
|
||||
db.Db.First(&entry)
|
||||
err := db.Db.First(&entry).Error
|
||||
if err != nil {
|
||||
log.Print("Error getting plan month from database.")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) GetTrackingItemsForDate(date time.Time) (interface{}, error) {
|
||||
entries := []models.TrackingItem{}
|
||||
err := db.Db.Where("date = ?", datatypes.Date(date)).Find(&entries).Error
|
||||
if err != nil {
|
||||
log.Print("Error getting tracking items from database.")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) WriteTrackingItems(entries interface{}) error {
|
||||
trackingItems := entries.([]models.TrackingItem)
|
||||
if len(trackingItems) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&trackingItems).Error
|
||||
if err != nil {
|
||||
log.Print("Error writing tracking items to database.")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) GetTrackingCategories() (interface{}, error) {
|
||||
categories := []models.TrackingCategory{}
|
||||
err := db.Db.Find(&categories).Error
|
||||
if err != nil {
|
||||
log.Print("Error getting tracking categories from database.")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) WriteInboxItem(entry interface{}) error {
|
||||
inboxItem := entry.(models.Inbox)
|
||||
err := db.Db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&inboxItem).Error
|
||||
if err != nil {
|
||||
log.Print("Error writing inbox item to database.")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) DeleteInboxItem(id int32) error {
|
||||
err := db.Db.Delete(&models.Inbox{}, id).Error
|
||||
if err != nil {
|
||||
log.Print("Error deleting inbox item from database.")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *PgDatabase) GetInboxItems() ([]interface{}, error) {
|
||||
var inboxItems []models.Inbox
|
||||
db.Db.Find(&inboxItems)
|
||||
|
||||
var interfaceSlice []interface{} = make([]interface{}, len(inboxItems))
|
||||
for i, d := range inboxItems {
|
||||
interfaceSlice[i] = d
|
||||
}
|
||||
|
||||
return interfaceSlice, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user