123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package orm
- import (
- "fmt"
- "net/http"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql"
- )
- type Timetable [][]Activity
- type Person interface {
- GetCredential() *Credential
- }
- type Address struct {
- City string
- Street string
- ZipCode string
- Country string
- }
- type Location struct {
- Id string
- Name string
- Address *Address
- }
- type Room struct {
- Id string
- Name string
- Capacity int
- Location *Location
- }
- type Desk struct {
- Id string
- Name string
- Students []*Student
- Teacher *Teacher
- }
- type Student struct {
- gorm.Model
- Credential
- Class *Class
- }
- type Department struct {
- gorm.Model
- Name string
- Subjects []Subject
- Teachers []Teacher
- }
- type GetFn func(map[string]string) (interface{}, error)
- type PostFn func(map[string]string, *http.Request) (IDer, error)
- var currDB *gorm.DB
- func New(connection string) (*gorm.DB, error) {
- db, err := gorm.Open("mysql", connection)
- if err != nil {
- return nil, err
- }
- return db, nil
- }
- func Use(db *gorm.DB) {
- currDB = db
- }
- func DB() *gorm.DB {
- return currDB
- }
- func AutoMigrate() {
- if err := currDB.AutoMigrate(
- &School{},
- &Subject{},
- &Teacher{},
- &Class{},
- &Activity{},
- &Department{},
- &Student{},
- ).Error; err != nil {
- panic(err)
- }
- }
- func GetFunc(path string) (GetFn, error) {
- fn, ok := Get[path]
- if !ok {
- return nil, fmt.Errorf("Can't map %s!", path)
- }
- return fn, nil
- }
- func PostFunc(path string) (PostFn, error) {
- fn, ok := Post[path]
- if !ok {
- return nil, fmt.Errorf("Can't map %s!", path)
- }
- return fn, nil
- }
|