-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (95 loc) · 2.64 KB
/
main.go
File metadata and controls
111 lines (95 loc) · 2.64 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"log"
"net/http"
)
// Структура новости
type new struct {
ID string `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Text string `json:"text" db:"text"`
Date string `json:"time" db:"time"`
}
//var schema = `
//CREATE TABLE news (
// id text,
// title text,
// text text,
// time text
//)`
//Слайс новостей
var news = []new{}
func main() {
//Коннект к БД
db, err := sqlx.Connect("postgres", "host=localhost port=5432 password=123321 user=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Fatalln(err)
}
//Создание БД - используется при первом запуске
//db.MustExec(schema)
//Чтение списка из БД
db.Select(&news, "SELECT * FROM news ORDER BY id ASC")
db.Close()
router := gin.Default()
router.GET("/news", getNews)
router.GET("/news/id/:id", getNewByID)
router.GET("/news/title/:title", getNewByTitle)
router.POST("/news", postNews)
router.Run("localhost:8080")
}
// Список всех новостей
func getNews(c *gin.Context) {
c.IndentedJSON(http.StatusOK, news)
}
// Добавление записи через POST
func postNews(c *gin.Context) {
//Открытие БД
db, err := sqlx.Connect("postgres", "host=localhost port=5432 password=123321 user=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Fatalln(err)
}
//Новая запись
var newNew new
// Перевод из JSON
if err := c.BindJSON(&newNew); err != nil {
c.IndentedJSON(http.StatusBadRequest, newNew)
return
}
// Добавляем запись в слайс
news = append(news, newNew)
// И в БД
_, err = db.Exec("INSERT INTO news (id, title, text, time) VALUES ($1, $2, $3, $4)", newNew.ID, newNew.Title, newNew.Text, newNew.Date)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, newNew)
return
}
db.Close()
c.IndentedJSON(http.StatusCreated, newNew)
}
// Получение записи по ID
func getNewByID(c *gin.Context) {
id := c.Param("id")
// Цикл поиска по ID
for _, a := range news {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
}
// Получение записи по заголовку
func getNewByTitle(c *gin.Context) {
title := c.Param("title")
// Цикл поиска
for _, a := range news {
if a.Title == title {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
}