entry.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const (
  10. EntryStatusUnread = "unread"
  11. EntryStatusRead = "read"
  12. EntryStatusRemoved = "removed"
  13. DefaultSortingOrder = "published_at"
  14. DefaultSortingDirection = "desc"
  15. )
  16. type Entry struct {
  17. ID int64 `json:"id"`
  18. UserID int64 `json:"user_id"`
  19. FeedID int64 `json:"feed_id"`
  20. Status string `json:"status"`
  21. Hash string `json:"hash"`
  22. Title string `json:"title"`
  23. URL string `json:"url"`
  24. Date time.Time `json:"published_at"`
  25. Content string `json:"content"`
  26. Author string `json:"author"`
  27. Enclosures EnclosureList `json:"enclosures,omitempty"`
  28. Feed *Feed `json:"feed,omitempty"`
  29. Category *Category `json:"category,omitempty"`
  30. }
  31. type Entries []*Entry
  32. func ValidateEntryStatus(status string) error {
  33. switch status {
  34. case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
  35. return nil
  36. }
  37. return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
  38. }
  39. func ValidateEntryOrder(order string) error {
  40. switch order {
  41. case "id", "status", "published_at", "category_title", "category_id":
  42. return nil
  43. }
  44. return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "published_at", "category_title", "category_id"`)
  45. }
  46. func ValidateDirection(direction string) error {
  47. switch direction {
  48. case "asc", "desc":
  49. return nil
  50. }
  51. return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
  52. }
  53. func GetOppositeDirection(direction string) string {
  54. if direction == "asc" {
  55. return "desc"
  56. }
  57. return "asc"
  58. }