-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (165 loc) · 5.94 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
type CompanyInfo struct {
ID int `json:"id"`
CompanyName string `json:"company_name"`
HeadquartersLocation string `json:"headquarters_location"`
Industry string `json:"industry"`
Welfare string `json:"welfare"`
RecruitmentMethod string `json:"recruitment_method"`
Requirements string `json:"requirements"`
Website string `json:"website"`
ImageURL string `json:"image_url"`
}
type Schedule struct {
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
ScheduleDate string `json:"schedule_date"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
ImgURL string `json:"img_url"`
}
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
ImgURL string `json:"img_url"`
}
var db *sql.DB
func main() {
var err error
dsn := "admin:pwd@tcp(gitmate-database.cbimo8eqih5y.ap-northeast-2.rds.amazonaws.com:3306)/gitmate_db?charset=utf8"
db, err = sql.Open("mysql", dsn)
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
err = db.Ping()
if err != nil {
log.Fatalf("Error pinging database: %v", err)
}
fmt.Println("Database connection established")
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
router.GET("/company_info", getCompanyInfo)
router.POST("/schedules", addSchedule)
router.GET("/schedules", getSchedules)
router.POST("/posts", addPost)
router.Run(":8080")
}
func getCompanyInfo(c *gin.Context) {
pageStr := c.DefaultQuery("page", "1")
limitStr := c.DefaultQuery("limit", "20")
page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid page number"})
return
}
limit, err := strconv.Atoi(limitStr)
if err != nil || limit < 1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit number"})
return
}
offset := (page - 1) * limit
query := fmt.Sprintf("SELECT id, company_name, headquarters_location, industry, welfare, recruitment_method, requirements, image_url, website FROM company_info LIMIT %d OFFSET %d", limit, offset)
rows, err := db.Query(query)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer rows.Close()
var companyInfos []CompanyInfo
for rows.Next() {
var companyInfo CompanyInfo
if err := rows.Scan(&companyInfo.ID, &companyInfo.CompanyName, &companyInfo.HeadquartersLocation, &companyInfo.Industry, &companyInfo.Welfare, &companyInfo.RecruitmentMethod, &companyInfo.Requirements, &companyInfo.ImageURL, &companyInfo.Website); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
companyInfos = append(companyInfos, companyInfo)
}
c.JSON(http.StatusOK, companyInfos)
}
func addSchedule(c *gin.Context) {
var newSchedule Schedule
if err := c.ShouldBindJSON(&newSchedule); err != nil {
log.Printf("JSON binding error: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
query := `INSERT INTO schedules (title, description, schedule_date, start_time, end_time, img_url) VALUES (?, ?, ?, ?, ?, ?)`
result, err := db.Exec(query, newSchedule.Title, newSchedule.Description, newSchedule.ScheduleDate, newSchedule.StartTime, newSchedule.EndTime, newSchedule.ImgURL)
if err != nil {
log.Printf("Database insert error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
id, err := result.LastInsertId()
if err != nil {
log.Printf("Getting last insert ID error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
log.Printf("Schedule added successfully with ID: %d", id)
c.JSON(http.StatusOK, gin.H{"message": "Schedule added successfully", "id": id})
}
func getSchedules(c *gin.Context) {
query := "SELECT id, title, description, schedule_date, COALESCE(start_time, ''), COALESCE(end_time, ''), COALESCE(img_url, '') FROM schedules"
rows, err := db.Query(query)
if err != nil {
log.Printf("Database query error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer rows.Close()
var schedules []Schedule
for rows.Next() {
var schedule Schedule
if err := rows.Scan(&schedule.ID, &schedule.Title, &schedule.Description, &schedule.ScheduleDate, &schedule.StartTime, &schedule.EndTime, &schedule.ImgURL); err != nil {
log.Printf("Row scan error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
schedules = append(schedules, schedule)
}
c.JSON(http.StatusOK, schedules)
}
func addPost(c *gin.Context) {
var newPost Post
if err := c.ShouldBindJSON(&newPost); err != nil {
log.Printf("JSON binding error: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
query := `INSERT INTO posts (title, description, img_url) VALUES (?, ?, ?)`
result, err := db.Exec(query, newPost.Title, newPost.Description, newPost.ImgURL)
if err != nil {
log.Printf("Database insert error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
id, err := result.LastInsertId()
if err != nil {
log.Printf("Getting last insert ID error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
log.Printf("Post added successfully with ID: %d", id)
c.JSON(http.StatusOK, gin.H{"message": "Post added successfully", "id": id})
}