Advertisement
Guest User

Untitled

a guest
May 19th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. _ "github.com/jinzhu/gorm/dialects/postgres"
  8. )
  9.  
  10. type Brand struct {
  11. gorm.Model
  12. Name string
  13. Description string
  14. Image int
  15. CreationYear int
  16. EndYear int
  17. Country string
  18. }
  19. type Bike struct {
  20. // add basic ID/Created@/Updated@/Delete@ through Gorm
  21. gorm.Model
  22. Name string
  23. Brand Brand
  24. BrandID int `json:"-"`
  25. Year string
  26. Description string
  27. Images []Image `gorm:"many2many:bike_images;"`
  28. }
  29. type Image struct {
  30. gorm.Model
  31. Name string
  32. Path string
  33. Type string
  34. }
  35.  
  36. var (
  37. DB *gorm.DB
  38. )
  39.  
  40. func initDB() (DB *gorm.DB) {
  41.  
  42. DB, err := gorm.Open("postgres", fmt.Sprintf(
  43. "user=%s password='%s' host=%s dbname=%s",
  44. "test",
  45. "test",
  46. "//var/run/postgresql",
  47. "test"))
  48. if err != nil {
  49. fmt.Println("error:", err)
  50. }
  51. // Debug Mode
  52. DB.LogMode(true)
  53. DB.DropTable(&Image{}, &Brand{}, &Bike{})
  54. DB.CreateTable(&Image{}, &Brand{}, &Bike{})
  55. DB.Model(&Bike{}).AddUniqueIndex("bike_uniqueness", "name, year, brand_id")
  56. DB.Model(&Image{}).AddUniqueIndex("image_uniqueness", "name", "path")
  57. DB.AutoMigrate(&Bike{}, &Image{}, &Brand{})
  58. return
  59.  
  60. }
  61.  
  62. func (img *Image) Save() {
  63.  
  64. if DB.NewRecord(img) {
  65. oldi := new(Image)
  66. DB.Where("name = ?", img.Name).First(oldi)
  67. if oldi.ID == 0 && oldi.Name == "" {
  68. fmt.Println("Recording the New Image")
  69. DB.Create(img)
  70. } else {
  71. fmt.Println("Updating The Image Record")
  72. DB.Model(oldi).Updates(img)
  73. img = oldi
  74. }
  75. } else {
  76. fmt.Printf("Saving Image %#v\n", *img)
  77. DB.Save(img)
  78. }
  79. fmt.Printf("Saved Image %#v\n", *img)
  80. }
  81.  
  82. func main() {
  83. DB = initDB()
  84. // We receive a JSON with No ID (Some of the element already Exists)
  85. var inputImage = []byte(`
  86. {
  87. "Name":"34FDF234.jpg",
  88. "Path":"uploads/34FDF234.jpg"
  89. }
  90. `)
  91. var inputBike = []byte(`
  92. {
  93. "Name":" Da Bike",
  94. "Description":"SUper Description ",
  95. "Brand":{
  96. "Name":"Trek"
  97. },
  98. "Images":[
  99. {
  100. "Name":"34FDF234.jpg",
  101. "Path":"uploads/34FDF234.jpg"
  102. },
  103. {
  104. "Name":"8404VRVRG01.jpg",
  105. "Path":"uploads/8404VRVRG01.jpg"
  106. }
  107. ]
  108. }`)
  109. // Unmarshal Image
  110. var image Image
  111. err := json.Unmarshal(inputImage, &image)
  112. if err != nil {
  113. fmt.Println("error:", err)
  114. }
  115. // Unmarshal Bike
  116. var bike Bike
  117. err = json.Unmarshal(inputBike, &bike)
  118. if err != nil {
  119. fmt.Println("error:", err)
  120. }
  121. fmt.Printf("Will Create the Image Record %v\n", image)
  122.  
  123. DB.Create(&image)
  124. fmt.Printf("Image Created within the Database %v\n", image)
  125. // Create the Bike Record
  126. for i := range bike.Images {
  127. bike.Images[i].Save()
  128. }
  129. fmt.Printf("Will Create the Bike Record %#v\n", bike)
  130.  
  131. DB.Set("gorm:save_associations", false).Create(&bike)
  132. DB.Save(&bike)
  133. fmt.Printf("Bike Saved %v\n", bike)
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement