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