-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (106 loc) · 3.01 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
package main
import (
"errors"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"electratype/jailbird/models"
)
var DB *gorm.DB
func MigrateDatabase() {
//DB.Exec("CREATE OR REPLACE FUNCTION get_film_count(len_from int, len_to int)")
DB.AutoMigrate(&models.Organization{}, &models.Project{}, &models.User{}, &models.OrganizationUser{}, &models.ProgressItem{}, &models.ApiKey{}, &models.ProjectUser{})
}
func ListOrganizations(c *gin.Context) {
var organizations []models.Organization
DB.Find(&organizations)
c.JSON(http.StatusOK, &organizations)
}
func AddOrganization(c *gin.Context) {
var plainOrg models.PlainOrganization
var org models.Organization
if err := c.ShouldBindJSON(&plainOrg); err != nil {
c.Error(err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "failure", "error": err.Error()})
}
log.Printf("%+v\n", &plainOrg)
org.Slug = plainOrg.Slug
org.Name = plainOrg.Name
org.Logo = plainOrg.Logo
result := DB.Create(&org)
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
c.Error(result.Error)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "failure", "error": result.Error.Error()})
}
c.JSON(204, "")
}
func ListProjects(c *gin.Context) {
var project []models.Project
DB.Find(&project)
c.JSON(http.StatusOK, &project)
}
func DeleteProject(c *gin.Context) {
id := c.Param("projectId")
DB.Where("slug = ?", id).Delete(&models.Project{})
c.JSON(204, "")
}
func AddProject(c *gin.Context) {
var plainProject models.PlainProject
var project models.Project
if err := c.ShouldBindJSON(&plainProject); err != nil {
c.Error(err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "failure", "error": err.Error()})
}
log.Printf("%+v\n", &plainProject)
project.Slug = plainProject.Slug
project.Name = plainProject.Name
project.Description = plainProject.Description
result := DB.Create(&project)
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
c.Error(result.Error)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "failure", "error": result.Error.Error()})
}
c.JSON(204, "")
}
func ListItems(c *gin.Context) {
}
func main() {
DSN := os.Getenv("DSN")
if DSN == "" {
log.Fatal("DSN not set! Terminating!")
}
log.Println("DSN is set to", DSN)
router := gin.Default()
api := router.Group("/api")
{
v1 := api.Group("/v1")
{
organizations := v1.Group("/organizations")
{
organizations.GET("", ListOrganizations)
organizations.POST("", AddOrganization)
projects := organizations.Group("/projects")
{
projects.GET("", ListProjects)
projects.POST("", AddProject)
projects.DELETE(":projectId", DeleteProject)
items := projects.Group(":projectId/items")
{
items.GET("", ListItems)
}
}
}
}
}
var err error
DB, err = gorm.Open(postgres.Open(DSN), &gorm.Config{TranslateError: true})
if err != nil {
panic("failed to connect database")
}
MigrateDatabase()
router.Run("localhost:5454")
log.Println("done!")
}