-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
135 lines (112 loc) · 3.8 KB
/
handlers.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
package main
import (
"log/slog"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type User struct {
ID int `json:"user_id" gorm:"primaryKey;column:user_id"`
Username string `json:"username" gorm:"column:username"`
}
type Memory struct {
ID int `json:"memory_id" gorm:"primaryKey;column:memory_id"`
Title string `json:"title" gorm:"column:title"`
Memory string `json:"memory" gorm:"column:memory"`
Image string `json:"image" gorm:"column:image"`
PhotoOrignURL string `json:"photo_origin_url" gorm:"column:photo_origin_url"`
PhotoURL string `json:"photo_url" gorm:"column:photo_url"`
CreatedBy int `json:"created_by" gorm:"column:created_by"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
}
type Message struct {
ID int `json:"message_id" gorm:"primaryKey;column:message_id"`
ToUser int `json:"to_user" gorm:"column:to_User"`
FromUser int `json:"from_user" gorm:"column:from_User"`
Title string `json:"title" gorm:"column:title"`
Message string `json:"message" gorm:"column:message"`
Image string `json:"image" gorm:"column:image"`
PhotoOrignURL string `json:"photo_origin_url" gorm:"column:photo_origin_url"`
PhotoURL string `json:"photo_url" gorm:"column:photo_url"`
ScheduledTime time.Time `gorm:"column:scheduled_time"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
}
func PostMemory(c *gin.Context) {
var memory Memory
if err := c.ShouldBindJSON(&memory); err != nil {
slog.Error("Message JSON Bind Error", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
db, err := CreateDB()
if err != nil {
slog.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
if memory.Image != "" {
b, err := DecodeBase64(memory.Image)
if err != nil {
slog.Error("Image Decode Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
r, err := PutImage(b)
if err != nil {
slog.Error("S3 Put Image Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
memory.PhotoOrignURL = r.Key
memory.PhotoURL = r.PreSignedURL
}
err = db.Create(&memory).Error
if err != nil {
slog.Error("User Find Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, nil)
}
func PostMessage(c *gin.Context) {
var message Message
if err := c.ShouldBindJSON(&message); err != nil {
slog.Error("Message JSON Bind Error", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
db, err := CreateDB()
if err != nil {
slog.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
if message.Image != "" {
b, err := DecodeBase64(message.Image)
if err != nil {
slog.Error("Image Decode Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
r, err := PutImage(b)
if err != nil {
slog.Error("S3 Put Image Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
message.PhotoOrignURL = r.Key
message.PhotoURL = r.PreSignedURL
}
message.ScheduledTime = time.Now()
err = db.Create(&message).Error
if err != nil {
slog.Error("User Find Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, nil)
}
func GetUsers(c *gin.Context) {
var users []User
db, err := CreateDB()
if err != nil {
slog.Error("DB Connection Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
err = db.Find(&users).Error
if err != nil {
slog.Error("User Find Error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, users)
}