feat: fixing upload product

This commit is contained in:
hanif salafi 2026-01-28 07:30:00 +07:00
parent 976b7bc021
commit 95f48ba56b
2 changed files with 71 additions and 36 deletions

View File

@ -30,13 +30,12 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
type productsService struct { type productsService struct {
Repo repository.ProductsRepository Repo repository.ProductsRepository
Log zerolog.Logger Log zerolog.Logger
Cfg *config.Config Cfg *config.Config
MinioStorage *minioStorage.MinioStorage MinioStorage *minioStorage.MinioStorage
UsersRepo usersRepository.UsersRepository UsersRepo usersRepository.UsersRepository
ApprovalHistoriesService approvalHistoriesService.ApprovalHistoriesService ApprovalHistoriesService approvalHistoriesService.ApprovalHistoriesService
} }
@ -310,25 +309,63 @@ func (_i *productsService) Delete(id uint) (err error) {
func (_i *productsService) Viewer(c *fiber.Ctx) (err error) { func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
filename := c.Params("filename") filename := c.Params("filename")
// Find product by filename (repository will search using LIKE pattern) var objectName string
var found bool
// First, try to find by ThumbnailPath (for main product images)
result, err := _i.Repo.FindByThumbnailPath(filename) result, err := _i.Repo.FindByThumbnailPath(filename)
if err != nil { if err == nil && result != nil && result.ThumbnailPath != nil && *result.ThumbnailPath != "" {
// Check if the filename matches the thumbnail
if strings.Contains(*result.ThumbnailPath, filename) {
objectName = *result.ThumbnailPath
found = true
}
}
// If not found in ThumbnailPath, search in Colors JSON field
if !found {
// Create a query request with large limit to search all products
queryReq := request.ProductsQueryRequest{
Pagination: &paginator.Pagination{
Page: 1,
Limit: 1000, // Large limit to search all products
},
}
allProducts, _, err := _i.Repo.GetAll(queryReq)
if err == nil {
for _, product := range allProducts {
if product.Colors != nil && *product.Colors != "" {
var rawColors []struct {
Name string `json:"name"`
ImagePath *string `json:"image_path"`
}
if err := json.Unmarshal([]byte(*product.Colors), &rawColors); err == nil {
for _, color := range rawColors {
if color.ImagePath != nil && strings.Contains(*color.ImagePath, filename) {
objectName = *color.ImagePath
found = true
break
}
}
}
if found {
break
}
}
}
}
}
if !found || objectName == "" {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": true, "error": true,
"msg": "Product file not found", "msg": "Product file not found",
}) })
} }
if result.ThumbnailPath == nil || *result.ThumbnailPath == "" {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": true,
"msg": "Product thumbnail path not found",
})
}
ctx := context.Background() ctx := context.Background()
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
objectName := *result.ThumbnailPath
_i.Log.Info().Str("timestamp", time.Now(). _i.Log.Info().Str("timestamp", time.Now().
Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer"). Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer").
@ -370,8 +407,6 @@ func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
return return
} }
func getFileExtension(filename string) string { func getFileExtension(filename string) string {
// split file name // split file name
parts := strings.Split(filename, ".") parts := strings.Split(filename, ".")
@ -409,14 +444,14 @@ func (_i *productsService) Approve(id uint, authToken string) (product *response
userID := user.ID userID := user.ID
statusApprove := 2 statusApprove := 2
err = _i.ApprovalHistoriesService.CreateHistory( err = _i.ApprovalHistoriesService.CreateHistory(
"products", "products",
id, id,
&statusApprove, // ✅ pointer &statusApprove, // ✅ pointer
"approve", "approve",
&userID, &userID,
nil, nil,
) )
if err != nil { if err != nil {
_i.Log.Error().Err(err).Msg("Failed to save approval history") _i.Log.Error().Err(err).Msg("Failed to save approval history")
} }
@ -461,14 +496,14 @@ func (_i *productsService) Reject(id uint, authToken string, message *string) (p
userID := user.ID userID := user.ID
statusReject := 3 statusReject := 3
err = _i.ApprovalHistoriesService.CreateHistory( err = _i.ApprovalHistoriesService.CreateHistory(
"productss", "productss",
id, id,
&statusReject, // ✅ pointer &statusReject, // ✅ pointer
"reject", "reject",
&userID, &userID,
message, message,
) )
if err != nil { if err != nil {
_i.Log.Error().Err(err).Msg("Failed to save rejection history") _i.Log.Error().Err(err).Msg("Failed to save rejection history")
} }
@ -511,7 +546,7 @@ func (_i *productsService) Comment(
err = _i.ApprovalHistoriesService.CreateHistory( err = _i.ApprovalHistoriesService.CreateHistory(
"banners", "banners",
id, id,
nil, // status_id NULL nil, // status_id NULL
"comment", "comment",
&userID, &userID,
message, message,

2
go.mod
View File

@ -13,6 +13,7 @@ require (
github.com/go-playground/validator/v10 v10.17.0 github.com/go-playground/validator/v10 v10.17.0
github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/fiber/v2 v2.52.4
github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/minio/minio-go/v7 v7.0.68 github.com/minio/minio-go/v7 v7.0.68
github.com/pelletier/go-toml/v2 v2.1.1 github.com/pelletier/go-toml/v2 v2.1.1
github.com/rs/zerolog v1.31.0 github.com/rs/zerolog v1.31.0
@ -34,7 +35,6 @@ require (
github.com/go-openapi/spec v0.20.15 // indirect github.com/go-openapi/spec v0.20.15 // indirect
github.com/go-openapi/swag v0.22.10 // indirect github.com/go-openapi/swag v0.22.10 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect github.com/jackc/pgx/v5 v5.4.3 // indirect