Skip to content

Commit

Permalink
Merge branch 'main' into feature/Responsive-borne-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptTF committed Jun 4, 2024
2 parents 801e4fb + e197b7f commit ce54099
Show file tree
Hide file tree
Showing 21 changed files with 2,663 additions and 219 deletions.
46 changes: 46 additions & 0 deletions backend/api/course.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import (
"bar/autogen"

"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)

// (GET /course)
func (s *Server) GetCourse(c echo.Context, params autogen.GetCourseParams) error {
// Get admin account from cookie
_, err := MustGetAdmin(c)
if err != nil {
return nil
}
search := ""
if params.Fournisseur != nil {
search = *params.Fournisseur
}
var course []autogen.CourseItem

data, err := s.DBackend.GetItems(c.Request().Context(), "", 0, 0, "", "", search)
if err != nil {
logrus.Error(err)
return Error500(c)
}

for _, item := range data {
var amount_needed = item.OptimalAmount - item.AmountLeft
if item.OptimalAmount > item.AmountLeft && item.AmountPerBundle != nil {
amountToBuy := amount_needed / *item.AmountPerBundle + (amount_needed%*item.AmountPerBundle)*2 / *item.AmountPerBundle
if amountToBuy > 0 {
course = append(course, autogen.CourseItem{
AmountToBuy: amountToBuy,
Item: item.Item,
})
}
}
}

autogen.GetCourse200JSONResponse{
Items: course,
}.VisitGetCourseResponse(c.Response())
return nil
}
83 changes: 79 additions & 4 deletions backend/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func (s *Server) GetCategoryItems(c echo.Context, categoryId autogen.UUID, param
return Error500(c)
}

count, err := s.DBackend.CountItems(c.Request().Context(), categoryId.String(), state, "")
count, err := s.DBackend.CountItems(c.Request().Context(), categoryId.String(), state, "", "")
if err != nil {
return Error500(c)
}

// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetItems(c.Request().Context(), categoryId.String(), dbpage, limit, state, "")
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId.String(), dbpage, limit, state, "", "")
if err != nil {
return Error500(c)
}
Expand Down Expand Up @@ -174,6 +174,15 @@ func (s *Server) PatchItem(c echo.Context, categoryId autogen.UUID, itemId autog
item.BuyLimit = &buyLimit
}
}
if p.AmountPerBundle != nil {
item.AmountPerBundle = p.AmountPerBundle
}
if p.RefBundle != nil {
item.RefBundle = p.RefBundle
}
if p.Fournisseur != nil {
item.Fournisseur = p.Fournisseur
}

rp := item.RealPrices()
item.DisplayPrices = &rp
Expand Down Expand Up @@ -238,6 +247,7 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e
state := ""
categoryId := ""
name := ""
fournisseur := ""
if params.State != nil {
state = string(*params.State)
}
Expand All @@ -247,8 +257,11 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e
if params.Name != nil {
name = string(*params.Name)
}
if params.Fournisseur != nil {
fournisseur = string(*params.Fournisseur)
}

count, err := s.DBackend.CountItems(c.Request().Context(), categoryId, state, name)
count, err := s.DBackend.CountItems(c.Request().Context(), categoryId, state, name, fournisseur)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand All @@ -257,7 +270,7 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e
// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetItems(c.Request().Context(), categoryId, dbpage, limit, state, name)
data, err := s.DBackend.GetItems(c.Request().Context(), categoryId, dbpage, limit, state, name, fournisseur)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand Down Expand Up @@ -286,3 +299,65 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e

return nil
}

// (GET /items/incoherent)
func (s *Server) GetAllIncoherentItems(c echo.Context, params autogen.GetAllIncoherentItemsParams) error {
// Get account from cookie
account, err := MustGetAdmin(c)
if err != nil {
return nil
}

state := ""
categoryId := ""
name := ""
if params.State != nil {
state = string(*params.State)
}
if params.CategoryId != nil {
categoryId = params.CategoryId.String()
}
if params.Name != nil {
name = string(*params.Name)
}

count, err := s.DBackend.CountIncoherentItems(c.Request().Context(), categoryId, state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
}

// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetIncoherentItems(c.Request().Context(), dbpage, limit, categoryId, state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
}



var items []autogen.Item

for _, item := range data {
rp := item.RealPrice(account.PriceRole)
item.DisplayPrice = &rp

if account.HasPrivileges() {
rp := item.RealPrices()
item.DisplayPrices = &rp
}

items = append(items, item.Item)
}

autogen.GetAllIncoherentItems200JSONResponse{
Items: items,
Page: page,
Limit: limit,
MaxPage: maxPage,
}.VisitGetAllIncoherentItemsResponse(c.Response())

return nil
}
Loading

0 comments on commit ce54099

Please sign in to comment.