create album with recursive display

This commit is contained in:
celogeek 2022-02-06 15:54:12 +01:00
parent 350c848ed9
commit 1f87df43a8
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 59 additions and 21 deletions

View File

@ -1,11 +1,14 @@
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"
) )
type AlbumCreateRequest struct { type AlbumCreateRequest struct {
@ -25,13 +28,33 @@ func (s *Service) AlbumCreate(c *gin.Context) {
sess := s.CurrentSession(c) sess := s.CurrentSession(c)
album := &models.Album{ var parentAlbum *models.Album
Name: req.Name,
ParentId: req.Parent, if req.Parent != nil {
AuthorId: &sess.AccountId, 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)
}
return
}
} }
if err := s.DB.Create(album).Error; err != nil { album := &models.Album{
Name: req.Name,
Parent: parentAlbum,
ParentId: req.Parent,
Author: sess.Account,
AuthorId: &sess.Account.ID,
}
if err := s.DB.Debug().Omit(clause.Associations).Clauses(clause.Returning{}).Create(album).Error; err != nil {
s.Error(c, http.StatusConflict, err) s.Error(c, http.StatusConflict, err)
return return
} }

View File

@ -22,6 +22,9 @@ var (
// Panic // Panic
ErrUnexpected = errors.New("an unexpected error occur") ErrUnexpected = errors.New("an unexpected error occur")
// Album
ErrAlbumDontExists = errors.New("album doesn't exists")
) )
func (s *Service) Error(c *gin.Context, code int, err error) { func (s *Service) Error(c *gin.Context, code int, err error) {

View File

@ -9,12 +9,12 @@ import (
) )
type Account struct { type Account struct {
ID uint32 `gorm:"primary_key"` ID uint32 `gorm:"primary_key" json:"-"`
Login string `gorm:"unique;size:64;not null"` Login string `gorm:"unique;size:64;not null" json:"login"`
Password string `gorm:"-"` Password string `gorm:"-" json:"-"`
EncryptedPassword string `gorm:"size:44;not null"` EncryptedPassword string `gorm:"size:44;not null" json:"-"`
CreatedAt time.Time CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time UpdatedAt time.Time `json:"-"`
} }
func (a *Account) BeforeCreate(tx *gorm.DB) error { func (a *Account) BeforeCreate(tx *gorm.DB) error {

View File

@ -1,15 +1,27 @@
package models package models
import "time" import (
"time"
"gorm.io/gorm"
)
type Album struct { type Album struct {
ID uint32 `gorm:"primary_key"` ID uint32 `gorm:"primary_key" json:"id"`
Name string `gorm:"not null"` Name string `gorm:"not null" json:"name"`
Parent *Album `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE"` Parent *Album `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"parent"`
ParentId *uint32 ParentId *uint32 `json:"-"`
Author *Account `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE"` Author *Account `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"author"`
AuthorId *uint32 AuthorId *uint32 `json:"-"`
Photos []*Photo `gorm:"many2many:album_photos"` Photos []*Photo `gorm:"many2many:album_photos" json:"-"`
CreatedAt time.Time CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time UpdatedAt time.Time `json:"updated_at"`
}
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
}
return nil
} }