entry.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import (
  6. "fmt"
  7. "time"
  8. )
  9. // Entry statuses
  10. const (
  11. EntryStatusUnread = "unread"
  12. EntryStatusRead = "read"
  13. EntryStatusRemoved = "removed"
  14. DefaultSortingOrder = "published_at"
  15. DefaultSortingDirection = "desc"
  16. )
  17. // Entry represents a feed item in the system.
  18. type Entry struct {
  19. ID int64 `json:"id"`
  20. UserID int64 `json:"user_id"`
  21. FeedID int64 `json:"feed_id"`
  22. Status string `json:"status"`
  23. Hash string `json:"hash"`
  24. Title string `json:"title"`
  25. URL string `json:"url"`
  26. Date time.Time `json:"published_at"`
  27. Content string `json:"content"`
  28. Author string `json:"author"`
  29. Enclosures EnclosureList `json:"enclosures,omitempty"`
  30. Feed *Feed `json:"feed,omitempty"`
  31. Category *Category `json:"category,omitempty"`
  32. }
  33. // Entries represents a list of entries.
  34. type Entries []*Entry
  35. // ValidateEntryStatus makes sure the entry status is valid.
  36. func ValidateEntryStatus(status string) error {
  37. switch status {
  38. case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
  39. return nil
  40. }
  41. return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
  42. }
  43. // ValidateEntryOrder makes sure the sorting order is valid.
  44. func ValidateEntryOrder(order string) error {
  45. switch order {
  46. case "id", "status", "published_at", "category_title", "category_id":
  47. return nil
  48. }
  49. return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "published_at", "category_title", "category_id"`)
  50. }
  51. // ValidateDirection makes sure the sorting direction is valid.
  52. func ValidateDirection(direction string) error {
  53. switch direction {
  54. case "asc", "desc":
  55. return nil
  56. }
  57. return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
  58. }
  59. // GetOppositeDirection returns the opposite sorting direction.
  60. func GetOppositeDirection(direction string) string {
  61. if direction == "asc" {
  62. return "desc"
  63. }
  64. return "asc"
  65. }