53 lines
849 B
Go
53 lines
849 B
Go
package model
|
|
|
|
import (
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Report struct {
|
|
ID uint `gorm:"primary_key"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Company Company
|
|
CompanyID uint
|
|
}
|
|
|
|
func (report *Report) CreateOrUpdate() (*Report) {
|
|
|
|
GetDB().Save(report)
|
|
|
|
return report;
|
|
}
|
|
|
|
func GetReports() ([]*Report) {
|
|
|
|
reports := make([]*Report, 0)
|
|
GetDB().Find(&reports)
|
|
|
|
sort.Slice(reports, func(i, j int) bool {
|
|
return reports[j].CreatedAt.Before(reports[i].CreatedAt)
|
|
})
|
|
|
|
return reports
|
|
}
|
|
|
|
func GetReport(id uint) (*Report) {
|
|
|
|
report := &Report{}
|
|
if GetDB().Where("id = ?", id).First(report).RecordNotFound() {
|
|
return nil
|
|
}
|
|
|
|
return report
|
|
}
|
|
|
|
func GetReportsForCompany(companyID uint) ([]*Report) {
|
|
|
|
reports := make([]*Report, 0)
|
|
if GetDB().Where("company_id = ?", companyID).Find(&reports).RecordNotFound() {
|
|
return nil
|
|
}
|
|
|
|
return reports
|
|
} |