public void Post() { var input = (new StreamReader(Request.Body)).ReadToEnd(); if (!_templateService.IsValid(input)) { Response.StatusCode = 400; } else if (_storeService.SaveTemplate(_templateService.Clean(input))) { Response.StatusCode = 201; } else { Response.StatusCode = 500; } }
public Template(string template, ITemplateService templateService) { _templateService = templateService; _templateService.AssertValidity(template); _hasNeighborhoods = Regex.Matches(template, TemplateVariables.NeighborhoodLoop).Count == 1; template = _templateService.Clean(template); _templateString = template; // The for loops are the separators. var parts = SplitTemplateIntoParts(template); PreparePartsToBeTransformedInRegex(parts); _neighborhoodRegex = CreateNeighborHoodRegex(parts); _cityRegexes = CreateCityRegexes(parts); }
public City[] ExtractCities(string input) { input = _templateService.Clean(input); if (_hasNeighborhoods) { var preNeighborhoodMatches = _cityRegexes[0] != null ? _cityRegexes[0].Matches(input) : null; var postNeighborhoodMatches = _cityRegexes[1] != null ? _cityRegexes[1].Matches(input) : null; if (preNeighborhoodMatches != null && postNeighborhoodMatches != null && preNeighborhoodMatches.Count != postNeighborhoodMatches.Count) { throw new InvalidOperationException("Wrong template."); } var totalCityMatches = preNeighborhoodMatches != null ? preNeighborhoodMatches.Count : postNeighborhoodMatches.Count; var cities = new City[totalCityMatches]; for (int i = 0; i < cities.Length; i++) { cities[i] = GetCity( preNeighborhoodMatches != null ? preNeighborhoodMatches[i] : null, postNeighborhoodMatches != null ? postNeighborhoodMatches[i] : null ); int cityStartIdx; int?cityEndIdx; if (preNeighborhoodMatches == null) { if (i > 0) { cityStartIdx = postNeighborhoodMatches[i - 1].Index; } else { cityStartIdx = 0; } cityEndIdx = postNeighborhoodMatches[i].Index + postNeighborhoodMatches[i].Length; } else if (postNeighborhoodMatches == null) { cityStartIdx = preNeighborhoodMatches[i].Index; if (i + 1 < cities.Length) { cityEndIdx = preNeighborhoodMatches[i + 1].Index; } else { cityEndIdx = null; } } else { cityStartIdx = preNeighborhoodMatches[i].Index; cityEndIdx = postNeighborhoodMatches[i].Index + postNeighborhoodMatches[i].Length; } var nbStr = cityEndIdx.HasValue ? input.Substring(cityStartIdx, cityEndIdx.Value - cityStartIdx) : input.Substring(cityStartIdx); var nbMatches = _neighborhoodRegex.Matches(nbStr); var neighborhoods = GetNeighborhoods(nbMatches); foreach (var neighborhood in neighborhoods) { cities[i].AddNeighborhood(neighborhood); } } return(cities); } else { return(ExtractCities(input, _cityRegexes[0])); } }