-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPosts_Model.go
53 lines (48 loc) · 1.18 KB
/
Posts_Model.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
package main
import (
"encoding/json"
"errors"
)
type Post struct {
Id string `json:"idPosts"`
Title string `json:"title"`
Content string `json:"content"`
ImagesUrl []string `json:"images"`
PostDate string `json:"date"`
}
func (post *Post) Create() (int64, error){
query := "INSERT IGNORE posts (Title, Content, Images) VALUES (?,?,?)"
imagesJson , err := json.Marshal(post.ImagesUrl)
rs, err := SqlDB.Exec(query, post.Title, post.Content,string(imagesJson))
return HandleSQLResponse(rs, err)
}
func ViewPost(id string) (Post,error) {
query := "SELECT idPosts,Title,Content,Images,PostDate from posts where idPosts = ?"
rows,err:=SqlDB.Query(query, id)
var post Post
if err != nil {
return post, err
}
idPost := ""
title := ""
content := ""
images := ""
postDate := ""
for rows.Next() {
err = rows.Scan(&idPost,&title,&content,&images,&postDate)
if err != nil {
return post, err
}
break
}
if idPost == "" {
rows.Close()
return post, errors.New("post does not exist")
}
post.Id = idPost
post.Title = title
post.Content = content
_ = json.Unmarshal([]byte(images), &post.ImagesUrl)
post.PostDate = postDate
return post,rows.Close()
}