github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/manage/topic/topic.go (about) 1 package managetopic 2 3 import ( 4 "net/http" 5 "strconv" 6 7 "github.com/gosimple/slug" 8 "github.com/ngocphuongnb/tetua/app/cache" 9 "github.com/ngocphuongnb/tetua/app/entities" 10 "github.com/ngocphuongnb/tetua/app/repositories" 11 "github.com/ngocphuongnb/tetua/app/server" 12 "github.com/ngocphuongnb/tetua/app/utils" 13 "github.com/ngocphuongnb/tetua/views" 14 ) 15 16 func Index(c server.Context) (err error) { 17 status := http.StatusOK 18 topics, err := repositories.Topic.All(c.Context()) 19 c.Meta().Title = "Manage topics" 20 21 if err != nil { 22 status = http.StatusBadRequest 23 c.WithError("Load all topics error", err) 24 } 25 26 return c.Status(status).Render(views.ManageTopicIndex(entities.PrintTopicsTree(topics, []int{}))) 27 } 28 29 func Compose(c server.Context) (err error) { 30 return getTopicComposeView(c, &entities.TopicMutation{}, false) 31 } 32 33 func Save(c server.Context) (err error) { 34 var topic *entities.Topic 35 composeData := getTopicSaveData(c) 36 37 if topic, err = getProcessingTopic(c); err != nil { 38 c.WithError("Query editting topic error", err) 39 } 40 41 if c.Messages().Length() > 0 { 42 return getTopicComposeView(c, composeData, true) 43 } 44 45 topic.Name = composeData.Name 46 topic.Content = composeData.Content 47 topic.ParentID = composeData.ParentID 48 topic.ContentHTML = composeData.ContentHTML 49 50 if topic.ID > 0 { 51 topic, err = repositories.Topic.Update(c.Context(), topic) 52 } else { 53 topic.Slug = slug.Make(composeData.Name) 54 topic, err = repositories.Topic.Create(c.Context(), topic) 55 } 56 57 if err != nil { 58 c.WithError("Error saving topic", err) 59 return getTopicComposeView(c, composeData, true) 60 } 61 62 if err := cache.CacheTopics(c.Context()); err != nil { 63 c.WithError("Error caching topics", err) 64 return getTopicComposeView(c, composeData, true) 65 } 66 67 return c.Redirect("/manage/topics/" + strconv.Itoa(topic.ID)) 68 } 69 70 func Delete(c server.Context) error { 71 topic, err := getProcessingTopic(c) 72 73 if err != nil { 74 c.Logger().Error("Error deleting topic", err) 75 return c.Status(http.StatusBadRequest).SendString("Error deleting topic") 76 } 77 78 if err := repositories.Topic.DeleteByID(c.Context(), topic.ID); err != nil { 79 c.Logger().Error("Error deleting topic", err) 80 return c.Status(http.StatusBadRequest).SendString("Error deleting topic") 81 } 82 83 return c.Status(http.StatusOK).SendString("Topic deleted") 84 } 85 86 func getProcessingTopic(c server.Context) (topic *entities.Topic, err error) { 87 if c.Param("id") == "new" { 88 return &entities.Topic{}, nil 89 } 90 return repositories.Topic.ByID(c.Context(), c.ParamInt("id")) 91 } 92 93 func getTopicComposeView(c server.Context, data *entities.TopicMutation, isSave bool) (err error) { 94 var topics []*entities.Topic 95 ignore := make([]int, 0) 96 topic, err := getProcessingTopic(c) 97 c.Meta().Title = "Create Topic" 98 99 if err != nil { 100 c.WithError("Query editting topic error", err) 101 } else if !isSave { 102 data.ID = topic.ID 103 data.Name = topic.Name 104 data.Content = topic.Content 105 data.ParentID = topic.ParentID 106 } 107 108 if topics, err = repositories.Topic.All(c.Context()); err != nil { 109 c.WithError("Load all topics error", err) 110 } 111 112 if topic.ID > 0 { 113 c.Meta().Title = "Edit Topic: " + topic.Name 114 ignore = append(ignore, topic.ID) 115 } 116 117 return c.Render(views.ManageTopicCompose(entities.PrintTopicsTree(topics, ignore), data)) 118 } 119 120 func getTopicSaveData(c server.Context) *entities.TopicMutation { 121 var err error 122 data := &entities.TopicMutation{} 123 if err = c.BodyParser(data); err != nil { 124 c.WithError("Error parsing body", err) 125 126 return data 127 } 128 129 data.Name = utils.SanitizePlainText(data.Name) 130 data.Content = utils.SanitizeMarkdown(data.Content) 131 132 if data.Name == "" || len(data.Name) > 250 { 133 c.Messages().AppendError("Name is required and can't be more than 250 characters") 134 } 135 136 if data.ContentHTML, err = utils.MarkdownToHtml(data.Content); err != nil { 137 c.WithError("Error convert markdown to html", err) 138 } 139 140 return data 141 }