You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package database
  2. import (
  3. "fmt"
  4. "git.tovijaeschke.xyz/tovi/JumboPetstore/models"
  5. "git.tovijaeschke.xyz/tovi/JumboPetstore/util"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/clause"
  8. )
  9. func CreatePet(petData *models.Pet) error {
  10. var (
  11. photoUrls []string
  12. photoUrl string
  13. fileName string
  14. err error
  15. )
  16. for _, photoUrl = range petData.PhotoUrlJson {
  17. fileName, err = util.DownloadFile(photoUrl)
  18. if err != nil {
  19. return err
  20. }
  21. petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
  22. PetId: petData.Id,
  23. FileName: fileName,
  24. })
  25. photoUrls = append(photoUrls, fmt.Sprintf(
  26. "/images/%s",
  27. fileName,
  28. ))
  29. }
  30. petData.PhotoUrlJson = photoUrls
  31. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  32. Omit("PhotoUrlJson").
  33. Create(petData).
  34. Error
  35. if err != nil {
  36. return err
  37. }
  38. return err
  39. }
  40. func UpdatePet(petData *models.Pet) error {
  41. var (
  42. photoUrls []string
  43. photoUrl string
  44. fileName string
  45. err error
  46. )
  47. for _, photoUrl = range petData.PhotoUrlJson {
  48. fileName, err = util.DownloadFile(photoUrl)
  49. if err != nil {
  50. return err
  51. }
  52. petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
  53. PetId: petData.Id,
  54. FileName: fileName,
  55. })
  56. photoUrls = append(photoUrls, fmt.Sprintf(
  57. "/images/%s",
  58. fileName,
  59. ))
  60. }
  61. petData.PhotoUrlJson = photoUrls
  62. // This leaves some orphaned files in ./uploads
  63. DB.Model(petData).Association("PhotoUrls").Replace(petData.PhotoUrls)
  64. DB.Model(petData).Association("Tags").Replace(petData.Tags)
  65. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  66. Omit("PhotoUrlJson").
  67. Updates(petData).
  68. Error
  69. if err != nil {
  70. return err
  71. }
  72. return err
  73. }
  74. func GetPetById(id int) models.Pet {
  75. var (
  76. petData models.Pet
  77. petPhoto models.PetPhoto
  78. photoUrls []string
  79. )
  80. DB.Preload(clause.Associations).First(&petData, "id = ?", id)
  81. for _, petPhoto = range petData.PhotoUrls {
  82. photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
  83. }
  84. petData.PhotoUrlJson = photoUrls
  85. return petData
  86. }
  87. func DeletePet(petData models.Pet) {
  88. DB.Preload(clause.Associations).Delete(&petData)
  89. }
  90. func GetPetsByStatus(status string) ([]models.Pet, error) {
  91. var (
  92. petDatas []models.Pet
  93. petPhoto models.PetPhoto
  94. photoUrls []string
  95. i int
  96. err error
  97. )
  98. err = DB.Preload(clause.Associations).
  99. Where("status = ?", status).
  100. Find(&petDatas).
  101. Error
  102. if err != nil {
  103. return petDatas, err
  104. }
  105. for i = range petDatas {
  106. photoUrls = []string{}
  107. for _, petPhoto = range petDatas[i].PhotoUrls {
  108. photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
  109. }
  110. petDatas[i].PhotoUrlJson = photoUrls
  111. }
  112. return petDatas, err
  113. }
  114. func AddPhotoToPet(petData *models.Pet, fileName string, additionalMetadata string, fileBytes []byte) (string, error) {
  115. var (
  116. err error
  117. )
  118. fileName, err = util.WriteFile(fileName, fileBytes)
  119. petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
  120. PetId: petData.Id,
  121. FileName: fileName,
  122. AdditionalMetadata: additionalMetadata,
  123. })
  124. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  125. Omit("PhotoUrlJson").
  126. Updates(&petData).
  127. Error
  128. return fileName, err
  129. }