Compare commits

...

2 Commits

Author SHA1 Message Date
hanif salafi dc0bbf1169 feat: fixing all module cms and media library
continuous-integration/drone/push Build is passing Details
2026-04-14 11:29:00 +07:00
hanif salafi fe9d6618f1 feat: fixing all module cms and media library 2026-04-14 11:28:45 +07:00
5 changed files with 55 additions and 21 deletions

View File

@ -386,6 +386,16 @@ func (_i *cmsContentSubmissionsService) mergeProduct(raw []byte) error {
if err != nil { if err != nil {
return err 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) return _i.OurProduct.Delete(id)
} }
ent := &entity.OurProductContent{ ent := &entity.OurProductContent{
@ -453,7 +463,18 @@ func (_i *cmsContentSubmissionsService) mergeService(raw []byte) error {
if p.ServiceID == nil || *p.ServiceID == 0 { if p.ServiceID == nil || *p.ServiceID == 0 {
return errors.New("service_id required for delete") 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{ ent := &entity.OurServiceContent{
PrimaryTitle: p.PrimaryTitle, PrimaryTitle: p.PrimaryTitle,

View File

@ -79,12 +79,14 @@ func (_i *MediaLibraryController) Upload(c *fiber.Ctx) error {
if user != nil { if user != nil {
uid = int(user.ID) 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 err
} }
return utilRes.Resp(c, utilRes.Response{ return utilRes.Resp(c, utilRes.Response{
Success: true, Success: true,
Messages: utilRes.Messages{"File uploaded and added to media library"}, Messages: utilRes.Messages{"File uploaded and added to media library"},
Data: map[string]string{"public_url": publicURL},
}) })
} }

View File

@ -40,7 +40,7 @@ type MediaLibraryService interface {
RegisterFromRequest(clientID *uuid.UUID, userID int, req *request.MediaLibraryRegisterRequest) error RegisterFromRequest(clientID *uuid.UUID, userID int, req *request.MediaLibraryRegisterRequest) error
RegisterCMSAsset(publicURL, objectKey, sourceLabel string, file *multipart.FileHeader) 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) 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 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 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") file, err := c.FormFile("file")
if err != nil { if err != nil {
return err return "", err
} }
key, previewURL, err := storage.UploadMediaLibraryObject(s.MinioStorage, file) key, previewURL, err := storage.UploadMediaLibraryObject(s.MinioStorage, file)
if err != nil { if err != nil {
return err return "", err
} }
name := filepath.Base(file.Filename) name := filepath.Base(file.Filename)
sz := file.Size sz := file.Size
return s.UpsertRegister(RegisterInput{ err = s.UpsertRegister(RegisterInput{
ClientID: clientID, ClientID: clientID,
UserID: userID, UserID: userID,
PublicURL: previewURL, PublicURL: previewURL,
@ -235,6 +235,7 @@ func (s *mediaLibraryService) Upload(clientID *uuid.UUID, userID int, c *fiber.C
SourceType: "upload", SourceType: "upload",
SourceLabel: strPtr("media_library_direct"), SourceLabel: strPtr("media_library_direct"),
}) })
return previewURL, err
} }
func strPtr(s string) *string { return &s } func strPtr(s string) *string { return &s }

View File

@ -99,12 +99,17 @@ func (r *ourProductContentRepository) Update(id uuid.UUID, data *entity.OurProdu
return nil return nil
} }
// Delete removes child images first so FK constraints do not block the parent delete.
func (r *ourProductContentRepository) Delete(id uuid.UUID) error { func (r *ourProductContentRepository) Delete(id uuid.UUID) error {
err := r.DB.DB.Delete(&entity.OurProductContent{}, id).Error return r.DB.DB.Transaction(func(tx *gorm.DB) error {
if err != nil { 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") r.Log.Error().Err(err).Msg("failed delete our product content")
return err return err
} }
return nil return nil
})
} }

View File

@ -96,12 +96,17 @@ func (r *ourServiceContentRepository) Update(id uint, data *entity.OurServiceCon
return nil return nil
} }
// Delete removes child images first so FK constraints do not block the parent delete.
func (r *ourServiceContentRepository) Delete(id uint) error { func (r *ourServiceContentRepository) Delete(id uint) error {
err := r.DB.DB.Delete(&entity.OurServiceContent{}, id).Error return r.DB.DB.Transaction(func(tx *gorm.DB) error {
if err != nil { 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") r.Log.Error().Err(err).Msg("failed delete our service content")
return err return err
} }
return nil return nil
})
} }