fix: update fixing get all article & categories

This commit is contained in:
hanif salafi 2025-10-12 23:51:43 +07:00
parent 142f048591
commit 0a2991d618
4 changed files with 32 additions and 22 deletions

View File

@ -2,19 +2,22 @@ package repository
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"github.com/rs/zerolog"
"netidhub-saas-be/app/database" "netidhub-saas-be/app/database"
"netidhub-saas-be/app/database/entity" "netidhub-saas-be/app/database/entity"
"netidhub-saas-be/app/module/article_categories/request" "netidhub-saas-be/app/module/article_categories/request"
"netidhub-saas-be/config/config"
"netidhub-saas-be/utils/paginator" "netidhub-saas-be/utils/paginator"
utilSvc "netidhub-saas-be/utils/service" utilSvc "netidhub-saas-be/utils/service"
"strings" "strings"
"github.com/google/uuid"
"github.com/rs/zerolog"
) )
type articleCategoriesRepository struct { type articleCategoriesRepository struct {
DB *database.Database DB *database.Database
Log zerolog.Logger Log zerolog.Logger
Cfg *config.Config
} }
// ArticleCategoriesRepository define interface of IArticleCategoriesRepository // ArticleCategoriesRepository define interface of IArticleCategoriesRepository
@ -28,10 +31,11 @@ type ArticleCategoriesRepository interface {
Delete(clientId *uuid.UUID, id uint) (err error) Delete(clientId *uuid.UUID, id uint) (err error)
} }
func NewArticleCategoriesRepository(db *database.Database, log zerolog.Logger) ArticleCategoriesRepository { func NewArticleCategoriesRepository(db *database.Database, log zerolog.Logger, cfg *config.Config) ArticleCategoriesRepository {
return &articleCategoriesRepository{ return &articleCategoriesRepository{
DB: db, DB: db,
Log: log, Log: log,
Cfg: cfg,
} }
} }
@ -42,7 +46,7 @@ func (_i *articleCategoriesRepository) GetAll(clientId *uuid.UUID, req request.A
query := _i.DB.DB.Model(&entity.ArticleCategories{}) query := _i.DB.DB.Model(&entity.ArticleCategories{})
// Add ClientId filter // Add ClientId filter
if clientId != nil { if clientId != nil && _i.Cfg.App.PrimaryClientKey != clientId.String() {
query = query.Where("article_categories.client_id = ?", clientId) query = query.Where("article_categories.client_id = ?", clientId)
} }

View File

@ -6,6 +6,7 @@ import (
"netidhub-saas-be/app/database/entity" "netidhub-saas-be/app/database/entity"
"netidhub-saas-be/app/module/articles/request" "netidhub-saas-be/app/module/articles/request"
"netidhub-saas-be/app/module/articles/response" "netidhub-saas-be/app/module/articles/response"
"netidhub-saas-be/config/config"
"netidhub-saas-be/utils/paginator" "netidhub-saas-be/utils/paginator"
utilSvc "netidhub-saas-be/utils/service" utilSvc "netidhub-saas-be/utils/service"
"strings" "strings"
@ -17,6 +18,7 @@ import (
type articlesRepository struct { type articlesRepository struct {
DB *database.Database DB *database.Database
Cfg *config.Config
Log zerolog.Logger Log zerolog.Logger
} }
@ -36,10 +38,11 @@ type ArticlesRepository interface {
ArticleMonthlyStats(clientId *uuid.UUID, userLevelId *uint, levelNumber *int, year int) (articleMontlyStats []*response.ArticleMonthlyStats, err error) ArticleMonthlyStats(clientId *uuid.UUID, userLevelId *uint, levelNumber *int, year int) (articleMontlyStats []*response.ArticleMonthlyStats, err error)
} }
func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRepository { func NewArticlesRepository(db *database.Database, log zerolog.Logger, cfg *config.Config) ArticlesRepository {
return &articlesRepository{ return &articlesRepository{
DB: db, DB: db,
Log: log, Log: log,
Cfg: cfg,
} }
} }
@ -48,9 +51,9 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, use
var count int64 var count int64
query := _i.DB.DB.Model(&entity.Articles{}) query := _i.DB.DB.Model(&entity.Articles{})
// Add client filter // Add client filter
if clientId != nil {
if clientId != nil && _i.Cfg.App.PrimaryClientKey != clientId.String() {
query = query.Where("client_id = ?", clientId) query = query.Where("client_id = ?", clientId)
} }

View File

@ -1,29 +1,31 @@
package config package config
import ( import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/pelletier/go-toml/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"time" "time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/pelletier/go-toml/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
) )
type app = struct { type app = struct {
Name string `toml:"name"` Name string `toml:"name"`
Host string `toml:"host"` Host string `toml:"host"`
Domain string `toml:"domain"` Domain string `toml:"domain"`
Port string `toml:"port"` Port string `toml:"port"`
ExternalPort string `toml:"external-port"` ExternalPort string `toml:"external-port"`
PrintRoutes bool `toml:"print-routes"` PrintRoutes bool `toml:"print-routes"`
Prefork bool `toml:"prefork"` Prefork bool `toml:"prefork"`
Production bool `toml:"production"` Production bool `toml:"production"`
IdleTimeout time.Duration `toml:"idle-timeout"` IdleTimeout time.Duration `toml:"idle-timeout"`
BodyLimit int `toml:"body-limit"` BodyLimit int `toml:"body-limit"`
PrimaryClientKey string `toml:"primary-client-key"`
} }
// db struct config // db struct config

View File

@ -10,6 +10,7 @@ print-routes = false
prefork = true prefork = true
production = false production = false
body-limit = 1048576000 # "100 * 1024 * 1024" body-limit = 1048576000 # "100 * 1024 * 1024"
primary-client-key = "78356d32-52fa-4dfc-b836-6cebf4e3eead"
[db.postgres] [db.postgres]
dsn = "postgresql://netidhub_user:NetidhubDB@2025@38.47.180.165:5432/netidhub_db" # <driver>://<username>:<password>@<host>:<port>/<database> dsn = "postgresql://netidhub_user:NetidhubDB@2025@38.47.180.165:5432/netidhub_db" # <driver>://<username>:<password>@<host>:<port>/<database>