public static void LoadWorld(HotelService hotelService, FactTypeService factTypeService, int maxHotels) { var world = Download <World>("http://api.thomascook.se/v-1/geography/world"); List <Hotel> hotels = new List <Hotel>(); FindHotels(hotels, world, maxHotels); Console.WriteLine("\n\nAdding FactTypes"); Dictionary <string, FactTypeModel> factTypeDictionary = new Dictionary <string, FactTypeModel>(); foreach (var fact in hotels.SelectMany(h => h.Facts)) { FactTypeModel model; if (!factTypeDictionary.TryGetValue(fact.Type, out model)) { model = factTypeService.GetFactType(fact.Type); if (model == null) { model = new FactTypeModel() { Code = fact.Type, Name = fact.Title }; var id = factTypeService.AddFactType(model); model.Id = id; } factTypeDictionary[fact.Type] = model; } } Console.WriteLine("\n\nAdding hotels"); foreach (var hotel in hotels) { Console.WriteLine("Adding hotel {0}", hotel.Name); var id = hotelService.AddHotel(hotel.Name, hotel.Description.Select(d => d.Text).FirstOrDefault(), hotel.ResortName, hotel.Images != null && hotel.Images.Main != null ? hotel.Images.Main.Select( i => i.OrderByDescending(ii => ii.Width).Select(ii => ii.Href).FirstOrDefault()) .FirstOrDefault() : null, hotel.Location != null ? hotel.Location.Latitude : 0, hotel.Location != null ? hotel.Location.Longitude : 0); Console.WriteLine("Setting facts for hotel {0}", hotel.Name); hotelService.SetHotelFacts(id, from f in hotel.Facts select new FactModel(factTypeDictionary[f.Type].Id, f.Type, f.Title, f.Value.Trim() != "-" && f.Value.Trim().ToLower() != "false", f.Value.ToLower() == "false" || f.Value.ToLower() == "true" ? string.Empty : f.Value)); } }
public FactTypeApiModule(IFactTypeService factTypeService) : base("/api/facttype") { Get["/"] = _ => { if (Request.Query["q"] == null) { return(Response.AsJson(factTypeService.ListFactTypes())); } return(FormatterExtensions.AsJson(Response, factTypeService.FindFactTypes(Request.Query["q"]))); }; Post["/"] = Post["/{factTypeId}"] = _ => { FactTypeModel model = this.Bind(); int factTypeId = model.Id; if (factTypeId == 0 && Context.Parameters["factTypeId"] != null) { factTypeId = Context.Parameters["factTypeId"] ?? 0; } try { if (factTypeId == 0) { factTypeId = factTypeService.AddFactType(model); } else { factTypeService.UpdateFactType(model); } return(Response.AsJson(new { FactTypeId = factTypeId })); } catch (InvalidOperationException ex) { var response = Response.AsJson(new { ex.Message }); return(response.StatusCode = HttpStatusCode.NotFound); } }; }