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.3 kB

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