Compare commits
10 Commits
383094d1b5
...
2360ed406d
| Author | SHA1 | Date |
|---|---|---|
|
|
2360ed406d | |
|
|
0f46937b14 | |
|
|
99aa77e60a | |
|
|
53b16cc0bc | |
|
|
95f48ba56b | |
|
|
976b7bc021 | |
|
|
42e856f068 | |
|
|
13b95c0109 | |
|
|
6ac6e6ea9c | |
|
|
1098c22738 |
|
|
@ -13,5 +13,7 @@ type Galleries struct {
|
||||||
IsActive *bool `json:"is_active" gorm:"type:bool;default:true"`
|
IsActive *bool `json:"is_active" gorm:"type:bool;default:true"`
|
||||||
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
|
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
|
||||||
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
|
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
|
||||||
|
Category string `json:"category" db:"category"`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Products struct {
|
type Products struct {
|
||||||
ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
|
ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
|
||||||
Title string `json:"title" gorm:"type:varchar"`
|
Title string `json:"title" gorm:"type:varchar"`
|
||||||
Variant *string `json:"variant" gorm:"type:varchar"`
|
Variant *string `json:"variant" gorm:"type:varchar"`
|
||||||
Price *string `json:"price" gorm:"type:varchar"`
|
Price *string `json:"price" gorm:"type:varchar"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path" gorm:"type:varchar"`
|
ThumbnailPath *string `json:"thumbnail_path" gorm:"type:varchar"`
|
||||||
Colors *string `json:"colors" gorm:"type:text"` // JSON array stored as text
|
Colors *string `json:"colors" gorm:"type:text"` // JSON array stored as text
|
||||||
Status *string `json:"status" gorm:"type:varchar"`
|
Specifications *string `json:"specifications" gorm:"type:text"` // JSON array stored as text
|
||||||
StatusId *int `json:"status_id" gorm:"type:int4;default:1"`
|
Status *string `json:"status" gorm:"type:varchar"`
|
||||||
IsActive *bool `json:"is_active" gorm:"type:bool;default:true"`
|
StatusId *int `json:"status_id" gorm:"type:int4;default:1"`
|
||||||
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
|
IsActive *bool `json:"is_active" gorm:"type:bool;default:true"`
|
||||||
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
|
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,21 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"jaecoo-be/app/middleware"
|
||||||
"jaecoo-be/app/module/banners/request"
|
"jaecoo-be/app/module/banners/request"
|
||||||
"jaecoo-be/app/module/banners/service"
|
"jaecoo-be/app/module/banners/service"
|
||||||
"jaecoo-be/utils/paginator"
|
"jaecoo-be/utils/paginator"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
utilRes "jaecoo-be/utils/response"
|
utilRes "jaecoo-be/utils/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
type bannersController struct {
|
type bannersController struct {
|
||||||
bannersService service.BannersService
|
bannersService service.BannersService
|
||||||
|
Log zerolog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,7 +64,14 @@ func (_i *bannersController) All(c *fiber.Ctx) error {
|
||||||
req := reqContext.ToParamRequest()
|
req := reqContext.ToParamRequest()
|
||||||
req.Pagination = paginate
|
req.Pagination = paginate
|
||||||
|
|
||||||
bannersData, paging, err := _i.bannersService.GetAll(req)
|
// ✅ ambil ClientId dari middleware
|
||||||
|
clientId := middleware.GetClientID(c)
|
||||||
|
|
||||||
|
// ✅ ambil Authorization token (kalau dibutuhkan)
|
||||||
|
|
||||||
|
_i.Log.Info().Interface("clientId", clientId).Msg("")
|
||||||
|
|
||||||
|
bannersData, paging, err := _i.bannersService.GetAll(clientId,req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -74,6 +84,7 @@ func (_i *bannersController) All(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Show Banner
|
// Show Banner
|
||||||
// @Summary Get Banner by ID
|
// @Summary Get Banner by ID
|
||||||
// @Description API for getting Banner by ID
|
// @Description API for getting Banner by ID
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"jaecoo-be/utils/paginator"
|
"jaecoo-be/utils/paginator"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -16,7 +17,7 @@ type bannersRepository struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type BannersRepository interface {
|
type BannersRepository interface {
|
||||||
GetAll(req request.BannersQueryRequest) (banners []*entity.Banners, paging paginator.Pagination, err error)
|
GetAll(clientId *uuid.UUID, req request.BannersQueryRequest) (banners []*entity.Banners, paging paginator.Pagination, err error)
|
||||||
FindOne(id uint) (banner *entity.Banners, err error)
|
FindOne(id uint) (banner *entity.Banners, err error)
|
||||||
Create(banner *entity.Banners) (bannerReturn *entity.Banners, err error)
|
Create(banner *entity.Banners) (bannerReturn *entity.Banners, err error)
|
||||||
Update(id uint, banner *entity.Banners) (err error)
|
Update(id uint, banner *entity.Banners) (err error)
|
||||||
|
|
@ -34,11 +35,15 @@ func NewBannersRepository(db *database.Database, log zerolog.Logger) BannersRepo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *bannersRepository) GetAll(req request.BannersQueryRequest) (banners []*entity.Banners, paging paginator.Pagination, err error) {
|
func (_i *bannersRepository) GetAll(clientId *uuid.UUID, req request.BannersQueryRequest) (banners []*entity.Banners, paging paginator.Pagination, err error) {
|
||||||
var count int64
|
var count int64
|
||||||
|
|
||||||
query := _i.DB.DB.Model(&entity.Banners{})
|
query := _i.DB.DB.Model(&entity.Banners{})
|
||||||
|
|
||||||
|
if clientId != nil {
|
||||||
|
query = query.Where("banners.client_id = ?", clientId)
|
||||||
|
}
|
||||||
|
|
||||||
if req.Title != nil && *req.Title != "" {
|
if req.Title != nil && *req.Title != "" {
|
||||||
title := strings.ToLower(*req.Title)
|
title := strings.ToLower(*req.Title)
|
||||||
query = query.Where("LOWER(title) LIKE ?", "%"+title+"%")
|
query = query.Where("LOWER(title) LIKE ?", "%"+title+"%")
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
@ -37,7 +38,7 @@ type bannersService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type BannersService interface {
|
type BannersService interface {
|
||||||
GetAll(req request.BannersQueryRequest) (banners []*response.BannersResponse, paging paginator.Pagination, err error)
|
GetAll(clientId *uuid.UUID,req request.BannersQueryRequest) (banners []*response.BannersResponse, paging paginator.Pagination, err error)
|
||||||
GetOne(id uint) (banner *response.BannersResponse, err error)
|
GetOne(id uint) (banner *response.BannersResponse, err error)
|
||||||
Create(c *fiber.Ctx, req request.BannersCreateRequest) (banner *response.BannersResponse, err error)
|
Create(c *fiber.Ctx, req request.BannersCreateRequest) (banner *response.BannersResponse, err error)
|
||||||
Update(c *fiber.Ctx, id uint, req request.BannersUpdateRequest) (banner *response.BannersResponse, err error)
|
Update(c *fiber.Ctx, id uint, req request.BannersUpdateRequest) (banner *response.BannersResponse, err error)
|
||||||
|
|
@ -60,14 +61,17 @@ func NewBannersService(repo repository.BannersRepository, log zerolog.Logger, cf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *bannersService) GetAll(req request.BannersQueryRequest) (banners []*response.BannersResponse, paging paginator.Pagination, err error) {
|
func (_i *bannersService) GetAll(
|
||||||
bannersEntity, paging, err := _i.Repo.GetAll(req)
|
clientId *uuid.UUID,
|
||||||
|
req request.BannersQueryRequest,
|
||||||
|
) (banners []*response.BannersResponse, paging paginator.Pagination, err error) {
|
||||||
|
|
||||||
|
bannersEntity, paging, err := _i.Repo.GetAll(clientId, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
host := _i.Cfg.App.Domain
|
host := _i.Cfg.App.Domain
|
||||||
|
|
||||||
for _, banner := range bannersEntity {
|
for _, banner := range bannersEntity {
|
||||||
banners = append(banners, mapper.BannersResponseMapper(banner, host))
|
banners = append(banners, mapper.BannersResponseMapper(banner, host))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ func GalleriesResponseMapper(gallery *entity.Galleries, host string) *res.Galler
|
||||||
response := &res.GalleriesResponse{
|
response := &res.GalleriesResponse{
|
||||||
ID: gallery.ID,
|
ID: gallery.ID,
|
||||||
Title: gallery.Title,
|
Title: gallery.Title,
|
||||||
|
Category: gallery.Category,
|
||||||
Description: gallery.Description,
|
Description: gallery.Description,
|
||||||
ThumbnailPath: gallery.ThumbnailPath,
|
ThumbnailPath: gallery.ThumbnailPath,
|
||||||
StatusId: gallery.StatusId,
|
StatusId: gallery.StatusId,
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ type GalleriesCreateRequest struct {
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
|
Category string `json:"category" validate:"required"`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req GalleriesCreateRequest) ToEntity() *entity.Galleries {
|
func (req GalleriesCreateRequest) ToEntity() *entity.Galleries {
|
||||||
|
|
@ -40,6 +42,7 @@ func (req GalleriesCreateRequest) ToEntity() *entity.Galleries {
|
||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
ThumbnailPath: req.ThumbnailPath,
|
ThumbnailPath: req.ThumbnailPath,
|
||||||
|
Category: req.Category,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,14 +50,18 @@ type GalleriesUpdateRequest struct {
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
|
Category string `json:"category" validate:"required"`
|
||||||
IsActive *bool `json:"is_active"`
|
IsActive *bool `json:"is_active"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (req GalleriesUpdateRequest) ToEntity() *entity.Galleries {
|
func (req GalleriesUpdateRequest) ToEntity() *entity.Galleries {
|
||||||
return &entity.Galleries{
|
return &entity.Galleries{
|
||||||
Title: getStringValue(req.Title),
|
Title: getStringValue(req.Title),
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
ThumbnailPath: req.ThumbnailPath,
|
ThumbnailPath: req.ThumbnailPath,
|
||||||
|
Category: req.Category,
|
||||||
IsActive: req.IsActive,
|
IsActive: req.IsActive,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
type GalleriesResponse struct {
|
type GalleriesResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
Category string `json:"category"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
ThumbnailUrl *string `json:"thumbnail_url"`
|
ThumbnailUrl *string `json:"thumbnail_url"`
|
||||||
|
|
|
||||||
|
|
@ -229,9 +229,9 @@ func (_i *productsController) Update(c *fiber.Ctx) error {
|
||||||
req.Price = &price
|
req.Price = &price
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle colors (JSON array string)
|
// Handle colors (JSON array of objects with name and image_path)
|
||||||
if colorsStr := c.FormValue("colors"); colorsStr != "" {
|
if colorsStr := c.FormValue("colors"); colorsStr != "" {
|
||||||
var colors []string
|
var colors []request.ProductColorRequest
|
||||||
if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
|
if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
|
||||||
req.Colors = colors
|
req.Colors = colors
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,31 +14,57 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
|
||||||
|
|
||||||
var colors []res.ProductColorResponse
|
var colors []res.ProductColorResponse
|
||||||
|
|
||||||
if product.Colors != nil && *product.Colors != "" {
|
if product.Colors != nil && *product.Colors != "" {
|
||||||
var rawColors []struct {
|
var rawColors []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ImagePath *string `json:"image_path"`
|
ImagePath *string `json:"image_path"`
|
||||||
}
|
|
||||||
|
|
||||||
_ = json.Unmarshal([]byte(*product.Colors), &rawColors)
|
|
||||||
|
|
||||||
for _, c := range rawColors {
|
|
||||||
var imageUrl *string
|
|
||||||
|
|
||||||
if c.ImagePath != nil {
|
|
||||||
filename := filepath.Base(*c.ImagePath)
|
|
||||||
url := host + "/products/viewer/" + filename
|
|
||||||
imageUrl = &url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colors = append(colors, res.ProductColorResponse{
|
_ = json.Unmarshal([]byte(*product.Colors), &rawColors)
|
||||||
Name: c.Name,
|
|
||||||
ImagePath: c.ImagePath,
|
|
||||||
ImageUrl: imageUrl,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for _, c := range rawColors {
|
||||||
|
var imageUrl *string
|
||||||
|
|
||||||
|
if c.ImagePath != nil {
|
||||||
|
filename := filepath.Base(*c.ImagePath)
|
||||||
|
url := host + "/products/viewer/" + filename
|
||||||
|
imageUrl = &url
|
||||||
|
}
|
||||||
|
|
||||||
|
colors = append(colors, res.ProductColorResponse{
|
||||||
|
Name: c.Name,
|
||||||
|
ImagePath: c.ImagePath,
|
||||||
|
ImageUrl: imageUrl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var specifications []res.ProductSpecificationResponse
|
||||||
|
|
||||||
|
if product.Specifications != nil && *product.Specifications != "" {
|
||||||
|
var rawSpecs []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = json.Unmarshal([]byte(*product.Specifications), &rawSpecs)
|
||||||
|
|
||||||
|
for _, s := range rawSpecs {
|
||||||
|
var imageUrls []string
|
||||||
|
|
||||||
|
for _, imagePath := range s.ImagePaths {
|
||||||
|
filename := filepath.Base(imagePath)
|
||||||
|
url := host + "/products/viewer/" + filename
|
||||||
|
imageUrls = append(imageUrls, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
specifications = append(specifications, res.ProductSpecificationResponse{
|
||||||
|
Title: s.Title,
|
||||||
|
ImagePaths: s.ImagePaths,
|
||||||
|
ImageUrls: imageUrls,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response := &res.ProductsResponse{
|
response := &res.ProductsResponse{
|
||||||
ID: product.ID,
|
ID: product.ID,
|
||||||
|
|
@ -47,6 +73,7 @@ if product.Colors != nil && *product.Colors != "" {
|
||||||
Price: product.Price,
|
Price: product.Price,
|
||||||
ThumbnailPath: product.ThumbnailPath,
|
ThumbnailPath: product.ThumbnailPath,
|
||||||
Colors: colors,
|
Colors: colors,
|
||||||
|
Specifications: specifications,
|
||||||
Status: product.Status,
|
Status: product.Status,
|
||||||
StatusId: product.StatusId,
|
StatusId: product.StatusId,
|
||||||
IsActive: product.IsActive,
|
IsActive: product.IsActive,
|
||||||
|
|
|
||||||
|
|
@ -95,13 +95,13 @@ func (req ProductsCreateRequest) ToEntity() *entity.Products {
|
||||||
|
|
||||||
|
|
||||||
type ProductsUpdateRequest struct {
|
type ProductsUpdateRequest struct {
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Variant *string `json:"variant"`
|
Variant *string `json:"variant"`
|
||||||
Price *string `json:"price"`
|
Price *string `json:"price"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
Colors []string `json:"colors"`
|
Colors []ProductColorRequest `json:"colors"`
|
||||||
Status *string `json:"status"`
|
Status *string `json:"status"`
|
||||||
IsActive *bool `json:"is_active"`
|
IsActive *bool `json:"is_active"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req ProductsUpdateRequest) ToEntity() *entity.Products {
|
func (req ProductsUpdateRequest) ToEntity() *entity.Products {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,12 @@ type ProductColorResponse struct {
|
||||||
ImageUrl *string `json:"image_url"`
|
ImageUrl *string `json:"image_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ProductSpecificationResponse struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
ImageUrls []string `json:"image_urls"`
|
||||||
|
}
|
||||||
|
|
||||||
type ProductsResponse struct {
|
type ProductsResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
|
@ -18,6 +24,7 @@ type ProductsResponse struct {
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
ThumbnailUrl *string `json:"thumbnail_url"`
|
ThumbnailUrl *string `json:"thumbnail_url"`
|
||||||
Colors []ProductColorResponse `json:"colors"`
|
Colors []ProductColorResponse `json:"colors"`
|
||||||
|
Specifications []ProductSpecificationResponse `json:"specifications"`
|
||||||
Status *string `json:"status"`
|
Status *string `json:"status"`
|
||||||
StatusId *int `json:"status_id"`
|
StatusId *int `json:"status_id"`
|
||||||
IsActive *bool `json:"is_active"`
|
IsActive *bool `json:"is_active"`
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,20 +144,79 @@ func (_i *productsService) uploadColorFile(
|
||||||
return &objectName, nil
|
return &objectName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_i *productsService) uploadSpecFile(
|
||||||
|
fileHeader *multipart.FileHeader,
|
||||||
|
) (*string, error) {
|
||||||
|
|
||||||
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := fileHeader.Open()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
ext := filepath.Ext(fileHeader.Filename)
|
||||||
|
name := strings.TrimSuffix(fileHeader.Filename, ext)
|
||||||
|
name = strings.ReplaceAll(name, " ", "")
|
||||||
|
|
||||||
|
filename := fmt.Sprintf(
|
||||||
|
"%s_%d%s",
|
||||||
|
name,
|
||||||
|
rand.Intn(999999),
|
||||||
|
ext,
|
||||||
|
)
|
||||||
|
|
||||||
|
objectName := fmt.Sprintf(
|
||||||
|
"products/specifications/%d/%d/%s",
|
||||||
|
now.Year(),
|
||||||
|
now.Month(),
|
||||||
|
filename,
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err = minioClient.PutObject(
|
||||||
|
context.Background(),
|
||||||
|
_i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName,
|
||||||
|
objectName,
|
||||||
|
src,
|
||||||
|
fileHeader.Size,
|
||||||
|
minio.PutObjectOptions{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &objectName, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateRequest) (product *response.ProductsResponse, err error) {
|
func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateRequest) (product *response.ProductsResponse, err error) {
|
||||||
|
_i.Log.Info().
|
||||||
|
Str("title", req.Title).
|
||||||
|
Interface("colors", req.Colors).
|
||||||
|
Msg("🚀 Starting Create Product")
|
||||||
|
|
||||||
// Handle file upload if exists
|
// Handle file upload if exists
|
||||||
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
||||||
req.ThumbnailPath = filePath
|
req.ThumbnailPath = filePath
|
||||||
|
_i.Log.Info().Str("thumbnailPath", *filePath).Msg("✅ Uploaded thumbnail")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 CONVERT REQUEST KE ENTITY
|
// 🔥 CONVERT REQUEST KE ENTITY
|
||||||
productEntity := req.ToEntity()
|
productEntity := req.ToEntity()
|
||||||
|
|
||||||
// ===============================
|
|
||||||
// 3️⃣ 🔥 HANDLE COLORS + IMAGE
|
|
||||||
// ===============================
|
|
||||||
form, _ := c.MultipartForm()
|
form, _ := c.MultipartForm()
|
||||||
colorFiles := form.File["color_images"]
|
colorFiles := form.File["color_images"]
|
||||||
|
colorsStr := c.FormValue("colors")
|
||||||
|
|
||||||
|
_i.Log.Info().
|
||||||
|
Str("colorsStr", colorsStr).
|
||||||
|
Int("colorFilesCount", len(colorFiles)).
|
||||||
|
Msg("🎨 Processing colors")
|
||||||
|
|
||||||
var colorEntities []struct {
|
var colorEntities []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
@ -195,19 +253,119 @@ func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateReques
|
||||||
bytes, _ := json.Marshal(colorEntities)
|
bytes, _ := json.Marshal(colorEntities)
|
||||||
str := string(bytes)
|
str := string(bytes)
|
||||||
productEntity.Colors = &str
|
productEntity.Colors = &str
|
||||||
|
_i.Log.Info().
|
||||||
|
Int("colorsCount", len(colorEntities)).
|
||||||
|
Str("json", str).
|
||||||
|
Msg("💾 Saved colors to entity")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4️⃣ DEFAULT ACTIVE
|
// 6️⃣ HANDLE SPECIFICATIONS (as JSON array like colors)
|
||||||
|
form, _ = c.MultipartForm()
|
||||||
|
specFiles := form.File["specification_images"]
|
||||||
|
specificationsStr := c.FormValue("specifications")
|
||||||
|
|
||||||
|
_i.Log.Info().
|
||||||
|
Str("specificationsStr", specificationsStr).
|
||||||
|
Int("specFilesCount", len(specFiles)).
|
||||||
|
Msg("📦 Processing specifications in Create")
|
||||||
|
|
||||||
|
var specEntities []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if specificationsStr != "" {
|
||||||
|
var specifications []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImageCount int `json:"imageCount"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(specificationsStr), &specifications); err != nil {
|
||||||
|
_i.Log.Error().Err(err).Str("specificationsStr", specificationsStr).Msg("❌ Failed to unmarshal specifications")
|
||||||
|
} else {
|
||||||
|
_i.Log.Info().Int("specsCount", len(specifications)).Msg("✅ Parsed specifications JSON")
|
||||||
|
fileIndex := 0
|
||||||
|
for specIdx, spec := range specifications {
|
||||||
|
if spec.Title == "" {
|
||||||
|
_i.Log.Warn().Int("specIndex", specIdx).Msg("⚠️ Skipping spec with empty title")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
specEntity := struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}{
|
||||||
|
Title: spec.Title,
|
||||||
|
ImagePaths: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
_i.Log.Info().
|
||||||
|
Int("specIndex", specIdx).
|
||||||
|
Str("title", spec.Title).
|
||||||
|
Int("imageCount", spec.ImageCount).
|
||||||
|
Int("fileIndex", fileIndex).
|
||||||
|
Msg("📝 Processing spec")
|
||||||
|
|
||||||
|
// Upload files for this specification
|
||||||
|
imageCount := spec.ImageCount
|
||||||
|
if imageCount > 0 && fileIndex < len(specFiles) {
|
||||||
|
for i := 0; i < imageCount && fileIndex < len(specFiles); i++ {
|
||||||
|
fileHeader := specFiles[fileIndex]
|
||||||
|
_i.Log.Info().
|
||||||
|
Int("fileIndex", fileIndex).
|
||||||
|
Str("filename", fileHeader.Filename).
|
||||||
|
Msg("📤 Uploading spec image")
|
||||||
|
|
||||||
|
path, uploadErr := _i.uploadSpecFile(fileHeader)
|
||||||
|
if uploadErr != nil {
|
||||||
|
_i.Log.Error().Err(uploadErr).Int("fileIndex", fileIndex).Msg("❌ Failed to upload spec file")
|
||||||
|
} else if path != nil {
|
||||||
|
specEntity.ImagePaths = append(specEntity.ImagePaths, *path)
|
||||||
|
_i.Log.Info().Str("path", *path).Msg("✅ Uploaded spec image")
|
||||||
|
}
|
||||||
|
fileIndex++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
specEntities = append(specEntities, specEntity)
|
||||||
|
_i.Log.Info().
|
||||||
|
Str("title", specEntity.Title).
|
||||||
|
Int("imagesCount", len(specEntity.ImagePaths)).
|
||||||
|
Msg("✅ Added spec entity")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_i.Log.Warn().Msg("⚠️ No specifications string in form")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save specifications as JSON string
|
||||||
|
if len(specEntities) > 0 {
|
||||||
|
bytes, _ := json.Marshal(specEntities)
|
||||||
|
str := string(bytes)
|
||||||
|
productEntity.Specifications = &str
|
||||||
|
_i.Log.Info().
|
||||||
|
Int("specsCount", len(specEntities)).
|
||||||
|
Str("json", str).
|
||||||
|
Msg("💾 Saved specifications to entity")
|
||||||
|
} else {
|
||||||
|
_i.Log.Warn().Msg("⚠️ No spec entities to save")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7️⃣ DEFAULT ACTIVE & SAVE TO DB
|
||||||
isActive := true
|
isActive := true
|
||||||
productEntity.IsActive = &isActive
|
productEntity.IsActive = &isActive
|
||||||
|
|
||||||
// 5️⃣ SIMPAN KE DB
|
|
||||||
productEntity, err = _i.Repo.Create(productEntity)
|
productEntity, err = _i.Repo.Create(productEntity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
_i.Log.Error().Err(err).Msg("❌ Failed to create product in DB")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_i.Log.Info().
|
||||||
|
Uint("productId", productEntity.ID).
|
||||||
|
Interface("colors", productEntity.Colors).
|
||||||
|
Interface("specifications", productEntity.Specifications).
|
||||||
|
Msg("✅ Product created in DB with all data")
|
||||||
|
|
||||||
// 6️⃣ RESPONSE
|
// 8️⃣ RESPONSE
|
||||||
host := _i.Cfg.App.Domain
|
host := _i.Cfg.App.Domain
|
||||||
product = mapper.ProductsResponseMapper(productEntity, host)
|
product = mapper.ProductsResponseMapper(productEntity, host)
|
||||||
return
|
return
|
||||||
|
|
@ -284,8 +442,187 @@ func (_i *productsService) Update(c *fiber.Ctx, id uint, req request.ProductsUpd
|
||||||
req.ThumbnailPath = filePath
|
req.ThumbnailPath = filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get existing product to preserve existing colors if no new color images uploaded
|
||||||
|
existingProduct, err := _i.Repo.FindOne(id)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
productEntity := req.ToEntity()
|
productEntity := req.ToEntity()
|
||||||
|
|
||||||
|
// Handle color images if uploaded (similar to Create)
|
||||||
|
form, _ := c.MultipartForm()
|
||||||
|
colorFiles := form.File["color_images"]
|
||||||
|
|
||||||
|
// If color images are uploaded, process them
|
||||||
|
if len(colorFiles) > 0 {
|
||||||
|
var colorEntities []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use colors from request if available, otherwise preserve existing
|
||||||
|
var existingColors []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}
|
||||||
|
if existingProduct.Colors != nil && *existingProduct.Colors != "" {
|
||||||
|
_ = json.Unmarshal([]byte(*existingProduct.Colors), &existingColors)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process color files and merge with request colors
|
||||||
|
for i, cReq := range req.Colors {
|
||||||
|
if cReq.Name == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
color := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}{
|
||||||
|
Name: cReq.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's a new file uploaded for this color index, use it
|
||||||
|
if len(colorFiles) > i {
|
||||||
|
fileHeader := colorFiles[i]
|
||||||
|
path, err := _i.uploadColorFile(fileHeader)
|
||||||
|
if err == nil && path != nil {
|
||||||
|
color.ImagePath = path
|
||||||
|
} else if i < len(existingColors) {
|
||||||
|
// Keep existing image path if upload failed
|
||||||
|
color.ImagePath = existingColors[i].ImagePath
|
||||||
|
}
|
||||||
|
} else if i < len(existingColors) {
|
||||||
|
// No new file, preserve existing image path
|
||||||
|
color.ImagePath = existingColors[i].ImagePath
|
||||||
|
}
|
||||||
|
|
||||||
|
colorEntities = append(colorEntities, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update colors JSON if we have colors
|
||||||
|
if len(colorEntities) > 0 {
|
||||||
|
bytes, _ := json.Marshal(colorEntities)
|
||||||
|
str := string(bytes)
|
||||||
|
productEntity.Colors = &str
|
||||||
|
}
|
||||||
|
} else if len(req.Colors) > 0 {
|
||||||
|
// No new color images uploaded, but colors data is provided
|
||||||
|
// Preserve existing image paths and update names
|
||||||
|
var existingColors []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}
|
||||||
|
if existingProduct.Colors != nil && *existingProduct.Colors != "" {
|
||||||
|
_ = json.Unmarshal([]byte(*existingProduct.Colors), &existingColors)
|
||||||
|
}
|
||||||
|
|
||||||
|
var colorEntities []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, cReq := range req.Colors {
|
||||||
|
if cReq.Name == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
color := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}{
|
||||||
|
Name: cReq.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve existing image path if available
|
||||||
|
if i < len(existingColors) {
|
||||||
|
color.ImagePath = existingColors[i].ImagePath
|
||||||
|
}
|
||||||
|
|
||||||
|
colorEntities = append(colorEntities, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(colorEntities) > 0 {
|
||||||
|
bytes, _ := json.Marshal(colorEntities)
|
||||||
|
str := string(bytes)
|
||||||
|
productEntity.Colors = &str
|
||||||
|
}
|
||||||
|
} else if existingProduct.Colors != nil {
|
||||||
|
// No colors in request, preserve existing colors
|
||||||
|
productEntity.Colors = existingProduct.Colors
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle specifications update (as JSON array like colors)
|
||||||
|
specFiles := form.File["specification_images"]
|
||||||
|
specificationsStr := c.FormValue("specifications")
|
||||||
|
|
||||||
|
var specEntities []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if specificationsStr != "" {
|
||||||
|
var specifications []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImageCount int `json:"imageCount"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(specificationsStr), &specifications); err == nil {
|
||||||
|
// Get existing specifications to preserve existing images if no new files uploaded
|
||||||
|
var existingSpecs []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}
|
||||||
|
if existingProduct.Specifications != nil && *existingProduct.Specifications != "" {
|
||||||
|
_ = json.Unmarshal([]byte(*existingProduct.Specifications), &existingSpecs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileIndex := 0
|
||||||
|
for i, spec := range specifications {
|
||||||
|
if spec.Title == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
specEntity := struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}{
|
||||||
|
Title: spec.Title,
|
||||||
|
ImagePaths: []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are new files uploaded for this spec index, use them
|
||||||
|
imageCount := spec.ImageCount
|
||||||
|
if imageCount > 0 && fileIndex < len(specFiles) {
|
||||||
|
// Upload new files
|
||||||
|
for j := 0; j < imageCount && fileIndex < len(specFiles); j++ {
|
||||||
|
fileHeader := specFiles[fileIndex]
|
||||||
|
path, uploadErr := _i.uploadSpecFile(fileHeader)
|
||||||
|
if uploadErr == nil && path != nil {
|
||||||
|
specEntity.ImagePaths = append(specEntity.ImagePaths, *path)
|
||||||
|
}
|
||||||
|
fileIndex++
|
||||||
|
}
|
||||||
|
} else if i < len(existingSpecs) {
|
||||||
|
// No new files, preserve existing image paths
|
||||||
|
specEntity.ImagePaths = existingSpecs[i].ImagePaths
|
||||||
|
}
|
||||||
|
|
||||||
|
specEntities = append(specEntities, specEntity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if existingProduct.Specifications != nil {
|
||||||
|
// No specifications in request, preserve existing
|
||||||
|
productEntity.Specifications = existingProduct.Specifications
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save specifications as JSON string
|
||||||
|
if len(specEntities) > 0 {
|
||||||
|
bytes, _ := json.Marshal(specEntities)
|
||||||
|
str := string(bytes)
|
||||||
|
productEntity.Specifications = &str
|
||||||
|
}
|
||||||
|
|
||||||
err = _i.Repo.Update(id, productEntity)
|
err = _i.Repo.Update(id, productEntity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
@ -310,25 +647,90 @@ 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 {
|
||||||
|
// Search in Colors
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search in Specifications
|
||||||
|
if !found && product.Specifications != nil && *product.Specifications != "" {
|
||||||
|
var rawSpecs []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
ImagePaths []string `json:"image_paths"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(*product.Specifications), &rawSpecs); err == nil {
|
||||||
|
for _, spec := range rawSpecs {
|
||||||
|
for _, imagePath := range spec.ImagePaths {
|
||||||
|
if strings.Contains(imagePath, filename) {
|
||||||
|
objectName = imagePath
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
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").
|
||||||
|
|
@ -407,14 +809,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")
|
||||||
}
|
}
|
||||||
|
|
@ -459,14 +861,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")
|
||||||
}
|
}
|
||||||
|
|
@ -509,7 +911,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,18 +2,21 @@ package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"jaecoo-be/app/middleware"
|
||||||
"jaecoo-be/app/module/sales_agents/request"
|
"jaecoo-be/app/module/sales_agents/request"
|
||||||
"jaecoo-be/app/module/sales_agents/service"
|
"jaecoo-be/app/module/sales_agents/service"
|
||||||
"jaecoo-be/utils/paginator"
|
"jaecoo-be/utils/paginator"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
utilRes "jaecoo-be/utils/response"
|
utilRes "jaecoo-be/utils/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
type salesAgentsController struct {
|
type salesAgentsController struct {
|
||||||
salesAgentsService service.SalesAgentsService
|
salesAgentsService service.SalesAgentsService
|
||||||
|
Log zerolog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type SalesAgentsController interface {
|
type SalesAgentsController interface {
|
||||||
|
|
@ -61,7 +64,13 @@ func (_i *salesAgentsController) All(c *fiber.Ctx) error {
|
||||||
req := reqContext.ToParamRequest()
|
req := reqContext.ToParamRequest()
|
||||||
req.Pagination = paginate
|
req.Pagination = paginate
|
||||||
|
|
||||||
agentsData, paging, err := _i.salesAgentsService.GetAll(req)
|
clientId := middleware.GetClientID(c)
|
||||||
|
|
||||||
|
// ✅ ambil Authorization token (kalau dibutuhkan)
|
||||||
|
|
||||||
|
_i.Log.Info().Interface("clientId", clientId).Msg("")
|
||||||
|
|
||||||
|
agentsData, paging, err := _i.salesAgentsService.GetAll(clientId,req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"jaecoo-be/utils/paginator"
|
"jaecoo-be/utils/paginator"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -16,7 +17,7 @@ type salesAgentsRepository struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SalesAgentsRepository interface {
|
type SalesAgentsRepository interface {
|
||||||
GetAll(req request.SalesAgentsQueryRequest) (agents []*entity.SalesAgents, paging paginator.Pagination, err error)
|
GetAll(clientId *uuid.UUID, req request.SalesAgentsQueryRequest) (agents []*entity.SalesAgents, paging paginator.Pagination, err error)
|
||||||
FindOne(id uint) (agent *entity.SalesAgents, err error)
|
FindOne(id uint) (agent *entity.SalesAgents, err error)
|
||||||
Create(agent *entity.SalesAgents) (agentReturn *entity.SalesAgents, err error)
|
Create(agent *entity.SalesAgents) (agentReturn *entity.SalesAgents, err error)
|
||||||
Update(id uint, agent *entity.SalesAgents) (err error)
|
Update(id uint, agent *entity.SalesAgents) (err error)
|
||||||
|
|
@ -33,7 +34,7 @@ func NewSalesAgentsRepository(db *database.Database, log zerolog.Logger) SalesAg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *salesAgentsRepository) GetAll(req request.SalesAgentsQueryRequest) (agents []*entity.SalesAgents, paging paginator.Pagination, err error) {
|
func (_i *salesAgentsRepository) GetAll(clientId *uuid.UUID, req request.SalesAgentsQueryRequest) (agents []*entity.SalesAgents, paging paginator.Pagination, err error) {
|
||||||
var count int64
|
var count int64
|
||||||
|
|
||||||
query := _i.DB.DB.Model(&entity.SalesAgents{})
|
query := _i.DB.DB.Model(&entity.SalesAgents{})
|
||||||
|
|
@ -47,6 +48,10 @@ func (_i *salesAgentsRepository) GetAll(req request.SalesAgentsQueryRequest) (ag
|
||||||
query = query.Where("job_title = ?", *req.JobTitle)
|
query = query.Where("job_title = ?", *req.JobTitle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if clientId != nil {
|
||||||
|
query = query.Where("client_id = ?", clientId)
|
||||||
|
}
|
||||||
|
|
||||||
if req.AgentType != nil && *req.AgentType != "" {
|
if req.AgentType != nil && *req.AgentType != "" {
|
||||||
query = query.Where("agent_type LIKE ?", "%"+*req.AgentType+"%")
|
query = query.Where("agent_type LIKE ?", "%"+*req.AgentType+"%")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
@ -37,7 +38,7 @@ type salesAgentsService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SalesAgentsService interface {
|
type SalesAgentsService interface {
|
||||||
GetAll(req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error)
|
GetAll(clientId *uuid.UUID, req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error)
|
||||||
GetOne(id uint) (agent *response.SalesAgentsResponse, err error)
|
GetOne(id uint) (agent *response.SalesAgentsResponse, err error)
|
||||||
Create(c *fiber.Ctx, req request.SalesAgentsCreateRequest) (agent *response.SalesAgentsResponse, err error)
|
Create(c *fiber.Ctx, req request.SalesAgentsCreateRequest) (agent *response.SalesAgentsResponse, err error)
|
||||||
Update(c *fiber.Ctx, id uint, req request.SalesAgentsUpdateRequest) (agent *response.SalesAgentsResponse, err error)
|
Update(c *fiber.Ctx, id uint, req request.SalesAgentsUpdateRequest) (agent *response.SalesAgentsResponse, err error)
|
||||||
|
|
@ -60,8 +61,8 @@ func NewSalesAgentsService(repo repository.SalesAgentsRepository, log zerolog.Lo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *salesAgentsService) GetAll(req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error) {
|
func (_i *salesAgentsService) GetAll(clientId *uuid.UUID, req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error) {
|
||||||
agentsEntity, paging, err := _i.Repo.GetAll(req)
|
agentsEntity, paging, err := _i.Repo.GetAll(clientId,req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -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
|
||||||
|
|
|
||||||
BIN
jaecoo-be.exe
BIN
jaecoo-be.exe
Binary file not shown.
Loading…
Reference in New Issue