Skip to content

Commit

Permalink
Add filter to the stock page
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonophore committed Jan 30, 2024
1 parent 617e1ea commit cb64095
Show file tree
Hide file tree
Showing 26 changed files with 1,056 additions and 302 deletions.
61 changes: 60 additions & 1 deletion web/internal/api/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions web/internal/controller/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func (c *Controller) UpdateWarehouse(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, r)
}

func (c *Controller) GetDictionaries(ctx echo.Context) error {
dict, err := c.dictService.GetDictionary(ctx.Request().Context())
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, dict)
}

func (c *Controller) GetClusters(ctx echo.Context, params api.GetClustersParams) error {
values, err := c.store.GetClusters(ctx.Request().Context(), params.Filter)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions web/internal/controller/stock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"context"
"errors"
"fmt"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -29,11 +30,12 @@ type AuthService interface {
}

type IStockService interface {
GetStocks(stockDate time.Time, limit, offset int) (*api.StocksFull, error)
ExportStocks(writer http.ResponseWriter, stockDate time.Time) error
GetStocks(stockDate time.Time, limit, offset int, source *[]string, filter *string) (*api.StocksFull, error)
ExportStocks(writer http.ResponseWriter, stockDate time.Time, source *[]string, filter *string) error
}

type DictionaryService interface {
GetDictionary(ctx context.Context) (*api.Dictionaries, error)
GetPositions(offset int32, limit int32, source []string, filter *string) (*api.DictPositions, error)
ExportWarehouses(writer http.ResponseWriter, source []string, code *string, cluster *string) error
GetWarehouses(offset int32, limit int32, source []string, code *string, cluster *string) (*api.Warehouses, error)
Expand Down Expand Up @@ -69,7 +71,7 @@ func (c *Controller) ExportStocks(ctx echo.Context, params api.ExportStocksParam
ctx.Response().Header().Set(echo.HeaderContentType, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
ctx.Response().Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
ctx.Response().WriteHeader(http.StatusOK)
return c.stockService.ExportStocks(ctx.Response().Writer, params.Date.Time)
return c.stockService.ExportStocks(ctx.Response().Writer, params.Date.Time, params.Source, params.Filter)

}

Expand All @@ -91,7 +93,7 @@ func (c *Controller) GetStocks(ctx echo.Context, date string) error {
}

func (c *Controller) GetStocksWithPages(ctx echo.Context, params api.GetStocksWithPagesParams) error {
stocks, err := c.stockService.GetStocks(params.Date.Time, params.Limit, params.Offset)
stocks, err := c.stockService.GetStocks(params.Date.Time, params.Limit, params.Offset, params.Source, params.Filter)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions web/internal/service/dictionary/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type IDictionaryRepository interface {
GetMarketplaces(ctx context.Context) (*[]storage.Marketplace, error)
GetClusters(context context.Context, filter *string) (*[]string, error)
GetPositions(offset int32, limit int32, source []string, filter *string) (*[]storage.Position, error)
GetWarehousesWithoutPages(source []string, code *string, cluster *string) (*[]storage.Warehouse, error)
Expand All @@ -23,6 +24,24 @@ func NewDictionaryService(repository IDictionaryRepository) *DictionaryService {
}
}

func (s *DictionaryService) GetDictionary(ctx context.Context) (*api.Dictionaries, error) {
marketplaces, err := s.repository.GetMarketplaces(ctx)
if err != nil {
return nil, err
}
mp := make([]api.Marketplace, len(*marketplaces))
for i, item := range *marketplaces {
mp[i] = api.Marketplace{
Code: item.Code,
Name: item.Name,
ShortName: item.ShortName,
}
}
return &api.Dictionaries{
Marketplaces: mp,
}, nil
}

func (s *DictionaryService) GetPositions(offset int32, limit int32, source []string, filter *string) (*api.DictPositions, error) {
positions, err := s.repository.GetPositions(offset, limit, source, filter)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions web/internal/service/stock/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ var header []service.ExcelHeaderColumn = []service.ExcelHeaderColumn{
}

type IStockRepository interface {
GetSticksWithPage(stockDate time.Time, limit, offset int) (*[]storage.StockFull, error)
GetStocks(stockDate time.Time) (*[]storage.StockFull, error)
GetSticksWithPage(stockDate time.Time, limit, offset int, source *[]string, filter *string) (*[]storage.StockFull, error)
GetStocks(stockDate time.Time, source *[]string, filter *string) (*[]storage.StockFull, error)
}

type Service struct {
Expand All @@ -75,17 +75,17 @@ func NewStockService(repository IStockRepository) *Service {
}
}

func (s *Service) ExportStocks(writer http.ResponseWriter, stockDate time.Time) error {
stocks, err := s.repository.GetStocks(stockDate)
func (s *Service) ExportStocks(writer http.ResponseWriter, stockDate time.Time, source *[]string, filter *string) error {
stocks, err := s.repository.GetStocks(stockDate, source, filter)
if err != nil {
return err
}
return service.GenerateExcelDoc(writer, "Заказы", stocks, &header)
}

// Get stocks with the pagginations
func (s *Service) GetStocks(stockDate time.Time, limit, offset int) (*api.StocksFull, error) {
stocks, err := s.repository.GetSticksWithPage(stockDate, limit, offset)
func (s *Service) GetStocks(stockDate time.Time, limit, offset int, source *[]string, filter *string) (*api.StocksFull, error) {
stocks, err := s.repository.GetSticksWithPage(stockDate, limit, offset, source, filter)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions web/internal/storage/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package builder

type SQLBuilder struct {
}

func (s *SQLBuilder) Build() {

}
90 changes: 90 additions & 0 deletions web/internal/storage/builder/stock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package builder

import (
"fmt"
"time"
)

const sql_stocks_base = `select s.stock_date, s.source, o.name org, s.supplier_article, s.barcode, s.external_code sku,
s.name, s.brand, s.warehouse, s.quantity, s.price, s.price_with_discount
from bl.stock s
inner join ml.owner o on o.code = s.owner_code
where stock_date = @stock_date`
const sql_stock_where_source = ` and source in @source`
const sql_stock_where_filter = ` and (s.supplier_article like @filter or s.barcode like @filter or s.external_code like @filter or s.name like @filter or s.brand like @filter or s.warehouse like @filter)`

const sql_wrapper = `with t as (%s)
select
stock_date,
source,
org,
supplier_article,
barcode,
sku,
name,
brand,
warehouse,
quantity,
price,
price_with_discount,
(select count(1) from t) total
from t
limit @limit offset @offset`

type StockSQLBuilder struct {
sources *[]string
filter *string
stockSate time.Time
limit *int
offset *int
}

func NewStockSQLBuilder(stockDate time.Time) *StockSQLBuilder {
return &StockSQLBuilder{
stockSate: stockDate,
}
}

func (s *StockSQLBuilder) Sources(sources *[]string) *StockSQLBuilder {
s.sources = sources
return s
}

func (s *StockSQLBuilder) Filter(filter *string) *StockSQLBuilder {
s.filter = filter
return s
}

func (s *StockSQLBuilder) Limit(limit *int) *StockSQLBuilder {
s.limit = limit
return s
}

func (s *StockSQLBuilder) Offset(offset *int) *StockSQLBuilder {
s.offset = offset
return s
}

func (s *StockSQLBuilder) Build() (string, map[string]interface{}) {
var vars = make(map[string]interface{})
vars["stock_date"] = s.stockSate
query := sql_stocks_base
if s.sources != nil && len(*s.sources) != 0 {
vars["source"] = *s.sources
} else {
vars["source"] = []string{}
}
query += sql_stock_where_source
if s.filter != nil && len(*s.filter) != 0 {
str := *s.filter
vars["filter"] = "%" + str + "%"
query += sql_stock_where_filter
}
if s.limit != nil || s.offset != nil {
query = fmt.Sprintf(sql_wrapper, query)
vars["limit"] = *s.limit
vars["offset"] = *s.offset
}

return query, vars
}
20 changes: 20 additions & 0 deletions web/internal/storage/builder/stock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package builder

import (
"testing"
"time"
)

func TestStockBuilder(t *testing.T) {
builder := NewStockSQLBuilder(time.Now())

builder.Sources(&[]string{"1", "2"})
limit := 10
offset := 20
builder.Limit(&limit)
builder.Offset(&offset)
filter := "filter"
builder.Filter(&filter)
sql, _ := builder.Build()
print(sql)
}
20 changes: 20 additions & 0 deletions web/internal/storage/marketplace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package storage

import "context"

type Marketplace struct {
Code string `gorm:"column:code"`
Name string `gorm:"column:name"`
ShortName string `gorm:"column:short_name"`
}

func (s *Storage) GetMarketplaces(ctx context.Context) (*[]Marketplace, error) {
var marketplaces []Marketplace
tx := s.db.WithContext(ctx).Table("ml.marketplace").Scan(&marketplaces)
if tx.Error != nil {
return nil, tx.Error
}

return &marketplaces, nil

}
Loading

0 comments on commit cb64095

Please sign in to comment.