This repository was archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstories.go
209 lines (175 loc) · 5.26 KB
/
stories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gocraft/dbr"
"net/http"
)
func getStoriesAPI(sess *dbr.Session, c *gin.Context) {
setCORSHeaders(c)
type Story struct {
ID string
Title string
Subtitle string
Cover string
}
var stories []Story
sess.Select("*").From("stories").Load(&stories)
c.JSON(http.StatusOK, stories)
}
func getStoryAPI(sess *dbr.Session, c *gin.Context, id string) {
setCORSHeaders(c)
type Image struct {
ID string
ImageID string
Title string
Description string
Cover string
Link string
}
type Story struct {
ID string
Title string
Subtitle string
Description string
Cover string
Keywords string `db:"meta_keywords"`
MetaTitle string `db:"meta_title"`
MetaDescription string `db:"meta_description"`
Images []Image
}
var story Story
sess.Select("*").From("stories").Where("id=?", id).Load(&story)
var images []Image
sess.Select("*").From("stories_images").Where("story_id=?", id).OrderBy("sort").Load(&images)
story.Images = images
c.JSON(http.StatusOK, story)
}
func listStories(sess *dbr.Session, c *gin.Context) {
type Story struct {
ID string
Title string
Cover string
}
var stories []Story
sess.Select("*").From("stories").Load(&stories)
c.HTML(http.StatusOK, "stories", gin.H{
"title": "stories",
"stories": stories,
})
}
func getStoryHandler(c *gin.Context) {
checkLogin(c)
c.HTML(http.StatusOK, "story", gin.H{
"title": "my title",
})
}
func editStoryHandler(sess *dbr.Session, c *gin.Context, id string) {
type Image struct {
ID int
ImageID string
Title string
Sort int
Description string
Cover string
Link string
}
type Story struct {
ID string
Title string
Subtitle string
Description string
Cover string
Keywords string `db:"meta_keywords"`
MetaTitle string `db:"meta_title"`
MetaDescription string `db:"meta_description"`
Images []Image
}
var story Story
var images []Image
sess.Select("*").From("stories").Where("id=?", id).Load(&story)
sess.Select("*").From("stories_images").Where("story_id=?", id).OrderBy("sort").Load(&images)
story.Images = images
res, _ := json.Marshal(story)
s := string(res)
fmt.Println(s)
c.HTML(http.StatusOK, "story", gin.H{
"title": "my title",
"json": s,
})
}
func saveStory(sess *dbr.Session, c *gin.Context) {
type Image struct {
New bool `json:"new"`
ID int `json:"id"`
ImageID string `json:"imageId"`
Title string `json:"title"`
Sort int `json:"sort"`
Description string `json:"description"`
Cover string `json:"cover"`
Link string `json:"link"`
Remove bool `json:"remove"`
}
type Story struct {
ID dbr.NullString `json:"id"`
Title string `form:"user" json:"title" binding:"required"`
Subtitle string `json:"subtitle"`
Description string `form:"password" json:"description" binding:"required"`
Cover string `json:"cover"`
Keywords string `json:"keywords"`
MetaTitle string `json:"metaTitle"`
MetaDescription string `json:"metaDescription"`
Images []Image `json:"images"`
}
var json Story
c.BindJSON(&json)
fmt.Println(json)
if json.ID.Valid {
sess.Update("stories").
Set("title", json.Title).
Set("subtitle", json.Subtitle).
Set("cover", json.Cover).
Set("description", json.Description).
Set("meta_keywords", json.Keywords).
Set("meta_title", json.MetaTitle).
Set("meta_description", json.MetaDescription).
Where("id = ?", json.ID.String).Exec()
for _, image := range json.Images {
if image.Remove == true {
sess.DeleteFrom("stories_images").Where("id=?", image.ID).Exec()
} else {
if image.New == false {
sess.Update("stories_images").
Set("image_id", image.ImageID).
Set("sort", image.Sort).
Set("title", image.Title).
Set("description", image.Description).
Set("cover", image.Cover).
Set("link", image.Link).
Where("id=?", image.ID).Exec()
} else {
fmt.Println("saving new story image...")
fmt.Println(image)
_, err := sess.InsertInto("stories_images").
Columns("image_id", "story_id", "sort", "title", "description", "cover", "link").
Values(image.ImageID, json.ID.String, image.Sort, image.Title, image.Description, image.Cover, image.Link).Exec()
fmt.Println(err)
}
}
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
} else {
res, _ := sess.InsertInto("stories").
Columns("title", "subtitle", "cover", "description", "meta_keywords", "meta_title", "meta_description").
Values(json.Title, json.Subtitle, json.Cover, json.Description, json.Keywords, json.MetaTitle, json.MetaDescription).Exec()
id, _ := res.LastInsertId()
for _, image := range json.Images {
sess.InsertInto("stories_images").
Columns("image_id", "story_id", "sort", "title", "description", "cover", "link").
Values(image.ImageID, id, image.Sort, image.Title, image.Description, image.Cover, image.Link).Exec()
fmt.Println(id, image)
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
}