58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab.celogeek.com/photos/api/internal/photos/models"
|
|
"gopkg.in/validator.v2"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type AlbumCreateRequest struct {
|
|
Name string `validate:"min=1,max=255,regexp=^[^/]*$"`
|
|
Parent *uint32
|
|
}
|
|
|
|
func (s *Service) AlbumCreate(c *gin.Context) {
|
|
req := &AlbumCreateRequest{}
|
|
if err := c.ShouldBindJSON(req); err != nil {
|
|
s.Error(c, http.StatusBadRequest, err)
|
|
}
|
|
if err := validator.Validate(req); err != nil {
|
|
s.Error(c, http.StatusExpectationFailed, err)
|
|
return
|
|
}
|
|
|
|
sess := s.CurrentSession(c)
|
|
|
|
if req.Parent != nil {
|
|
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,
|
|
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)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "success",
|
|
"album": album,
|
|
})
|
|
}
|