helpers.go 4.9 KB

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