package models import ( "code.gitea.io/gitea/modules/timeutil" "xorm.io/builder" ) type Badge struct { ID int64 `xorm:"pk autoincr"` Name string LightedIcon string `xorm:"varchar(2048)"` GreyedIcon string `xorm:"varchar(2048)"` Url string `xorm:"varchar(2048)"` CategoryId int64 CreatedUnix timeutil.TimeStamp `xorm:"created"` UpdatedUnix timeutil.TimeStamp `xorm:"updated"` DeletedAt timeutil.TimeStamp `xorm:"deleted"` } func (m *Badge) ToUserShow() *Badge4UserShow { return &Badge4UserShow{ Name: m.Name, LightedIcon: m.LightedIcon, GreyedIcon: m.GreyedIcon, Url: m.Url, } } type GetBadgeOpts struct { BadgeType BadgeType } type BadgeAndCategory struct { Badge Badge `xorm:"extends"` Category BadgeCategory `xorm:"extends"` } func (*BadgeAndCategory) TableName() string { return "badge" } func (m *BadgeAndCategory) ToShow() *Badge4AdminShow { return &Badge4AdminShow{ ID: m.Badge.ID, Name: m.Badge.Name, LightedIcon: m.Badge.LightedIcon, GreyedIcon: m.Badge.GreyedIcon, Url: m.Badge.Url, CategoryName: m.Category.Name, CategoryId: m.Category.ID, CreatedUnix: m.Badge.CreatedUnix, UpdatedUnix: m.Badge.UpdatedUnix, } } type Badge4AdminShow struct { ID int64 Name string LightedIcon string GreyedIcon string Url string CategoryName string CategoryId int64 CreatedUnix timeutil.TimeStamp UpdatedUnix timeutil.TimeStamp } func (m Badge4AdminShow) ToDTO() Badge { return Badge{ Name: m.Name, LightedIcon: m.LightedIcon, GreyedIcon: m.GreyedIcon, Url: m.Url, CategoryId: m.CategoryId, } } type Badge4UserShow struct { Name string LightedIcon string GreyedIcon string Url string } type BadgeShowWithStatus struct { Badge *Badge4UserShow IsLighted bool } type UserAllBadgeInCategory struct { CategoryName string CategoryId int64 LightedNum int Badges []*BadgeShowWithStatus } func GetBadgeList(opts GetBadgeOpts) ([]*BadgeAndCategory, error) { var cond = builder.NewCond() if opts.BadgeType > 0 { cond = cond.And(builder.Eq{"badge_category.type": opts.BadgeType}) } r := make([]*BadgeAndCategory, 0) if err := x.Join("INNER", "badge_category", "badge_category.ID = badge.category_id").Where(cond).OrderBy("badge.created_unix desc").Find(&r); err != nil { return nil, err } return r, nil } func AddBadge(m Badge) (int64, error) { return x.Insert(&m) } func UpdateBadgeById(id int64, param Badge) (int64, error) { return x.ID(id).Update(¶m) } func DelBadge(id int64) (int64, error) { return x.ID(id).Delete(&Badge{}) } func GetBadgeById(id int64) (*Badge, error) { m := &Badge{} has, err := x.ID(id).Get(m) if err != nil { return nil, err } else if !has { return nil, &ErrRecordNotExist{} } return m, nil } func GetBadgeByCategoryId(categoryId int64) ([]*Badge, error) { r := make([]*Badge, 0) err := x.Where("category_id = ?", categoryId).Find(&r) return r, err }