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
Expand file tree
/
Copy pathapi.go
More file actions
146 lines (128 loc) · 3.86 KB
/
Copy pathapi.go
File metadata and controls
146 lines (128 loc) · 3.86 KB
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
package main
import (
"fmt"
"net/http"
"database/sql"
"github.com/gin-gonic/gin"
"github.com/gocraft/dbr"
)
func setCORSHeaders(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
}
func preflightHandler (c *gin.Context) {
setCORSHeaders(c)
c.JSON(http.StatusOK, struct{}{})
}
func getImagesAPI(db *sql.DB, c *gin.Context) {
setCORSHeaders(c)
imageType := c.DefaultQuery("type", "art")
stmt, err := db.Prepare(`
SELECT id, title, description, small_url, big_url, original_url, date, location, original_width, original_height, meta_title, meta_description
FROM images
WHERE type = ?
ORDER BY id DESC
`)
defer stmt.Close()
if err != nil {
fmt.Print(err.Error())
}
rows, err := stmt.Query(imageType)
var (
id string
title string
description string
smallURL string
bigURL string
originalURL string
date string
location string
originalWidth int
originalHeight int
metaTitle string
metaDescription string
)
defer rows.Close()
content := make([]gin.H, 0)
for rows.Next() {
err := rows.Scan(&id, &title, &description, &smallURL, &bigURL, &originalURL, &date, &location, &originalWidth, &originalHeight, &metaTitle, &metaDescription)
if err != nil {
fmt.Print(err.Error())
}
content = append(content, gin.H{
"title": title,
"id": id,
"type": imageType,
"description": description,
"small_url": smallURL,
"big_url": bigURL,
"original_url": originalURL,
"date": date,
"location": location,
"originalWidth": originalWidth,
"originalHeight": originalHeight,
"metaTitle": metaTitle,
"metaDescription": metaDescription,
})
}
c.JSON(http.StatusOK, content)
}
func getImagesAPI2(sess *dbr.Session, c *gin.Context) {
setCORSHeaders(c)
imageType := c.DefaultQuery("type", "art")
type Tag struct {
Name string
}
type Image struct {
Title string
ID string
Type string `db:"type"`
Description dbr.NullString
SmallURL string `db:"small_url"`
BigURL string `db:"big_url"`
OriginalURL string `db:"original_url"`
Date dbr.NullString
Location dbr.NullString
Keywords dbr.NullString
OriginalWidth dbr.NullInt64 `db:"original_width"`
OriginalHeight dbr.NullInt64 `db:"original_height"`
MetaTitle dbr.NullString `db:"meta_title"`
MetaDescription dbr.NullString `db:"meta_description"`
Tags []string
}
var images []Image
imagesWithTags := make([]Image, 0)
sess.Select("*").From("images").Where("type = ?", imageType).OrderDir("id", false).Load(&images)
for _, image := range images {
var tags []Tag
sess.Select("*").From("tags_images").Join("tags", "tags_images.tag_id = tags.id").Where("tags_images.image_id = ?", image.ID).Load(&tags)
plainTags := make([]string, 0)
for _, tag := range tags {
fmt.Println(tag.Name)
plainTags = append(plainTags, tag.Name)
}
image.Tags = plainTags
imagesWithTags = append(imagesWithTags, image)
}
c.JSON(http.StatusOK, imagesWithTags)
}
func getArticlesAPI(sess *dbr.Session, c *gin.Context) {
setCORSHeaders(c)
type Article struct {
ID string
Title dbr.NullString
Subtitle dbr.NullString
Cover dbr.NullString
Country dbr.NullString
City dbr.NullString
Keywords dbr.NullString
MetaTitle dbr.NullString `db:"meta_title"`
MetaDescription dbr.NullString `db:"meta_description"`
Text dbr.NullString
}
var articles []Article
sess.Select("*").From("articles").OrderDir("id", false).Load(&articles)
c.JSON(http.StatusOK, articles)
}