public IHttpActionResult CreateAd(AdBindingModel adBindingModel) { if (adBindingModel == null) { return this.BadRequest("Your data is missing"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (!adBindingModel.Categories.Any() || adBindingModel.Categories.Count() > 3) { return this.BadRequest("Categories count must be between 1 and 3"); } var ad = new Ad(); ad.OwnerId = this.User.Identity.GetUserId(); ad.Name = adBindingModel.Name; ad.Description = adBindingModel.Description; ad.Price = adBindingModel.Price; ad.PostedOn = DateTime.Now; var type = this.Data.AdTypes.FirstOrDefault(t => t.Id == adBindingModel.TypeId); if (type != null) { ad.Type = type; } ad.Categories = new List<Category>(); foreach (var categoryId in adBindingModel.Categories) { var category = this.Data.Categories.FirstOrDefault(c => c.Id == categoryId); if (category != null) { ad.Categories.Add(category); } } this.Data.Ads.Add(ad); this.Data.SaveChanges(); var adViewModel = this.Data.Ads .Where(a => a.Id == ad.Id) .Select(AdViewModel.Create) .FirstOrDefault(); return this.Ok(adViewModel); }
public IHttpActionResult AddAd(AdBindingModel adBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(ModelState); } if (adBindingModel == null) { return this.BadRequest("No data privided for ad"); } var ad = adBindingModel.CreateAd(); ad.OwnerId = this.User.Identity.GetUserId(); ad.PostedOn = DateTime.Now; // ad.Type = this.Data.Ads.Add(ad); this.Data.SaveChanges(); return this.Ok("kur"); }