Skip to content

Commit 53a9ed7

Browse files
authored
Merge pull request #143 from RemiF1908/feature/sort-out-of-stock-last
feat(front): items oos displayed at end
2 parents 018afa5 + ff62552 commit 53a9ed7

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

backend/api/course.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (s *Server) GetCourse(c echo.Context, params autogen.GetCourseParams) error
2020
}
2121
var course []autogen.CourseItem
2222

23-
data, err := s.DBackend.GetItems(c.Request().Context(), "", 0, 0, "", "", search)
23+
data, err := s.DBackend.GetItems(c.Request().Context(), "", 0, 0, "", "", search, false)
2424
if err != nil {
2525
logrus.Error(err)
2626
return Error500(c)

backend/api/items.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *Server) GetCategoryItems(c echo.Context, categoryId autogen.UUID, param
4343
// Make sure the last page is not empty
4444
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)
4545

46-
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId.String(), dbpage, limit, state, "", "")
46+
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId.String(), dbpage, limit, state, "", "", true)
4747
if err != nil {
4848
return Error500(c)
4949
}
@@ -270,7 +270,7 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e
270270
// Make sure the last page is not empty
271271
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)
272272

273-
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId, dbpage, limit, state, name, fournisseur)
273+
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId, dbpage, limit, state, name, fournisseur, false)
274274
if err != nil {
275275
logrus.Error(err)
276276
return Error500(c)

backend/internal/db/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ type DBackend interface {
173173
CountAccounts(ctx context.Context, query string) (uint64, error)
174174
GetRefills(ctx context.Context, account string, page uint64, size uint64, startAt, endAt uint64) ([]*models.Refill, error)
175175
CountRefills(ctx context.Context, account string, startAt, endAt uint64) (uint64, error)
176-
GetItems(ctx context.Context, categoryID string, page, size uint64, state string, name string, fournisseur string) ([]*models.Item, error)
176+
GetItems(ctx context.Context, categoryID string, page, size uint64, state string, name string, fournisseur string, sort bool) ([]*models.Item, error)
177177
GetIncoherentItems(ctx context.Context, page, size uint64, categoryID string, state string, name string) ([]*models.Item, error)
178178
CountItems(ctx context.Context, categoryID string, state string, name string, fournisseur string) (uint64, error)
179179
CountIncoherentItems(ctx context.Context, categoryID string, state string, name string) (uint64, error)

backend/internal/db/mongo/item_misc.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
)
1313

14-
func (b *Backend) GetItems(ctx context.Context, categoryID string, page, size uint64, state string, name string, fournisseur string) ([]*models.Item, error) {
14+
func (b *Backend) GetItems(ctx context.Context, categoryID string, page, size uint64, state string, name string, fournisseur string, sort bool) ([]*models.Item, error) {
1515
ctx, cancel := b.TimeoutContext(ctx)
1616
defer cancel()
1717

1818
var items []*models.Item
1919

20-
filter := bson.M{
20+
matchFilter := bson.M{
2121
"$or": []bson.M{
2222
{
2323
"deleted_at": bson.M{
@@ -30,12 +30,12 @@ func (b *Backend) GetItems(ctx context.Context, categoryID string, page, size ui
3030
},
3131
}
3232
if state != "" {
33-
filter["state"] = state
33+
matchFilter["state"] = state
3434
if state == string(autogen.ItemBuyable) {
35-
// Get seconds since day start
35+
// Get seconds since day start
3636
t := time.Since(time.Now().Truncate(24 * time.Hour)).Seconds()
3737
// available_from <= t <= available_until or (available_from == nil && available_until == nil)
38-
filter["$and"] = []bson.M{
38+
matchFilter["$and"] = []bson.M{
3939
{
4040
"$or": []bson.M{
4141
{
@@ -64,19 +64,51 @@ func (b *Backend) GetItems(ctx context.Context, categoryID string, page, size ui
6464
}
6565
}
6666
if categoryID != "" {
67-
filter["category_id"] = uuid.MustParse(categoryID)
67+
matchFilter["category_id"] = uuid.MustParse(categoryID)
6868
}
6969
if name != "" {
70-
filter["name"] = bson.M{
70+
matchFilter["name"] = bson.M{
7171
"$regex": name,
7272
"$options": "i",
7373
}
7474
}
7575
if fournisseur != "" {
76-
filter["fournisseur"] = fournisseur
76+
matchFilter["fournisseur"] = fournisseur
7777
}
7878

79-
cursor, err := b.db.Collection(ItemsCollection).Find(ctx, filter, options.Find().SetSkip(int64(page*size)).SetLimit(int64(size)))
79+
pipeline := []bson.M{
80+
{"$match": matchFilter},
81+
}
82+
83+
if sort {
84+
pipeline = append(pipeline, bson.M{
85+
"$addFields": bson.M{
86+
"sortPriority": bson.M{
87+
"$cond": bson.A{
88+
bson.M{"$eq": bson.A{"$amount_left", 0}},
89+
2,
90+
1,
91+
},
92+
},
93+
},
94+
})
95+
96+
pipeline = append(pipeline, bson.M{
97+
"$sort": bson.D{
98+
{"sortPriority", 1},
99+
},
100+
})
101+
}
102+
pipeline = append(pipeline, bson.M{"$skip": int64(page * size)})
103+
pipeline = append(pipeline, bson.M{"$limit": int64(size)})
104+
105+
if sort {
106+
pipeline = append(pipeline, bson.M{
107+
"$project": bson.M{"sortPriority": 0},
108+
})
109+
}
110+
111+
cursor, err := b.db.Collection(ItemsCollection).Aggregate(ctx, pipeline)
80112
if err != nil {
81113
return nil, err
82114
}

0 commit comments

Comments
 (0)