The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.9 KiB

  1. package model
  2. import (
  3. "database/sql"
  4. "errors"
  5. "time"
  6. "git.aiterp.net/AiteRP/aitestory/server"
  7. )
  8. // Header contains a subset of the page database
  9. // table needed for the front page. It's a read-only
  10. // model.
  11. type Header struct {
  12. ID string `json:"id"`
  13. Name string `json:"name"`
  14. Author string `json:"author"`
  15. Category string `json:"category"`
  16. FictionalDate time.Time `json:"fictionalDate"`
  17. PublishDate time.Time `json:"publishDate"`
  18. EditDate time.Time `json:"editDate"`
  19. Dated bool `json:"dated"`
  20. PrimaryTag *Tag `json:"primaryTag"`
  21. }
  22. // ListHeaders grabs all the general pages from
  23. // the database to list them
  24. func ListHeaders() ([]Header, error) {
  25. const query = `
  26. SELECT page.id,page.name,author,category,fictional_date,publish_date,edit_date,dated,tag.id,tag.type,tag.name
  27. FROM page
  28. LEFT JOIN page_tag ON (page.id = page_tag.page_id AND page_tag.primary = true)
  29. LEFT JOIN tag ON (tag.id = page_tag.tag_id)
  30. WHERE page.specific=false AND page.published=true AND page.unlisted=false;
  31. `
  32. db := server.Main.DB
  33. rows, err := db.Query(query)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer rows.Close()
  38. results := make([]Header, 0, 64)
  39. header := Header{}
  40. for rows.Next() {
  41. err := parseHeader(&header, rows)
  42. if err != nil {
  43. return nil, err
  44. }
  45. results = append(results, header)
  46. }
  47. return results, nil
  48. }
  49. // ListHeadersByTag lists all headers that has the tag
  50. func ListHeadersByTag(tag *Tag) ([]Header, error) {
  51. const query = `
  52. SELECT page.id,page.name,page.author,page.category,page.fictional_date,page.publish_date,page.edit_date,page.dated,tag.id,tag.type,tag.name
  53. FROM page_tag
  54. RIGHT JOIN page ON page.id = page_tag.page_id
  55. LEFT JOIN (page_tag AS pt2) ON (page.id = pt2.page_id AND pt2.primary = true)
  56. LEFT JOIN (tag AS tag) ON (tag.id = pt2.tag_id)
  57. WHERE page_tag.tag_id=?
  58. `
  59. if tag == nil {
  60. return nil, errors.New("no tag")
  61. }
  62. db := server.Main.DB
  63. rows, err := db.Query(query, tag.ID)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer rows.Close()
  68. results := make([]Header, 0, 64)
  69. header := Header{}
  70. for rows.Next() {
  71. err := parseHeader(&header, rows)
  72. if err != nil {
  73. return nil, err
  74. }
  75. results = append(results, header)
  76. }
  77. return results, nil
  78. }
  79. func parseHeader(header *Header, rows *sql.Rows) error {
  80. var tagID, tagName, tagType string
  81. var fictionalDate, publishDate, editDate string
  82. var err error
  83. rows.Scan(&header.ID, &header.Name, &header.Author, &header.Category, &fictionalDate, &publishDate, &editDate, &header.Dated, &tagID, &tagType, &tagName)
  84. if tagID != "" {
  85. header.PrimaryTag = &Tag{tagID, tagName, tagType}
  86. }
  87. header.FictionalDate, err = time.Parse("2006-01-02 15:04:05", fictionalDate)
  88. if err != nil {
  89. return err
  90. }
  91. header.PublishDate, err = time.Parse("2006-01-02 15:04:05", publishDate)
  92. if err != nil {
  93. return err
  94. }
  95. header.EditDate, err = time.Parse("2006-01-02 15:04:05", editDate)
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. }