public IHttpActionResult PutCreateAd(CreateAdBindingModel model) { if (model == null) { return this.BadRequest("Model cannot be empty!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (!model.Categories.Any() || model.Categories.Count() > 3) { return this.BadRequest("Categories count cannot be " + model.Categories.Count()); } var dbType = this.Data.AdTypes.All().FirstOrDefault(c => c.Id == model.TypeId); if (dbType == null) { return this.BadRequest("No type with id " + model.TypeId + " found!"); } var categories = new List<Category>(); foreach (var categoryId in model.Categories) { var dbCategory = this.Data.Categories.All().FirstOrDefault(c => c.Id == categoryId); if (dbCategory == null) { return this.BadRequest("No category with id " + model.TypeId + " found!"); } categories.Add(dbCategory); } var userId = this.User.Identity.GetUserId(); var user = this.Data.Users.All().FirstOrDefault(u => u.Id == userId); var newAd = new Ad() { Categories = categories, PostedOn = DateTime.Now, Description = model.Description, Name = model.Name, Owner = user, Price = model.Price, Type = dbType }; this.Data.Ads.Add(newAd); this.Data.SaveChanges(); var dbAd = this.Data.Ads.All() .Where(a => a.Id == newAd.Id) .Select(AdViewModel.Create) .FirstOrDefault(); return this.Ok(dbAd); }
public IHttpActionResult CreateAd(CreateAdBindingModel model) { if (!this.ModelState.IsValid) { return BadRequest(this.ModelState); } if (model == null) { return BadRequest("Missing data!"); } if (model.Categories.Count() < 0 && model.Categories.Count() > 3) { return BadRequest("Invalid data!1"); } if (model.Categories.Any(category => !this.Data.Categories.Any(c => c.Id == category))) { return BadRequest("Invalid data!2"); } if (!this.Data.AdTypes.Any(t => t.Id == model.TypeId)) { return BadRequest("Invalid data!3"); } string userId = User.Identity.GetUserId(); var user = this.Data.Users.FirstOrDefault(x => x.Id == userId); var ad = new Ad { Name = model.Name, Price = model.Price, Description = model.Description, TypeId = model.TypeId, PostedOn = DateTime.Now, Owner = user }; foreach (var c in model.Categories) { ad.Categories.Add(Data.Categories.FirstOrDefault(x => x.Id == c)); } this.Data.Ads.AddOrUpdate(ad); this.Data.SaveChanges(); AdViewModel result = this.Data.Ads .Where(a => a.Id == ad.Id) .Select(AdViewModel.Create) .FirstOrDefault(); return Ok(result); }
public IHttpActionResult CreateAd(CreateAdBindingModel model) { var userId = this.User.Identity.GetUserId(); if (userId == null) { return this.BadRequest(); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var categories = new HashSet<Category>(); var allCategories = this.Data.Categories; foreach (var category in model.Categories) { var categoryData = allCategories.FirstOrDefault(c => c.Id == category); if (categoryData == null) { return this.BadRequest("Invalid category"); } categories.Add(categoryData); } var ad = new Ad() { Name = model.Name, Description = model.Description, PostedOn = DateTime.Now, TypeId = model.TypeId, Price = model.Price, OwnerId = userId, Categories = categories }; this.Data.Ads.Add(ad); this.Data.SaveChanges(); var result = this.Data.Ads .Where(a => a.Id == ad.Id) .Select(AdViewModel.Create) .FirstOrDefault(); return this.Ok(result); }
public IHttpActionResult CreateAds(CreateAdBindingModel model) { var UserId = this.User.Identity.GetUserId(); if (UserId == null) { return this.Unauthorized(); } if (!this.ModelState.IsValid) { return this.BadRequest(ModelState); } if (model == null) { return this.BadRequest("Model cannot be empty!"); } if (!model.Categories.Any() || model.Categories.Count() > 3) { return this.BadRequest("Categories count cannot be " + model.Categories.Count()); } var adTypeDb = this.Data.AdTypes.FirstOrDefault(a => a.Id == model.TypeId); if (adTypeDb == null) { return this.BadRequest("No type with id " + model.TypeId + " found!"); } var categoris = new List<Category>(); foreach (var catId in model.Categories) { var categoryFromDB = this.Data.Categories.Find(catId); if (categoryFromDB == null) { return this.BadRequest("No category with id " + catId + " found!"); } categoris.Add(categoryFromDB); } var user = this.Data.Users.Find(UserId); var adToAdd = new Ad() { Categories = categoris, Description = model.Description, Name = model.Name, Price = model.Price, TypeId = model.TypeId, PostedOn = DateTime.Now, Owner = user, Type = adTypeDb }; this.Data.Ads.Add(adToAdd); this.Data.SaveChanges(); var result = this.Data.Ads .Where(a => a.Id == adToAdd.Id) .Select(AdsViewModel.Create) .FirstOrDefault(); return this.Ok(result); }