admin.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "time"
  7. "github.com/Unknwon/com"
  8. )
  9. type NoticeType int
  10. const (
  11. NOTICE_REPOSITORY NoticeType = iota + 1
  12. )
  13. // Notice represents a system notice for admin.
  14. type Notice struct {
  15. Id int64
  16. Type NoticeType
  17. Description string `xorm:"TEXT"`
  18. Created time.Time `xorm:"CREATED"`
  19. }
  20. // TrStr returns a translation format string.
  21. func (n *Notice) TrStr() string {
  22. return "admin.notices.type_" + com.ToStr(n.Type)
  23. }
  24. // CreateNotice creates new system notice.
  25. func CreateNotice(tp NoticeType, desc string) error {
  26. n := &Notice{
  27. Type: tp,
  28. Description: desc,
  29. }
  30. _, err := x.Insert(n)
  31. return err
  32. }
  33. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  34. func CreateRepositoryNotice(desc string) error {
  35. return CreateNotice(NOTICE_REPOSITORY, desc)
  36. }
  37. // CountNotices returns number of notices.
  38. func CountNotices() int64 {
  39. count, _ := x.Count(new(Notice))
  40. return count
  41. }
  42. // Notices returns number of notices in given page.
  43. func Notices(page, pageSize int) ([]*Notice, error) {
  44. notices := make([]*Notice, 0, pageSize)
  45. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  46. }
  47. // DeleteNotice deletes a system notice by given ID.
  48. func DeleteNotice(id int64) error {
  49. _, err := x.Id(id).Delete(new(Notice))
  50. return err
  51. }