public int SaveMobileBrandModel(ref Ad ad) { ad.status = "a"; var company = System.Web.HttpContext.Current.Request["brand"]; var model = System.Web.HttpContext.Current.Request["model"]; if (company != null && company != "") { company = company.Trim(); model = model.Trim(); } if (true) //company != null { var allBrands = (db.Mobiles.Select(x => x.brand)).AsEnumerable(); //getBrands bool isNewBrand = true; foreach (var brand in allBrands) { if (brand == company) { isNewBrand = false; } } if (isNewBrand) { Mobile mob = new Mobile(); mob.brand = company; mob.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId(); mob.time = DateTime.UtcNow; if (company == null || company == "") { mob.status = "a"; } else { mob.status = "p"; } db.Mobiles.Add(mob); db.SaveChanges(); MobileModel mod = new MobileModel(); mod.model = model; mod.brandId = mob.Id; mod.time = DateTime.UtcNow; if (model == null || model == "") { mod.status = "a"; } else { mod.status = "p"; } mod.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId(); db.MobileModels.Add(mod); db.SaveChanges(); ad.status = "p"; } else { var allModels = db.MobileModels.Where(x => x.Mobile.brand == company).Select(x => x.model); bool isNewModel = true; foreach (var myModel in allModels) { if (myModel == model) { isNewModel = false; } } if (isNewModel) { ad.status = "p"; var brandId = db.Mobiles.FirstOrDefault(x => x.brand.Equals(company)); MobileModel mod = new MobileModel(); mod.brandId = brandId.Id; mod.model = model; if (model == null || model == "") { mod.status = "a"; } else { mod.status = "p"; } mod.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId(); mod.time = DateTime.UtcNow; db.MobileModels.Add(mod); try { db.SaveChanges(); } catch (Exception e) { string s = e.ToString(); } } } var mobileModel = db.MobileModels.FirstOrDefault(x => x.Mobile.brand == company && x.model == model); return mobileModel.Id; } }
public async Task<IHttpActionResult> PostMobile(Mobile mobile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Mobiles.Add(mobile); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = mobile.Id }, mobile); }
public ActionResult Create([Bind(Include="Id,category,postedBy,title,description,time")] Ad ad) { if (ModelState.IsValid) { if (Request.IsAuthenticated) { MobileAd mobileAd= new MobileAd(); var isbiding = Request["bidingAllowed"]; if (isbiding == "fixedPrice") { var nn = Request["isNegotiable"]; if (nn == "on") { ad.isnegotiable = "y"; } else { ad.isnegotiable = "n"; } } else if (isbiding == "allowBiding") { ad.isnegotiable = "b"; } var condition = Request["condition"]; if (condition == "new") { mobileAd.condition = "n"; } else if (condition == "unboxed") { mobileAd.condition = "b"; } else { mobileAd.condition = "u"; } mobileAd.sims =int.Parse( Request["sims"]); mobileAd.color = Request["color"]; var pp = Request["price"]; pp = pp.Replace(",", string.Empty); if (pp != null && pp != "" ){ //if (pp.Substring(pp.Length - 1) == ",") //{ // pp = pp.Remove(pp.Length - 1); //} ad.price =int.Parse( pp); } var company = Request["brand"]; var model = Request["model"]; ad.time = DateTime.UtcNow; ad.description = System.Web.HttpUtility.HtmlEncode(ad.description); ad.postedBy = User.Identity.GetUserId(); AspNetUser asp = db.AspNetUsers.FirstOrDefault(x => x.Id == ad.postedBy); ad.category = "Electronics"; //var companyName = model.Split(' ')[0]; var allBrands = (db.Mobiles.Select(x => x.Id)).AsEnumerable(); //getBrands bool isNewBrand = true; foreach (var brand in allBrands) { if (brand == company) { isNewBrand = false; } } if (isNewBrand) { Mobile mob = new Mobile(); mob.Id = company; db.Mobiles.Add(mob); db.SaveChanges(); MobileModel mod = new MobileModel(); mod.model = model; mod.Mobile = company; db.MobileModels.Add(mod); db.SaveChanges(); //send admin notification } else { var allModels = db.MobileModels.Where(x => x.Mobile == company).Select(x => x.model); bool isNewModel = true; foreach (var myModel in allModels) { if (myModel == model) { isNewModel = false; } } if (isNewModel) { MobileModel mod = new MobileModel(); mod.Mobile = company; mod.model = model; db.MobileModels.Add(mod); db.SaveChanges(); //send admin notification with user info } } //if (companyName == "HTC" || companyName == "Nokia" || companyName == "Samsung" || companyName == "Iphone") //{ // string[] newModel = model.Split(' ').Skip(1).ToArray(); // model = string.Join("", newModel); //} var mobiledata = db.Mobiles.FirstOrDefault(x => x.Id == company && x.MobileModels.Any(xu=>xu.model.Equals(model))); //if (mobiledata == null) //{ // Mobile mob = new Mobile(); // mob.Id = companyName; // db.Mobiles.Add(mob); // MobileModel mm = new MobileModel(); // mm.Mobile = companyName; // mm.model = model; // //send admin notification //} mobileAd.mobileId = mobiledata.Id; asp.Ads.Add(ad); db.Ads.Add(ad); mobileAd.adId = ad.Id; ad.MobileAds.Add(mobileAd); try { db.SaveChanges(); } catch (Exception e) { string s = e.ToString(); } return RedirectToAction("Index", new {category = "mobiles",subcategory = mobiledata.Id,lowcategory = mobiledata.MobileModels.FirstOrDefault(x=>x.model.Equals(model)).model,id = ad.Id , title = ad.title }); } TempData["error"] = "You must be logged in to post ad"; return View("Create", ad); } TempData["error"] = "Only enter those information about which you are asked"; return View("Create", ad); //ViewBag.postedBy = new SelectList(db.AspNetUsers, "Id", "Email", ad.postedBy); //return View(ad); }
// PUT api/Admin/5 public async Task<IHttpActionResult> PutMobile(int id, Mobile mobile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != mobile.Id) { return BadRequest(); } db.Entry(mobile).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MobileExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> UpdateMobileBrand(Mobile mob) { if (User.Identity.IsAuthenticated) { if (!ModelState.IsValid) { return BadRequest(); } mob.addedBy = User.Identity.GetUserId(); mob.time = DateTime.UtcNow; mob.status = "a"; db.Entry(mob).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbEntityValidationException e) { string s = e.ToString(); List<string> errorMessages = new List<string>(); foreach (DbEntityValidationResult validationResult in e.EntityValidationErrors) { string entityName = validationResult.Entry.Entity.GetType().Name; foreach (DbValidationError error in validationResult.ValidationErrors) { errorMessages.Add(entityName + "." + error.PropertyName + ": " + error.ErrorMessage); } } } return StatusCode(HttpStatusCode.NoContent); } return BadRequest("Not login"); }