46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gitea.zokki.net/zokki/uni/web43-diary/context"
|
|
"gitea.zokki.net/zokki/uni/web43-diary/internal/database"
|
|
)
|
|
|
|
type Diary struct {
|
|
database.SQLTable
|
|
ID uint32 `json:"id,omitempty" db:"NOT NULL PRIMARY KEY AUTO_INCREMENT"`
|
|
CreationTime time.Time `json:"creationTime,omitempty" db:"NOT NULL DEFAULT CURRENT_TIMESTAMP"`
|
|
OwnerID uint32 `json:"ownerId,omitempty" db:"NOT NULL" foreignKey:"user(ID) ON DELETE CASCADE"`
|
|
Owner *User `json:"owner,omitempty" db:"-"`
|
|
Title string `json:"title,omitempty" db:"NOT NULL"`
|
|
Content string `json:"content,omitempty" dbType:"TEXT" db:"NOT NULL"`
|
|
Tags []*Tag `json:"tags,omitempty" db:"-"`
|
|
}
|
|
|
|
func init() {
|
|
database.AddCreateTableQueue(&Diary{})
|
|
}
|
|
|
|
func (diary *Diary) LoadForeignValues(ctx *context.Context) error {
|
|
var err error
|
|
diary.Owner, err = database.GetOne(ctx, &User{ID: diary.OwnerID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
diary.Tags, err = database.GetAllWhere(ctx, database.NewQueryBuilder(&Tag{}).
|
|
Select("tag.*").
|
|
Join(database.Join, &DiaryTags{}, "`tag`.`ID` = `diary_tags`.`TagID`").
|
|
Where(&database.QueryCondition{Row: "`diary_tags`.`DiaryID`", Operator: database.Equal, Value: diary.ID}),
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (diary *Diary) ShortContent() string {
|
|
if len(diary.Content) > 200 {
|
|
return diary.Content[:197] + "..."
|
|
}
|
|
return diary.Content
|
|
}
|