|
|
- package database
-
- import (
- "fmt"
-
- "git.tovijaeschke.xyz/tovi/JumboPetstore/models"
- "git.tovijaeschke.xyz/tovi/JumboPetstore/util"
- "gorm.io/gorm/clause"
- )
-
- func CreatePet(petData models.Pet) (models.Pet, error) {
- var (
- photoUrl string
- fileName string
- err error
- )
-
- err = DB.Omit("PhotoUrlJson").Create(&petData).Error
- if err != nil {
- return petData, err
- }
-
- for _, photoUrl = range petData.PhotoUrlJson {
- fileName, err = util.DownloadFile(photoUrl)
- if err != nil {
- return petData, err
- }
-
- petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
- PetId: petData.Id,
- FileName: fileName,
- })
- }
-
- petData, err = UpdatePet(petData)
- if err != nil {
- return petData, err
- }
-
- return petData, err
- }
-
- func UpdatePet(petData models.Pet) (models.Pet, error) {
- var (
- photoUrl string
- err error
- )
-
- err = DB.Model(&models.Pet{}).
- Where("id = ?", petData.Id).
- Omit("PhotoUrlJson").
- Updates(&petData).
- Error
- if err != nil {
- return petData, err
- }
-
- // Delete existing pet photos
- err = DB.Where("pet_id = ?", petData.ID).
- Delete(&models.PetPhoto{}).
- Error
- if err != nil {
- return petData, err
- }
-
- // Update pet photos from PUT data
- for _, photoUrl = range petData.PhotoUrlJson {
- err = DB.Create(&models.PetPhoto{
- PetId: petData.Id,
- Url: photoUrl,
- }).Error
- if err != nil {
- return petData, err
- }
- }
-
- return petData, err
- }
-
- func GetPetById(id int) models.Pet {
- var (
- petData models.Pet
- petPhoto models.PetPhoto
- photoUrls []string
- )
-
- DB.Preload(clause.Associations).First(&petData, "id = ?", id)
-
- for _, petPhoto = range petData.PhotoUrls {
- photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
- }
-
- petData.PhotoUrlJson = photoUrls
-
- fmt.Println(petData)
-
- return petData
- }
-
- func DeletePet(petData models.Pet) {
- DB.Where("pet_id = ?", petData.Id).Delete(&petData.Categories)
- DB.Where("pet_id = ?", petData.Id).Delete(&petData.Tags)
- DB.Where("pet_id = ?", petData.Id).Delete(&petData.PhotoUrls)
- DB.Delete(&petData)
- }
-
- func GetPetsByStatus(status string) ([]models.Pet, error) {
- var (
- petDatas []models.Pet
- petData models.Pet
- petPhoto models.PetPhoto
- photoUrls []string
- i int
- err error
- )
-
- err = DB.Where("status = ?", status).Find(&petDatas).Error
- if err != nil {
- return petDatas, err
- }
-
- for i, petData = range petDatas {
- err = DB.Where("pet_id = ?", petData.Id).Find(&petDatas[i].Tags).Error
- if err != nil {
- return petDatas, err
- }
-
- err = DB.Where("pet_id = ?", petData.Id).Find(&petDatas[i].PhotoUrls).Error
- if err != nil {
- return petDatas, err
- }
-
- for _, petPhoto = range petDatas[i].PhotoUrls {
- photoUrls = append(photoUrls, "/images/"+petPhoto.FileName)
- }
-
- petDatas[i].PhotoUrlJson = photoUrls
- }
-
- return petDatas, err
- }
-
- func AddPhotoToPet(id int, fileName string) error {
- var (
- petData models.Pet
- err error
- )
-
- petData = GetPetById(id)
-
- petData.PhotoUrls = append(petData.PhotoUrls, models.PetPhoto{
- PetId: id,
- FileName: fileName,
- })
- _, err = UpdatePet(petData)
-
- return err
- }
|