helpers.go 4.8 KB

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