You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

badge.go 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/setting"
  4. "code.gitea.io/gitea/modules/timeutil"
  5. "path/filepath"
  6. "strings"
  7. "xorm.io/builder"
  8. )
  9. type Badge struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. Name string
  12. LightedIcon string `xorm:"varchar(2048)"`
  13. GreyedIcon string `xorm:"varchar(2048)"`
  14. Url string `xorm:"varchar(2048)"`
  15. CategoryId int64
  16. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  17. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  18. DeletedAt timeutil.TimeStamp `xorm:"deleted"`
  19. }
  20. func (m *Badge) ToUserShow() *Badge4UserShow {
  21. return &Badge4UserShow{
  22. Name: m.Name,
  23. LightedIcon: GetIconOuterLink(m.LightedIcon),
  24. GreyedIcon: GetIconOuterLink(m.GreyedIcon),
  25. Url: m.Url,
  26. }
  27. }
  28. type GetBadgeOpts struct {
  29. BadgeType BadgeType
  30. LO ListOptions
  31. }
  32. type BadgeAndCategory struct {
  33. Badge Badge `xorm:"extends"`
  34. Category BadgeCategory `xorm:"extends"`
  35. }
  36. func (*BadgeAndCategory) TableName() string {
  37. return "badge"
  38. }
  39. func (m *BadgeAndCategory) ToShow() *Badge4AdminShow {
  40. return &Badge4AdminShow{
  41. ID: m.Badge.ID,
  42. Name: m.Badge.Name,
  43. LightedIcon: GetIconOuterLink(m.Badge.LightedIcon),
  44. GreyedIcon: GetIconOuterLink(m.Badge.GreyedIcon),
  45. Url: m.Badge.Url,
  46. CategoryName: m.Category.Name,
  47. CategoryId: m.Category.ID,
  48. CreatedUnix: m.Badge.CreatedUnix,
  49. UpdatedUnix: m.Badge.UpdatedUnix,
  50. }
  51. }
  52. type Badge4AdminShow struct {
  53. ID int64
  54. Name string
  55. LightedIcon string
  56. GreyedIcon string
  57. Url string
  58. CategoryName string
  59. CategoryId int64
  60. CreatedUnix timeutil.TimeStamp
  61. UpdatedUnix timeutil.TimeStamp
  62. }
  63. func (m Badge4AdminShow) ToDTO() Badge {
  64. return Badge{
  65. Name: m.Name,
  66. LightedIcon: m.LightedIcon,
  67. GreyedIcon: m.GreyedIcon,
  68. Url: m.Url,
  69. CategoryId: m.CategoryId,
  70. }
  71. }
  72. type BadgeOperateReq struct {
  73. ID int64
  74. Name string
  75. LightedIcon string
  76. GreyedIcon string
  77. Url string
  78. CategoryId int64
  79. }
  80. func (m BadgeOperateReq) ToDTO() Badge {
  81. return Badge{
  82. Name: m.Name,
  83. LightedIcon: m.LightedIcon,
  84. GreyedIcon: m.GreyedIcon,
  85. Url: m.Url,
  86. CategoryId: m.CategoryId,
  87. }
  88. }
  89. type Badge4UserShow struct {
  90. Name string
  91. LightedIcon string
  92. GreyedIcon string
  93. Url string
  94. }
  95. type BadgeShowWithStatus struct {
  96. Badge *Badge4UserShow
  97. IsLighted bool
  98. }
  99. type UserAllBadgeInCategory struct {
  100. CategoryName string
  101. CategoryId int64
  102. LightedNum int
  103. Badges []*BadgeShowWithStatus
  104. }
  105. func GetBadgeList(opts GetBadgeOpts) (int64, []*BadgeAndCategory, error) {
  106. if opts.LO.Page <= 0 {
  107. opts.LO.Page = 1
  108. }
  109. var cond = builder.NewCond()
  110. if opts.BadgeType > 0 {
  111. cond = cond.And(builder.Eq{"badge_category.type": opts.BadgeType})
  112. }
  113. n, err := x.Join("INNER", "badge_category", "badge_category.ID = badge.category_id").Where(cond).Count(&BadgeAndCategory{})
  114. if err != nil {
  115. return 0, nil, err
  116. }
  117. r := make([]*BadgeAndCategory, 0)
  118. if err = x.Join("INNER", "badge_category", "badge_category.ID = badge.category_id").Where(cond).OrderBy("badge.created_unix desc").Limit(opts.LO.PageSize, (opts.LO.Page-1)*opts.LO.PageSize).Find(&r); err != nil {
  119. return 0, nil, err
  120. }
  121. return n, r, nil
  122. }
  123. func AddBadge(m Badge) (int64, error) {
  124. return x.Insert(&m)
  125. }
  126. func UpdateBadgeById(id int64, param Badge) (int64, error) {
  127. return x.ID(id).Update(&param)
  128. }
  129. func DelBadge(id int64) (int64, error) {
  130. return x.ID(id).Delete(&Badge{})
  131. }
  132. func GetBadgeById(id int64) (*Badge, error) {
  133. m := &Badge{}
  134. has, err := x.ID(id).Get(m)
  135. if err != nil {
  136. return nil, err
  137. } else if !has {
  138. return nil, &ErrRecordNotExist{}
  139. }
  140. return m, nil
  141. }
  142. func GetBadgeByCategoryId(categoryId int64) ([]*Badge, error) {
  143. r := make([]*Badge, 0)
  144. err := x.Where("category_id = ?", categoryId).Find(&r)
  145. return r, err
  146. }
  147. func GetCustomIconByHash(hash string) string {
  148. if len(hash) == 0 {
  149. return ""
  150. }
  151. return filepath.Join(setting.IconUploadPath, hash)
  152. }
  153. func GetIconOuterLink(hash string) string {
  154. return strings.TrimRight(setting.AppSubURL, "/") + "/show/icon/" + hash
  155. }