compute recursive path
This commit is contained in:
parent
1f87df43a8
commit
a4483f446d
@ -1,13 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.celogeek.com/photos/api/internal/photos/models"
|
||||
"gopkg.in/validator.v2"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
@ -28,27 +26,20 @@ func (s *Service) AlbumCreate(c *gin.Context) {
|
||||
|
||||
sess := s.CurrentSession(c)
|
||||
|
||||
var parentAlbum *models.Album
|
||||
|
||||
if req.Parent != nil {
|
||||
parentAlbum = &models.Album{}
|
||||
if err := s.
|
||||
DB.
|
||||
Debug().
|
||||
Preload("Author").
|
||||
Find(parentAlbum, req.Parent).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Error(c, http.StatusNotFound, ErrAlbumDontExists)
|
||||
} else {
|
||||
s.Error(c, http.StatusInternalServerError, err)
|
||||
}
|
||||
var parentExists int64
|
||||
if err := s.DB.Model(&models.Album{}).Where("id = ?", req.Parent).Count(&parentExists).Error; err != nil {
|
||||
s.Error(c, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if parentExists == 0 {
|
||||
s.Error(c, http.StatusNotFound, ErrAlbumDontExists)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
album := &models.Album{
|
||||
Name: req.Name,
|
||||
Parent: parentAlbum,
|
||||
ParentId: req.Parent,
|
||||
Author: sess.Account,
|
||||
AuthorId: &sess.Account.ID,
|
||||
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@ -9,8 +10,9 @@ import (
|
||||
type Album struct {
|
||||
ID uint32 `gorm:"primary_key" json:"id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Parent *Album `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"parent"`
|
||||
ParentId *uint32 `json:"-"`
|
||||
Fullname string `gorm:"-" json:"fullname"`
|
||||
Parent *Album `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"-"`
|
||||
ParentId *uint32 `json:"parent_id"`
|
||||
Author *Account `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"author"`
|
||||
AuthorId *uint32 `json:"-"`
|
||||
Photos []*Photo `gorm:"many2many:album_photos" json:"-"`
|
||||
@ -19,9 +21,24 @@ type Album struct {
|
||||
}
|
||||
|
||||
func (a *Album) AfterFind(tx *gorm.DB) error {
|
||||
if a.ParentId != nil {
|
||||
a.Parent = &Album{}
|
||||
return tx.Preload("Author").First(a.Parent, a.ParentId).Error
|
||||
a.ComputeFullpath(tx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Album) AfterCreate(tx *gorm.DB) error {
|
||||
a.ComputeFullpath(tx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Album) ComputeFullpath(tx *gorm.DB) error {
|
||||
if a.ParentId == nil {
|
||||
a.Fullname = a.Name
|
||||
} else {
|
||||
parent := &Album{}
|
||||
if err := tx.Session(&gorm.Session{NewDB: true}).Select("name", "parent_id").First(parent, "id = ?", a.ParentId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
a.Fullname = filepath.Join(parent.Fullname, a.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user