51 lines
1 KiB
Go
51 lines
1 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func GetScheduleItems(db *gorm.DB, fromDate time.Time, toDate time.Time) ([]ScheduleItem, error) {
|
|
var items []ScheduleItem
|
|
res := db.Where("date BETWEEN ? AND ?", fromDate.Format(time.DateOnly), toDate.Format(time.DateOnly)).Find(&items)
|
|
return items, res.Error
|
|
}
|
|
|
|
func GetScheduleItem(db *gorm.DB, id int) (*ScheduleItem, error) {
|
|
var item ScheduleItem
|
|
res := db.First(&item, id)
|
|
|
|
if res.RowsAffected < 1 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &item, res.Error
|
|
}
|
|
|
|
func DeleteScheduleItem(db *gorm.DB, id int) (bool, error) {
|
|
res := db.Delete(&ScheduleItem{}, id)
|
|
|
|
if res.Error != nil {
|
|
return false, res.Error
|
|
}
|
|
|
|
if res.RowsAffected < 1 {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func CreateScheduleItem(db *gorm.DB, name string, description *string, duration Duration, date time.Time) (ScheduleItem, error) {
|
|
item := ScheduleItem{
|
|
Name: name,
|
|
Description: description,
|
|
Duration: duration,
|
|
Date: date,
|
|
}
|
|
|
|
res := db.Create(&item)
|
|
|
|
return item, res.Error
|
|
}
|