public object Get(GetListing request) { var listing = new Listing(); using (IDbConnection db = DbFactory.Open()) { listing = db.Select<Listing>().Where(ar => (ar.Id == request.Id || request.Id == null)).First(); } // add categories var categoryService = this.ResolveService<CategoryServices>(); var categores = ((GetCategoriesResponse)categoryService.Get(new GetCategories())).Result; listing.Category = categores.First(ar => ar.Id == listing.CategoryId); //build page hierarchy var pageHierarchy = new List<PageHierarchy>(); int rank = 0; //if (request.CategoryId != null) { var level = categores.First(ar => ar.Id == listing.CategoryId); pageHierarchy.Add(new PageHierarchy(rank, new Link { DisplayName = level.Name, Type = "Breadcrumb", Url = (new GetListings { CategoryId = level.Id }).ToGetUrl() })); bool hasParent = level.ParentCategoryId.HasValue; while (hasParent) { rank++; level = categores.First(ar => ar.Id == level.ParentCategoryId); pageHierarchy.Add(new PageHierarchy(rank, new Link { DisplayName = level.Name, Type = "Breadcrumb", Url = (new GetListings { CategoryId = level.Id }).ToGetUrl() })); hasParent = level.ParentCategoryId.HasValue; } } if (request.Id != null) pageHierarchy.Add(new PageHierarchy(rank, new Link { DisplayName = listing.Title, Type = "Breadcrumb", Url = "" })); return new GetListingResponse { PageHierarchy = pageHierarchy.OrderByDescending(ar => ar.Rank).ToList(), Result = listing }; }
/// <summary> /// Application specific configuration /// This method should initialize any IoC resources utilized by your web service classes. /// </summary> /// <param name="container"></param> public override void Configure(Container container) { //Config examples //this.Plugins.Add(new PostmanFeature()); //this.Plugins.Add(new CorsFeature()); this.Plugins.Add(new RazorFormat()); // register Db var dbFactory = new OrmLiteConnectionFactory( ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString, PostgreSqlDialect.Provider); container.Register<IDbConnectionFactory>(c => dbFactory); using (IDbConnection db = container.Resolve<IDbConnectionFactory>().Open()) { db.DropAndCreateTable<Listing>(); db.DropAndCreateTable<Category>(); List<Category> categories = new List<Category>(); categories.Add(new Category { Id = 1, Name = "Houses and flats" }); categories.Add(new Category { Id = 2, Name = "Houses for sale", ParentCategoryId = 1 }); categories.Add(new Category { Id = 3, Name = "Flats for sale", ParentCategoryId = 1 }); categories.Add(new Category { Id = 4, Name = "Houses for rent", ParentCategoryId = 1 }); categories.Add(new Category { Id = 5, Name = "Flats for rent", ParentCategoryId = 1 }); categories.Add(new Category { Id = 6, Name = "Vehicles" }); categories.Add(new Category { ParentCategoryId = 6, Name = "Cars for sale" }); categories.Add(new Category { ParentCategoryId = 6, Name = "Motorbikes for sale" }); categories.Add(new Category { ParentCategoryId = 6, Name = "Parts & Accessories" }); categories.Add(new Category { Id = 7, Name = "Electronics" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Phones & Tablets" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Computers" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Cameras" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Video games & Consoles" }); categories.Add(new Category { ParentCategoryId = 7, Name = "TVs, DVDs" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Music equipment" }); categories.Add(new Category { ParentCategoryId = 7, Name = "Other" }); categories.Add(new Category { Id = 8, Name = "Jobs" }); db.SaveAll(categories); Listing l = new Listing(); l.CategoryId = 2; l.Description = "asdasdas"; l.Price = 100; l.City = "Windhoek"; l.EmailAddress = "*****@*****.**"; l.Title = "Some title here - very long"; db.Save(l); Listing l2 = new Listing(); l2.CategoryId = 2; l2.Description = "House in Windhieok for sale"; l2.Price = 10000000; l2.City = "Windhoek"; l2.EmailAddress = "*****@*****.**"; l2.Title = "Some title here - very long indeed goes on and on"; db.Save(l2); int count = 0; while (count < 1000) { count++; Listing list = new Listing { CategoryId = 2, City = "Windhoek", Description = count + "House is " + "Longasd asdkjhasd askdjhasdaks dasd " + count, EmailAddress = "*****@*****.**", Price = 10000000, Title = "Tile " +count }; db.Save(list); } } }
public object Post(CreateListing request) { Listing newAd = new Listing(); newAd = request.ConvertTo<Listing>(); using (IDbConnection db = DbFactory.Open()) { var imageUrls = request.Files.Split(';'); foreach (var url in imageUrls) {if (url!= "") newAd.ImageUrls.Add(new ImageModel {IsPrimary = false, Name = url, Url = url}); } db.Save(newAd); } return new CreateListingResponse { Result = true }; }