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) (interface{}, error)
  38. type PostFn func(map[string]string, *http.Request) (IDer, error)
  39. var currDB *gorm.DB
  40. func New(connection string) (*gorm.DB, error) {
  41. db, err := gorm.Open("mysql", connection)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return db, nil
  46. }
  47. func Use(db *gorm.DB) {
  48. currDB = db
  49. }
  50. func DB() *gorm.DB {
  51. return currDB
  52. }
  53. func AutoMigrate() {
  54. if err := currDB.AutoMigrate(
  55. &School{},
  56. &Subject{},
  57. &Teacher{},
  58. &Class{},
  59. &Activity{},
  60. &Department{},
  61. &Student{},
  62. &Office{},
  63. &Administrative{},
  64. &Document{},
  65. &Job{},
  66. &File{},
  67. &GeneratorType{},
  68. &Log{},
  69. ).Error; err != nil {
  70. panic(err)
  71. }
  72. }