-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (109 loc) · 3.63 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
package main
import (
"flag"
"time"
"github.com/gopher1980/gormcrud"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// Author is the one entity
type Author struct {
ID uint `gorm:"primary_key" json:"id" `
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Name string `json:"name"`
}
// Category is the one entity
type Category struct {
ID uint `gorm:"primary_key" json:"id" `
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
CategoryID *uint `json:"category_id" sql:"type:integer REFERENCES category(id)"`
Categories []Category `json:"categories"`
Notes []Note `json:"notes"`
}
//CrudValidateSave is Validate
func (elem Category) CrudValidateSave(db *gorm.DB) error {
if elem.CategoryID == nil {
return gormcrud.ErrorCrud{Message: "CategoryID can't not be null ", Code: 500}
}
return nil
}
// CrudValidateDelete is Validate
func (elem Category) CrudValidateDelete(db *gorm.DB) error {
if elem.CategoryID == nil {
return gormcrud.ErrorCrud{Message: "CategoryID can't not delete root category ", Code: 500}
}
return nil
}
// Tag is the one entity
type Tag struct {
ID uint `gorm:"primary_key" json:"id" `
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Name string `json:"title"`
Notes []Note `json:"notes" gorm:"many2many:tag_note;"`
}
// Note is the one entity
type Note struct {
ID uint `gorm:"primary_key" json:"id" `
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
Description string `json:"description"`
Text string `json:"text"`
Version string `json:"version"`
AuthorID uint `json:"author_id" sql:"type:integer REFERENCES author(id)"`
CategoryID uint `json:"category_id" sql:"type:integer REFERENCES category(id)"`
Tags []Tag `json:"tags" gorm:"many2many:tag_note;"`
}
// TagNote is not the one entity, it is the relation many to many. I map for gorm makeing REFERENCES key
type TagNote struct {
TagID uint `sql:"type:integer REFERENCES tag(id)"`
NoteID uint `sql:"type:integer REFERENCES note(id)"`
}
var (
db *gorm.DB
)
func main() {
var uridb string
var drivedb string
var addr string
var debug bool
flag.StringVar(&drivedb, "drive", "sqlite3", "[sqlite3, mysql, postgres ]")
flag.StringVar(&uridb, "db", "test.db", "Uri of db")
flag.StringVar(&addr, "addr", ":9090", "this is addr of this ListenAndServe")
flag.BoolVar(&debug, "debug", false, "sql debug")
flag.Parse()
var err error
db, err = gorm.Open(drivedb, uridb)
if err != nil {
panic("failed to connect database")
}
defer db.Close()
db.SingularTable(true)
db.AutoMigrate(&Author{})
db.AutoMigrate(&Category{})
db.AutoMigrate(&TagNote{})
db.AutoMigrate(&Tag{})
db.AutoMigrate(&Note{})
if drivedb == "sqlite3" {
db.Exec("PRAGMA foreign_keys = ON;")
}
db.Save(Category{ID: 1, Title: "root", CategoryID: nil})
r := gin.Default()
gormcrud.MapGin(r, db).
NewMap("/api/v1/author", Author{}, []Author{}).Full().
NewMap("/api/v1/category", Category{}, []Category{}).Full().
NewMap("/api/v1/tag", Tag{}, []Tag{}).Full().
NewMap("/api/v1/note", Note{}, []Note{}).Full()
r.Run(addr)
}