admin.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // GetNotices returns given number of notices with offset.
  43. func GetNotices(num, offset int) ([]*Notice, error) {
  44. notices := make([]*Notice, 0, num)
  45. err := x.Limit(num, offset).Desc("id").Find(&notices)
  46. return notices, err
  47. }
  48. // DeleteNotice deletes a system notice by given ID.
  49. func DeleteNotice(id int64) error {
  50. _, err := x.Id(id).Delete(new(Notice))
  51. return err
  52. }