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.

154 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package model
  2. import (
  3. "errors"
  4. "git.aiterp.net/AiteRP/aitestory/server"
  5. "git.aiterp.net/gisle/wrouter/generate"
  6. )
  7. // TagTypes are the allowed values for Tag.Type
  8. var TagTypes = []string{
  9. "Location",
  10. "Character",
  11. "Event",
  12. "Organization",
  13. "Source",
  14. }
  15. // Tag describes a tag
  16. type Tag struct {
  17. ID string
  18. Type string
  19. Name string
  20. }
  21. // Insert adds the tag to the database, giving it a new unique ID
  22. func (tag *Tag) Insert() error {
  23. db := server.Main.DB
  24. // Validate tag type
  25. if err := tag.Validate(); err != nil {
  26. return err
  27. }
  28. // Generate an ID if none exists
  29. if tag.ID == "" {
  30. tag.ID = generate.ID()
  31. }
  32. // Do the thing
  33. _, err := db.Exec("INSERT INTO `tag` (id,type,name,disabled) VALUES (?,?,?,false)", tag.ID, tag.Type, tag.Name)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. // Update saves the entry for the tag in the database
  40. func (tag *Tag) Update() error {
  41. db := server.Main.DB
  42. // Validate tag type
  43. if err := tag.Validate(); err != nil {
  44. return err
  45. }
  46. // Do the thing
  47. _, err := db.Exec("UPDATE `tag` SET type=?,name=? WHERE id=?", tag.Type, tag.Name, tag.ID)
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. }
  53. // Delete removes a tag from the database
  54. func (tag *Tag) Delete() error {
  55. db := server.Main.DB
  56. // Do the thing
  57. results, err := db.Exec("DELETE FROM `tag` WHERE id=? LIMIT 1", tag.ID)
  58. if err != nil {
  59. return err
  60. }
  61. // Count the stuffs that were done things to
  62. affected, err := results.RowsAffected()
  63. if err != nil {
  64. return err
  65. }
  66. if affected == 0 {
  67. return errors.New("tag not found")
  68. }
  69. return nil
  70. }
  71. // Validate checks the name and type, and returns an error if they're not valid. It's
  72. // ran by Update and Insert before doing anything
  73. func (tag *Tag) Validate() error {
  74. validType := false
  75. for _, tagType := range TagTypes {
  76. if tagType == tag.Type {
  77. validType = true
  78. break
  79. }
  80. }
  81. if !validType {
  82. return errors.New("invalid tag type")
  83. }
  84. // Validate tag name
  85. if len(tag.Name) == 0 || len(tag.Name) > 64 {
  86. return errors.New("invalid length")
  87. }
  88. return nil
  89. }
  90. // FindTag finds a tag by ID
  91. func FindTag(key string, id string) (*Tag, error) {
  92. db := server.Main.DB
  93. // Make damn sure that – should this take user data as key – it
  94. // does not open up for a SQL injection attack
  95. if key != "name" && key != "id" {
  96. return nil, errors.New("invalid key")
  97. }
  98. rows, err := db.Query("SELECT id,type,name FROM `tag` WHERE "+key+"=? AND disabled=false", id)
  99. if err != nil {
  100. return nil, err
  101. }
  102. defer rows.Close()
  103. if !rows.Next() {
  104. return nil, errors.New("not found")
  105. }
  106. tag := new(Tag)
  107. rows.Scan(&tag.ID, &tag.Type, &tag.Name)
  108. return tag, nil
  109. }
  110. // ListTags finds all the tags, without filter. If it hits
  111. // the tag cache, it will copy it making it safe to modify
  112. func ListTags() ([]Tag, error) {
  113. db := server.Main.DB
  114. rows, err := db.Query("SELECT id,type,name FROM `tag` WHERE disabled=false")
  115. if err != nil {
  116. return nil, err
  117. }
  118. defer rows.Close()
  119. results := make([]Tag, 0, 64)
  120. for rows.Next() {
  121. tag := Tag{}
  122. rows.Scan(&tag.ID, &tag.Type, &tag.Name)
  123. results = append(results, tag)
  124. }
  125. return results, nil
  126. }