Compare commits
2 Commits
7b41a9c7af
...
dc0bbf1169
| Author | SHA1 | Date |
|---|---|---|
|
|
dc0bbf1169 | |
|
|
fe9d6618f1 |
|
|
@ -386,6 +386,16 @@ func (_i *cmsContentSubmissionsService) mergeProduct(raw []byte) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Remove images first (FK from our_product_content_images → our_product_contents).
|
||||
imgs, err := _i.OurProductImg.FindByContentID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range imgs {
|
||||
if err := _i.OurProductImg.Delete(imgs[i].ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return _i.OurProduct.Delete(id)
|
||||
}
|
||||
ent := &entity.OurProductContent{
|
||||
|
|
@ -453,7 +463,18 @@ func (_i *cmsContentSubmissionsService) mergeService(raw []byte) error {
|
|||
if p.ServiceID == nil || *p.ServiceID == 0 {
|
||||
return errors.New("service_id required for delete")
|
||||
}
|
||||
return _i.OurService.Delete(uint(*p.ServiceID))
|
||||
sid := uint(*p.ServiceID)
|
||||
// Remove images first (FK from our_service_content_images → our_service_contents).
|
||||
simgs, err := _i.OurServiceImg.FindByContentID(sid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range simgs {
|
||||
if err := _i.OurServiceImg.Delete(simgs[i].ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return _i.OurService.Delete(sid)
|
||||
}
|
||||
ent := &entity.OurServiceContent{
|
||||
PrimaryTitle: p.PrimaryTitle,
|
||||
|
|
|
|||
|
|
@ -79,12 +79,14 @@ func (_i *MediaLibraryController) Upload(c *fiber.Ctx) error {
|
|||
if user != nil {
|
||||
uid = int(user.ID)
|
||||
}
|
||||
if err := _i.svc.Upload(clientID, uid, c); err != nil {
|
||||
publicURL, err := _i.svc.Upload(clientID, uid, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return utilRes.Resp(c, utilRes.Response{
|
||||
Success: true,
|
||||
Messages: utilRes.Messages{"File uploaded and added to media library"},
|
||||
Data: map[string]string{"public_url": publicURL},
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ type MediaLibraryService interface {
|
|||
RegisterFromRequest(clientID *uuid.UUID, userID int, req *request.MediaLibraryRegisterRequest) error
|
||||
RegisterCMSAsset(publicURL, objectKey, sourceLabel string, file *multipart.FileHeader) error
|
||||
All(clientID *uuid.UUID, q string, sourceType *string, p *paginator.Pagination) ([]*response.MediaLibraryItemResponse, *paginator.Pagination, error)
|
||||
Upload(clientID *uuid.UUID, userID int, c *fiber.Ctx) error
|
||||
Upload(clientID *uuid.UUID, userID int, c *fiber.Ctx) (publicURL string, err error)
|
||||
Delete(clientID *uuid.UUID, id uint) error
|
||||
}
|
||||
|
||||
|
|
@ -213,18 +213,18 @@ func (s *mediaLibraryService) All(clientID *uuid.UUID, q string, sourceType *str
|
|||
return out, paging, nil
|
||||
}
|
||||
|
||||
func (s *mediaLibraryService) Upload(clientID *uuid.UUID, userID int, c *fiber.Ctx) error {
|
||||
func (s *mediaLibraryService) Upload(clientID *uuid.UUID, userID int, c *fiber.Ctx) (string, error) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
key, previewURL, err := storage.UploadMediaLibraryObject(s.MinioStorage, file)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
name := filepath.Base(file.Filename)
|
||||
sz := file.Size
|
||||
return s.UpsertRegister(RegisterInput{
|
||||
err = s.UpsertRegister(RegisterInput{
|
||||
ClientID: clientID,
|
||||
UserID: userID,
|
||||
PublicURL: previewURL,
|
||||
|
|
@ -235,6 +235,7 @@ func (s *mediaLibraryService) Upload(clientID *uuid.UUID, userID int, c *fiber.C
|
|||
SourceType: "upload",
|
||||
SourceLabel: strPtr("media_library_direct"),
|
||||
})
|
||||
return previewURL, err
|
||||
}
|
||||
|
||||
func strPtr(s string) *string { return &s }
|
||||
|
|
|
|||
|
|
@ -99,12 +99,17 @@ func (r *ourProductContentRepository) Update(id uuid.UUID, data *entity.OurProdu
|
|||
return nil
|
||||
}
|
||||
|
||||
// Delete removes child images first so FK constraints do not block the parent delete.
|
||||
func (r *ourProductContentRepository) Delete(id uuid.UUID) error {
|
||||
err := r.DB.DB.Delete(&entity.OurProductContent{}, id).Error
|
||||
if err != nil {
|
||||
return r.DB.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec(`DELETE FROM our_product_content_images WHERE our_product_content_id = ?`, id).Error; err != nil {
|
||||
r.Log.Error().Err(err).Msg("failed delete our product content images")
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec(`DELETE FROM our_product_contents WHERE id = ?`, id).Error; err != nil {
|
||||
r.Log.Error().Err(err).Msg("failed delete our product content")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,12 +96,17 @@ func (r *ourServiceContentRepository) Update(id uint, data *entity.OurServiceCon
|
|||
return nil
|
||||
}
|
||||
|
||||
// Delete removes child images first so FK constraints do not block the parent delete.
|
||||
func (r *ourServiceContentRepository) Delete(id uint) error {
|
||||
err := r.DB.DB.Delete(&entity.OurServiceContent{}, id).Error
|
||||
if err != nil {
|
||||
return r.DB.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec(`DELETE FROM our_service_content_images WHERE our_service_content_id = ?`, id).Error; err != nil {
|
||||
r.Log.Error().Err(err).Msg("failed delete our service content images")
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec(`DELETE FROM our_service_contents WHERE id = ?`, id).Error; err != nil {
|
||||
r.Log.Error().Err(err).Msg("failed delete our service content")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue