feat: site count
This commit is contained in:
parent
dfed117d90
commit
4013b12ac9
@ -28,10 +28,12 @@ type Site struct {
|
||||
}
|
||||
|
||||
type SiteSearch struct {
|
||||
Search string
|
||||
Name string
|
||||
Limit int
|
||||
Offset int
|
||||
PartnerID *int64
|
||||
IsAdmin bool
|
||||
Search string
|
||||
Name string
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
type SiteList []*SiteDB
|
||||
@ -143,3 +145,23 @@ func (o *SiteDB) SetDeleted(updatedBy int64) {
|
||||
o.DeletedAt = ¤tTime
|
||||
o.UpdatedBy = updatedBy
|
||||
}
|
||||
|
||||
type SiteCount struct {
|
||||
Count int `gorm:"type:int;column:count"`
|
||||
}
|
||||
|
||||
type SiteCountDB struct {
|
||||
SiteCount
|
||||
}
|
||||
|
||||
func (b *SiteCount) ToSiteCountDB() *SiteCountDB {
|
||||
return &SiteCountDB{
|
||||
SiteCount: *b,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *SiteCountDB) ToSiteCount() *SiteCount {
|
||||
return &SiteCount{
|
||||
Count: e.Count,
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route.PUT("/:id", jwt, h.Update)
|
||||
route.GET("/:id", jwt, h.GetByID)
|
||||
route.DELETE("/:id", jwt, h.Delete)
|
||||
route.GET("/count", jwt, h.Count)
|
||||
}
|
||||
|
||||
func NewHandler(service services.Site) *Handler {
|
||||
@ -148,7 +149,8 @@ func (h *Handler) GetAll(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
Sites, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity())
|
||||
ctx := request.GetMyContext(c)
|
||||
Sites, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity(ctx))
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
@ -242,6 +244,30 @@ func (h *Handler) GetByID(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) Count(c *gin.Context) {
|
||||
var req request.SiteParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.GetMyContext(c)
|
||||
res, err := h.service.Count(ctx, req.ToEntity(ctx))
|
||||
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: response.SiteCount{
|
||||
Count: res.Count,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) toSiteResponse(resp *entity.Site) response.Site {
|
||||
return response.Site{
|
||||
ID: &resp.ID,
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/entity"
|
||||
)
|
||||
|
||||
@ -65,8 +66,10 @@ type SiteParam struct {
|
||||
Offset int `form:"offset,default=0"`
|
||||
}
|
||||
|
||||
func (r *SiteParam) ToEntity() entity.SiteSearch {
|
||||
func (r *SiteParam) ToEntity(ctx mycontext.Context) entity.SiteSearch {
|
||||
return entity.SiteSearch{
|
||||
PartnerID: ctx.GetPartnerID(),
|
||||
IsAdmin: ctx.IsAdmin(),
|
||||
Search: r.Search,
|
||||
Name: r.Name,
|
||||
Limit: r.Limit,
|
||||
|
||||
@ -24,6 +24,10 @@ type SiteName struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type SiteCount struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type SiteList struct {
|
||||
Sites []Site `json:"sites"`
|
||||
Total int64 `json:"total"`
|
||||
|
||||
@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/repository/branches"
|
||||
"furtuna-be/internal/repository/brevo"
|
||||
mdtrns "furtuna-be/internal/repository/midtrans"
|
||||
@ -16,6 +17,7 @@ import (
|
||||
"furtuna-be/internal/repository/trx"
|
||||
"furtuna-be/internal/repository/users"
|
||||
repository "furtuna-be/internal/repository/wallet"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
@ -154,6 +156,7 @@ type SiteRepository interface {
|
||||
GetByID(ctx context.Context, id int64) (*entity.SiteDB, error)
|
||||
GetAll(ctx context.Context, req entity.SiteSearch) (entity.SiteList, int, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Count(ctx mycontext.Context, req entity.SiteSearch) (*entity.SiteCountDB, error)
|
||||
}
|
||||
|
||||
type TransactionManager interface {
|
||||
|
||||
@ -3,6 +3,7 @@ package sites
|
||||
import (
|
||||
"context"
|
||||
"furtuna-be/internal/common/logger"
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/entity"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@ -129,3 +130,21 @@ func (r *SiteRepository) Delete(ctx context.Context, id int64) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SiteRepository) Count(ctx mycontext.Context, req entity.SiteSearch) (*entity.SiteCountDB, error) {
|
||||
count := new(entity.SiteCountDB)
|
||||
|
||||
query := r.db.Table("sites").
|
||||
Select("count(*) as count")
|
||||
|
||||
if !req.IsAdmin {
|
||||
query = query.Where("partner_id = ?", req.PartnerID)
|
||||
}
|
||||
|
||||
if err := query.Scan(&count).Error; err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when get count sites", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
@ -122,4 +122,5 @@ type Site interface {
|
||||
GetByID(ctx context.Context, id int64) (*entity.Site, error)
|
||||
GetAll(ctx context.Context, search entity.SiteSearch) ([]*entity.Site, int, error)
|
||||
Delete(ctx mycontext.Context, id int64) error
|
||||
Count(ctx mycontext.Context, req entity.SiteSearch) (*entity.SiteCount, error)
|
||||
}
|
||||
|
||||
@ -87,3 +87,13 @@ func (s *SiteService) Delete(ctx mycontext.Context, id int64) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SiteService) Count(ctx mycontext.Context, req entity.SiteSearch) (*entity.SiteCount, error) {
|
||||
count, err := s.repo.Count(ctx, req)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when getting all sites", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return count.ToSiteCount(), nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user