Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions controller/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,33 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

func GetAllCompanies(mongikClient *mongikModels.Mongik, noCache bool) ([]model.Company, error) {
companies, err := db.Aggregate[model.Company](mongikClient, constants.DB, constants.COLLECTION_COMPANY, []bson.M{}, noCache)
func GetAllCompanies(mongikClient *mongikModels.Mongik, noCache bool, currentPage int, companiesPerPage int) ([]model.Company, int, error) {

return companies, err
var pipeline []bson.M

pipeline = []bson.M{
{"$sort": bson.M{"createdAt": -1}},
}

if currentPage != 0 {
skip := (currentPage - 1) * companiesPerPage
limit := companiesPerPage

pipeline = []bson.M{
{"$sort": bson.M{"createdAt": -1}},
{"$skip": skip},
{"$limit": limit},
}
}

totalCompaniesArray, err := db.Aggregate[map[string]int](mongikClient, constants.DB, constants.COLLECTION_COMPANY, []bson.M{{"$count": "total"}}, noCache)
if err != nil {
return nil, 0, err
}

totalCompanies := totalCompaniesArray[0]["total"]

companies, err := db.Aggregate[model.Company](mongikClient, constants.DB, constants.COLLECTION_COMPANY, pipeline, noCache)

return companies, totalCompanies, err
}
10 changes: 8 additions & 2 deletions handler/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"net/http"
"strconv"

"github.com/FrosTiK-SD/auth/constants"
"github.com/FrosTiK-SD/auth/controller"
Expand All @@ -11,8 +12,12 @@ import (

func (h *Handler) GetAllCompanies(ctx *gin.Context) {
noCache := util.GetNoCache(ctx)
currentPage := 0

companies, err := controller.GetAllCompanies(h.MongikClient, noCache)
currentPage, err := strconv.Atoi(ctx.Request.URL.Query().Get("page"))
companiesPerPage, err := strconv.Atoi(ctx.Request.URL.Query().Get("limit"))

companies, totalCompanies, err := controller.GetAllCompanies(h.MongikClient, noCache, currentPage, companiesPerPage)

if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
Expand All @@ -23,6 +28,7 @@ func (h *Handler) GetAllCompanies(ctx *gin.Context) {
}

ctx.JSON(http.StatusOK, gin.H{
"data": companies,
"data": companies,
"totalCompanies": totalCompanies,
})
}