entry.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = "asc"
  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. Starred bool `json:"starred"`
  30. Enclosures EnclosureList `json:"enclosures,omitempty"`
  31. Feed *Feed `json:"feed,omitempty"`
  32. Category *Category `json:"category,omitempty"`
  33. }
  34. // Entries represents a list of entries.
  35. type Entries []*Entry
  36. // ValidateEntryStatus makes sure the entry status is valid.
  37. func ValidateEntryStatus(status string) error {
  38. switch status {
  39. case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
  40. return nil
  41. }
  42. return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
  43. }
  44. // ValidateEntryOrder makes sure the sorting order is valid.
  45. func ValidateEntryOrder(order string) error {
  46. switch order {
  47. case "id", "status", "published_at", "category_title", "category_id":
  48. return nil
  49. }
  50. return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "published_at", "category_title", "category_id"`)
  51. }
  52. // ValidateDirection makes sure the sorting direction is valid.
  53. func ValidateDirection(direction string) error {
  54. switch direction {
  55. case "asc", "desc":
  56. return nil
  57. }
  58. return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
  59. }
  60. // ValidateRange makes sure the offset/limit values are valid.
  61. func ValidateRange(offset, limit int) error {
  62. if offset < 0 {
  63. return fmt.Errorf(`Offset value should be >= 0`)
  64. }
  65. if limit < 0 {
  66. return fmt.Errorf(`Limit value should be >= 0`)
  67. }
  68. return nil
  69. }
  70. // OppositeDirection returns the opposite sorting direction.
  71. func OppositeDirection(direction string) string {
  72. if direction == "asc" {
  73. return "desc"
  74. }
  75. return "asc"
  76. }