public int SaveLaptopBrandModel(Ad ad) { ad.status = "a"; var company = Request["brand"]; var model = Request["model"]; if (company != null && company != "") { company = company.Trim(); model = model.Trim(); } if (true) //company != null { var allBrands = (db.LaptopBrands.Select(x => x.brand)).AsEnumerable(); //getBrands bool isNewBrand = true; foreach (var brand in allBrands) { if (brand == company) { isNewBrand = false; } } if (isNewBrand) { LaptopBrand mob = new LaptopBrand(); 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.LaptopBrands.Add(mob); db.SaveChanges(); LaptopModel mod = new LaptopModel(); 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.LaptopModels.Add(mod); db.SaveChanges(); ad.status = "p"; } else { var allModels = db.LaptopModels.Where(x => x.LaptopBrand.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.LaptopBrands.FirstOrDefault(x => x.brand.Equals(company)); LaptopModel mod = new LaptopModel(); 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.LaptopModels.Add(mod); try { db.SaveChanges(); } catch (Exception e) { string s = e.ToString(); } } } var laptopModel = db.LaptopModels.FirstOrDefault(x => x.LaptopBrand.brand == company && x.model == model); return laptopModel.Id; } }
public async Task<IHttpActionResult> UpdateLaptopBrand(LaptopBrand 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"); }