helpers.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package helpers
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. "github.com/redis/go-redis/v9"
  7. )
  8. const HEADER_KEY = "header-links"
  9. const MENU_KEY = "menu-config"
  10. const ADMIN_TABLE_KEY = "admin-tables"
  11. const TECHNICAL = "technical"
  12. const CONFIGURATION = "configuration"
  13. const BLOG = "blog"
  14. const CREATIVE = "creative"
  15. const DIGITAL_ART = "digital_art"
  16. var Topics = []string{
  17. TECHNICAL,
  18. BLOG,
  19. CREATIVE,
  20. }
  21. var TopicMap = map[string]string{
  22. TECHNICAL: TECHNICAL,
  23. BLOG: BLOG,
  24. CREATIVE: CREATIVE,
  25. }
  26. type HeaderCollection struct {
  27. Category string `json:"category"`
  28. Elements []HeaderElem `json:"elements"`
  29. }
  30. type HeaderElem struct {
  31. Png string `json:"png"`
  32. Link string `json:"link"`
  33. }
  34. type ImageElement struct {
  35. ImgUrl string `json:"img_url"`
  36. }
  37. type MenuElement struct {
  38. Png string `json:"png"`
  39. Category string `json:"category"`
  40. MenuLinks []MenuLinkPair `json:"menu_links"`
  41. }
  42. type MenuLinkPair struct {
  43. MenuLink string `json:"menu_link"`
  44. LinkText string `json:"link_text"`
  45. }
  46. type Document struct {
  47. Ident string `json:"identifier"`
  48. Created string `json:"created"`
  49. Body string `json:"body"`
  50. Category string `json:"category"`
  51. Sample string
  52. }
  53. type AdminTables struct {
  54. Tables []Table `json:"tables"`
  55. }
  56. type Table struct {
  57. TableName string `json:"table_name"`
  58. TableData []TableData `json:"table_data"`
  59. }
  60. type TableData struct {
  61. DisplayName string `json:"display_name"`
  62. Link string `json:"link"`
  63. }
  64. func NewDocument(ident string, created *time.Time, body string, category string) Document {
  65. var ts time.Time
  66. if created == nil {
  67. rn := time.Now()
  68. ts = time.Date(rn.Year(), rn.Month(), rn.Day(), rn.Hour(), rn.Minute(),
  69. rn.Second(), rn.Nanosecond(), rn.Location())
  70. } else {
  71. ts = *created
  72. }
  73. return Document{Ident: ident, Created: ts.String(), Body: body, Category: category}
  74. }
  75. type DocumentUpload struct {
  76. Name string `json:"name"`
  77. Category string `json:"category"`
  78. Text string `json:"text"`
  79. }
  80. type HeaderIo interface {
  81. GetHeaders() (*HeaderCollection, error)
  82. AddHeaders(HeaderCollection) error
  83. GetMenuLinks() (*MenuElement, error)
  84. }
  85. /*
  86. Retrieves the header data from the redis database
  87. */
  88. func GetHeaders(redisCfg RedisConf) (*HeaderCollection, error) {
  89. rds := NewRedisClient(redisCfg)
  90. d, err := rds.Client.Get(rds.ctx, HEADER_KEY).Result()
  91. if err != nil {
  92. if err == redis.Nil {
  93. return nil, nil
  94. }
  95. return nil, err
  96. }
  97. header := &HeaderCollection{}
  98. err = json.Unmarshal([]byte(d), header); if err != nil {
  99. return nil, err
  100. }
  101. return header, nil
  102. }
  103. /*
  104. Retrieves the menu elements from the database
  105. */
  106. func GetMenuLinks(redisCfg RedisConf) (*MenuElement, error) {
  107. rds := NewRedisClient(redisCfg)
  108. d, err := rds.Client.Get(rds.ctx, MENU_KEY).Result()
  109. if err != nil {
  110. if err == redis.Nil {
  111. return nil, nil
  112. }
  113. return nil, err
  114. }
  115. header := &MenuElement{}
  116. err = json.Unmarshal([]byte(d), header); if err != nil {
  117. return nil, err
  118. }
  119. return header, nil
  120. }
  121. /*
  122. retreives the admin table config from the database
  123. */
  124. func GetAdminTables(redisCfg RedisConf) (*AdminTables, error) {
  125. rds := NewRedisClient(redisCfg)
  126. d, err := rds.Client.Get(rds.ctx, ADMIN_TABLE_KEY).Result()
  127. if err != nil {
  128. if err == redis.Nil {
  129. return nil, nil
  130. }
  131. return nil, err
  132. }
  133. tables := &AdminTables{}
  134. err = json.Unmarshal([]byte(d), tables); if err != nil {
  135. return nil, err
  136. }
  137. return tables, nil
  138. }
  139. /*
  140. Place holder func to create the header element in redis
  141. */
  142. func AddHeaders(h HeaderCollection, redisCfg RedisConf) error {
  143. rdc := NewRedisClient(redisCfg)
  144. data, err := json.Marshal(&h)
  145. if err != nil {
  146. return err
  147. }
  148. err = rdc.Client.Set(rdc.ctx, HEADER_KEY, data, 0).Err()
  149. if err != nil {
  150. return err
  151. }
  152. return nil
  153. }
  154. /*
  155. Truncates a text post into a 256 character long 'sample' for displaying posts
  156. */
  157. func (d *Document) MakeSample() string {
  158. t := strings.Split(d.Body, "")
  159. var sample []string
  160. if len(d.Body) < 256 {
  161. return d.Body
  162. }
  163. for i := 0; i < 256; i++ {
  164. sample = append(sample, t[i])
  165. }
  166. sample = append(sample, " ...")
  167. return strings.Join(sample, "")
  168. }
  169. /*
  170. Retrieve all documents from the category specified in the argument category
  171. :param category: the category to get documents from
  172. */
  173. func GetAllDocuments(category string, redisCfg RedisConf) ([]*Document, error) {
  174. rdc := NewRedisClient(redisCfg)
  175. ids, err := rdc.AllDocIds()
  176. if err != nil {
  177. return nil, err
  178. }
  179. var docs []*Document
  180. for idx := range ids {
  181. doc, err := rdc.GetItem(ids[idx])
  182. if err != nil {
  183. return nil, err
  184. }
  185. if doc.Category != category {
  186. continue
  187. }
  188. docs = append(docs, &Document{
  189. Ident: doc.Ident,
  190. Created: doc.Created,
  191. Body: doc.Body,
  192. Sample: doc.MakeSample(),
  193. })
  194. }
  195. return docs, nil
  196. }
  197. /*
  198. adds a text post document to the redis database
  199. */
  200. func AddDocument(d Document, redisCfg RedisConf) error {
  201. rdc := NewRedisClient(redisCfg)
  202. return rdc.AddDoc(d)
  203. }