From fe9d6618f1870d453c899b8a2b11e76cf13ba208 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Tue, 14 Apr 2026 11:28:45 +0700 Subject: [PATCH] feat: fixing all module cms and media library --- .../cms_content_submissions.service.go | 23 ++++++++++++++++++- .../controller/media_library.controller.go | 4 +++- .../service/media_library.service.go | 11 +++++---- .../our_product_contents.repository.go | 19 +++++++++------ .../our_service_contents.repository.go | 19 +++++++++------ config/toml/config.toml | 2 +- 6 files changed, 56 insertions(+), 22 deletions(-) diff --git a/app/module/cms_content_submissions/service/cms_content_submissions.service.go b/app/module/cms_content_submissions/service/cms_content_submissions.service.go index c5d1125..3dfe009 100644 --- a/app/module/cms_content_submissions/service/cms_content_submissions.service.go +++ b/app/module/cms_content_submissions/service/cms_content_submissions.service.go @@ -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, diff --git a/app/module/media_library/controller/media_library.controller.go b/app/module/media_library/controller/media_library.controller.go index 58dc692..268ca94 100644 --- a/app/module/media_library/controller/media_library.controller.go +++ b/app/module/media_library/controller/media_library.controller.go @@ -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}, }) } diff --git a/app/module/media_library/service/media_library.service.go b/app/module/media_library/service/media_library.service.go index e607fe5..1ea0cec 100644 --- a/app/module/media_library/service/media_library.service.go +++ b/app/module/media_library/service/media_library.service.go @@ -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 } diff --git a/app/module/our_product_contents/repository/our_product_contents.repository.go b/app/module/our_product_contents/repository/our_product_contents.repository.go index 3275d99..a171ff0 100644 --- a/app/module/our_product_contents/repository/our_product_contents.repository.go +++ b/app/module/our_product_contents/repository/our_product_contents.repository.go @@ -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 { - r.Log.Error().Err(err).Msg("failed delete our product content") - return err - } - - return 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 + }) } diff --git a/app/module/our_service_contents/repository/our_service_contents.repository.go b/app/module/our_service_contents/repository/our_service_contents.repository.go index cfc7deb..fd5337a 100644 --- a/app/module/our_service_contents/repository/our_service_contents.repository.go +++ b/app/module/our_service_contents/repository/our_service_contents.repository.go @@ -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 { - r.Log.Error().Err(err).Msg("failed delete our service content") - return err - } - - return 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 + }) } diff --git a/config/toml/config.toml b/config/toml/config.toml index a18d711..7d6e3a2 100644 --- a/config/toml/config.toml +++ b/config/toml/config.toml @@ -9,7 +9,7 @@ idle-timeout = 5 # As seconds print-routes = false prefork = false # false: CMS preview URLs use http://localhost + port above. true: use domain (e.g. https://qudo.id/api). -production = true +production = false body-limit = 1048576000 # "100 * 1024 * 1024" [db.postgres]