github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/api/models/search.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/ShoshinNikita/budget-manager/internal/db"
     8  )
     9  
    10  // SearchSpendsReq is used to search for spends
    11  type SearchSpendsReq struct {
    12  	BaseRequest
    13  
    14  	// Title can be in any case. Search will be performed by lowercased value
    15  	Title string `json:"title"`
    16  	// Notes can be in any case. Search will be performed by lowercased value
    17  	Notes string `json:"notes"`
    18  
    19  	// TitleExactly defines should we search exactly for the given title
    20  	TitleExactly bool `json:"title_exactly" default:"false"`
    21  	// NotesExactly defines should we search exactly for the given notes
    22  	NotesExactly bool `json:"notes_exactly" default:"false"`
    23  
    24  	// After must be in the RFC3339 format (https://tools.ietf.org/html/rfc3339#section-5.8)
    25  	After time.Time `json:"after" format:"date"`
    26  	// Before must be in the RFC3339 format (https://tools.ietf.org/html/rfc3339#section-5.8)
    27  	Before time.Time `json:"before" format:"date"`
    28  
    29  	MinCost float64 `json:"min_cost"`
    30  	MaxCost float64 `json:"max_cost"`
    31  
    32  	// TypeIDs is a list of Spend Type ids to search for. Use id '0' to search for Spends without type
    33  	TypeIDs []uint `json:"type_ids"`
    34  
    35  	// Sort specify field to sort by
    36  	Sort string `json:"sort" enums:"title,cost,date" default:"date"`
    37  	// Order specify sort order
    38  	Order string `json:"order" enums:"asc,desc" default:"asc"`
    39  }
    40  
    41  func (req *SearchSpendsReq) SanitizeAndCheck() error {
    42  	sanitizeString(&req.Title)
    43  	sanitizeString(&req.Notes)
    44  	sanitizeString(&req.Sort)
    45  	sanitizeString(&req.Order)
    46  
    47  	if req.MinCost != 0 && req.MaxCost != 0 && req.MinCost > req.MaxCost {
    48  		return fmt.Errorf("min_cost can't be greater than max_cost")
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  type SearchSpendsResp struct {
    55  	BaseResponse
    56  
    57  	Spends []db.Spend `json:"spends"`
    58  }