Пример #1
0
func ParseSearch(u *url.URL) (brew.Search, error, []string) {
	args := u.Query()
	search := brew.Search{}

	funcs := []func(*brew.Search, url.Values) error{
		parseMulticolor,
		parseRarity,
		parseTypes,
		parseSupertypes,
		parseColors,
		parseSubtypes,
		parseFormat,
		parseStatus,
		parseMultiverseIDs,
		parseSets,
		parseName,
		parseRules,
		parsePaging,
	}

	var err error
	results := []string{}

	for _, fun := range funcs {
		if e := fun(&search, args); e != nil {
			results = append(results, e.Error())
			err = fmt.Errorf("Errors while processing the search")
		}
	}

	// By default, include 100 cards
	search.Limit = 100

	return search, err, results
}
Пример #2
0
func parseMulticolor(s *brew.Search, args url.Values) error {
	switch args.Get("multicolor") {
	case "true":
		s.IncludeMulticolor = true
		s.Multicolor = true
	case "false":
		s.IncludeMulticolor = true
		s.Multicolor = false
	case "":
		s.IncludeMulticolor = false
		return nil
	default:
		return fmt.Errorf("Multicolor should be either 'true' or 'false'")
	}
	return nil
}
Пример #3
0
func parseMultiverseIDs(s *brew.Search, args url.Values) error {
	ids := args["multiverseid"][:]
	for _, m := range args["m"] {
		ids = append(ids, m)
	}
	s.MultiverseIDs = ids
	return nil
}
Пример #4
0
func parsePaging(s *brew.Search, args url.Values) error {
	s.Limit = 100

	pagenum := args.Get("page")
	if pagenum == "" {
		return nil
	}

	page, err := strconv.Atoi(pagenum)
	if err != nil {
		return err
	}

	if page < 0 {
		return fmt.Errorf("Page parameter must be >= 0")
	}

	s.Page = page
	s.Offset = s.Page * s.Limit

	return nil
}
Пример #5
0
// TODO: Add validation
func parseSets(s *brew.Search, args url.Values) error {
	s.Sets = toLower(args["set"])
	return nil
}
Пример #6
0
// TODO: Add validation
func parseSubtypes(s *brew.Search, args url.Values) error {
	s.Subtypes = toLower(args["subtype"])
	return nil
}