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.

158 lines
3.0 KiB

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/clause"
  7. )
  8. func CreatePet(petData models.Pet) (models.Pet, error) {
  9. var (
  10. photoUrl string
  11. fileName string
  12. err error
  13. )
  14. err = DB.Omit("PhotoUrlJson").Create(&petData).Error
  15. if err != nil {
  16. return petData, err
  17. }
  18. for _, photoUrl = range petData.PhotoUrlJson {
  19. fileName, err = util.DownloadFile(photoUrl)
  20. if err != nil {
  21. return petData, err
  22. }
  23. petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
  24. PetId: petData.Id,
  25. FileName: fileName,
  26. })
  27. }
  28. petData, err = UpdatePet(petData)
  29. if err != nil {
  30. return petData, err
  31. }
  32. return petData, err
  33. }
  34. func UpdatePet(petData models.Pet) (models.Pet, error) {
  35. var (
  36. photoUrl string
  37. err error
  38. )
  39. err = DB.Model(&models.Pet{}).
  40. Where("id = ?", petData.Id).
  41. Omit("PhotoUrlJson").
  42. Updates(&petData).
  43. Error
  44. if err != nil {
  45. return petData, err
  46. }
  47. // Delete existing pet photos
  48. err = DB.Where("pet_id = ?", petData.ID).
  49. Delete(&models.PetPhoto{}).
  50. Error
  51. if err != nil {
  52. return petData, err
  53. }
  54. // Update pet photos from PUT data
  55. for _, photoUrl = range petData.PhotoUrlJson {
  56. err = DB.Create(&models.PetPhoto{
  57. PetId: petData.Id,
  58. Url: photoUrl,
  59. }).Error
  60. if err != nil {
  61. return petData, err
  62. }
  63. }
  64. return petData, err
  65. }
  66. func GetPetById(id int) models.Pet {
  67. var (
  68. petData models.Pet
  69. petPhoto models.PetPhoto
  70. photoUrls []string
  71. )
  72. DB.Preload(clause.Associations).First(&petData, "id = ?", id)
  73. for _, petPhoto = range petData.PhotoUrls {
  74. photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
  75. }
  76. petData.PhotoUrlJson = photoUrls
  77. fmt.Println(petData)
  78. return petData
  79. }
  80. func DeletePet(petData models.Pet) {
  81. DB.Where("pet_id = ?", petData.Id).Delete(&petData.Categories)
  82. DB.Where("pet_id = ?", petData.Id).Delete(&petData.Tags)
  83. DB.Where("pet_id = ?", petData.Id).Delete(&petData.PhotoUrls)
  84. DB.Delete(&petData)
  85. }
  86. func GetPetsByStatus(status string) ([]models.Pet, error) {
  87. var (
  88. petDatas []models.Pet
  89. petData models.Pet
  90. petPhoto models.PetPhoto
  91. photoUrls []string
  92. i int
  93. err error
  94. )
  95. err = DB.Where("status = ?", status).Find(&petDatas).Error
  96. if err != nil {
  97. return petDatas, err
  98. }
  99. for i, petData = range petDatas {
  100. err = DB.Where("pet_id = ?", petData.Id).Find(&petDatas[i].Tags).Error
  101. if err != nil {
  102. return petDatas, err
  103. }
  104. err = DB.Where("pet_id = ?", petData.Id).Find(&petDatas[i].PhotoUrls).Error
  105. if err != nil {
  106. return petDatas, err
  107. }
  108. for _, petPhoto = range petDatas[i].PhotoUrls {
  109. photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
  110. }
  111. petDatas[i].PhotoUrlJson = photoUrls
  112. }
  113. return petDatas, err
  114. }
  115. func AddPhotoToPet(id int, fileName string) error {
  116. var (
  117. petData models.Pet
  118. err error
  119. )
  120. petData = GetPetById(id)
  121. petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
  122. PetId: id,
  123. FileName: fileName,
  124. })
  125. _, err = UpdatePet(petData)
  126. return err
  127. }