orm.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package orm
  2. import (
  3. "net/http"
  4. "github.com/jinzhu/gorm"
  5. _ "github.com/jinzhu/gorm/dialects/mysql"
  6. )
  7. type IDer interface {
  8. GetID() uint
  9. }
  10. type Timetable [][]Activity
  11. type Person interface {
  12. GetCredential() *Credential
  13. }
  14. type Address struct {
  15. City string
  16. Street string
  17. ZipCode string
  18. Country string
  19. }
  20. type Location struct {
  21. Id string
  22. Name string
  23. Address *Address
  24. }
  25. type Room struct {
  26. Id string
  27. Name string
  28. Capacity int
  29. Location *Location
  30. }
  31. type Desk struct {
  32. Id string
  33. Name string
  34. Students []*Student
  35. Teacher *Teacher
  36. }
  37. type GetFn func(map[string]string, *http.Request) (interface{}, error)
  38. var currDB *gorm.DB
  39. func New(connection string) (*gorm.DB, error) {
  40. db, err := gorm.Open("mysql", connection)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return db, nil
  45. }
  46. func Use(db *gorm.DB) {
  47. currDB = db
  48. }
  49. func DB() *gorm.DB {
  50. return currDB
  51. }
  52. func AutoMigrate() {
  53. if err := currDB.AutoMigrate(
  54. &School{},
  55. &Subject{},
  56. &Teacher{},
  57. &Class{},
  58. &Activity{},
  59. &Department{},
  60. &Student{},
  61. &Office{},
  62. &Administrative{},
  63. &Document{},
  64. &Job{},
  65. &File{},
  66. &GeneratorType{},
  67. &Log{},
  68. &Group{},
  69. ).Error; err != nil {
  70. panic(err)
  71. }
  72. }