44 lines
850 B
Go
44 lines
850 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab.celogeek.com/photos/api/internal/photos/models"
|
|
"gopkg.in/validator.v2"
|
|
)
|
|
|
|
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)
|
|
|
|
album := &models.Album{
|
|
Name: req.Name,
|
|
ParentId: req.Parent,
|
|
AuthorId: &sess.AccountId,
|
|
}
|
|
|
|
if err := s.DB.Create(album).Error; err != nil {
|
|
s.Error(c, http.StatusConflict, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "success",
|
|
"album": album,
|
|
})
|
|
}
|