public async Task <IHttpActionResult> UpdateBikeBrand(BikeBrand 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")); }
public ActionResult DeleteConfirmed(int id) { BikeBrand bikeBrand = db.brands.Find(id); db.brands.Remove(bikeBrand); db.SaveChanges(); return(RedirectToAction("Index")); }
// 构造器是创建类的一种方式 // 下面是一个默认的构造器 public Bicycle() { Gear = 1; // 你可以使用关键词this访问对象的成员 Cadence = 50; // 不过你并不总是需要它 this._speed = 5; Name = "Bontrager"; this.Brand = BikeBrand.AIST; BicyclesCreated++; }
readonly bool _hasCardsInSpokes = false; // read-only private // Constructors are a way of creating classes // This is a default constructor public Bicycle() { this.Gear = 1; // you can access members of the object with the keyword this Cadence = 50; // but you don't always need it _speed = 5; Name = "Bontrager"; Brand = BikeBrand.AIST; BicyclesCreated++; }
public Bicycle() { this.Gear = 1; Cadence = 50; _speed = 5; Name = "Bontrager"; Brand = BikeBrand.AIST; BicyclesCreated++; }
public ActionResult Edit([Bind(Include = "BikeBrandID,Name,Description")] BikeBrand bikeBrand) { if (ModelState.IsValid) { db.Entry(bikeBrand).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(bikeBrand)); }
// 另一个构造器的例子(包含参数) public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes, BikeBrand brand) : base() // 首先调用base { Gear = startGear; Cadence = startCadence; this._speed = startSpeed; Name = name; this._hasCardsInSpokes = hasCardsInSpokes; this.Brand = brand; }
// This is a specified constructor (it contains arguments) public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes, BikeBrand brand) : base() // calls base first { Gear = startGear; Cadence = startCadence; _speed = startSpeed; Name = name; _hasCardsInSpokes = hasCardsInSpokes; Brand = brand; }
public ActionResult Create([Bind(Include = "BikeBrandID,Name,Description")] BikeBrand bikeBrand) { if (ModelState.IsValid) { db.brands.Add(bikeBrand); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(bikeBrand)); }
// GET: BikeBrands/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BikeBrand bikeBrand = db.brands.Find(id); if (bikeBrand == null) { return(HttpNotFound()); } return(View(bikeBrand)); }
public async Task <IHttpActionResult> ApproveBikeBrand(int id) { BikeBrand mobile = await db.BikeBrands.FindAsync(id); if (mobile == null) { return(NotFound()); } if (mobile.status == "a") { return(BadRequest("Already approved")); } mobile.status = "a"; await db.SaveChangesAsync(); return(Ok("approved")); }
public async Task <IHttpActionResult> DeleteBikeBrand(int id) { if (User.Identity.IsAuthenticated) { BikeBrand bid = await db.BikeBrands.FindAsync(id); if (bid == null) { return(NotFound()); } db.BikeBrands.Remove(bid); var ads = db.Ads.Where(x => x.BikeAd.BikeModel1.brandId.Equals(id)); foreach (var ad in ads) { db.Ads.Remove(ad); } await db.SaveChangesAsync(); return(Ok(bid)); } return(BadRequest()); }
// 构造器可以连锁使用 public Bicycle(int startCadence, int startSpeed, BikeBrand brand) : this(startCadence, startSpeed, 0, "big wheels", true, brand) { }
public IdStatus SaveBikesBrandModel(Ad ad) { IdStatus idStatus = new IdStatus(); idStatus.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 { bool isOldBrand = db.BikeBrands.Any(x => x.brand.Equals(company)); if (!isOldBrand) { BikeBrand mob = new BikeBrand(); 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.BikeBrands.Add(mob); db.SaveChanges(); BikeModel mod = new BikeModel(); 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.BikeModels.Add(mod); db.SaveChanges(); //ad.status = "p"; idStatus.status = "p"; } else { bool isOldModel = db.BikeModels.Any(x => x.model.Equals(model)); if (!isOldModel) { idStatus.status = "p"; //ad.status = "p"; var brandId = db.BikeBrands.FirstOrDefault(x => x.brand.Equals(company)); BikeModel mod = new BikeModel(); 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.BikeModels.Add(mod); try { db.SaveChanges(); } catch (Exception e) { string s = e.ToString(); } } } var mobileModel = db.BikeModels.FirstOrDefault(x => x.BikeBrand.brand == company && x.model == model); idStatus.id = mobileModel.Id; return(idStatus); } }
public async Task <bool> addNewBrandModel(string brand, string model, string category) { if (brand != null) { brand.Trim(); } if (model != null) { model.Trim(); } if (brand == "" || brand == "undefined" || brand == null) { return(true); } if (category == "Mobiles") { var isNew = db.Mobiles.Any(x => x.brand.Equals(brand)); if (!isNew) { Mobile mob = new Mobile(); mob.brand = brand; mob.addedBy = User.Identity.GetUserId(); mob.status = "a"; mob.time = DateTime.UtcNow; db.Mobiles.Add(mob); await db.SaveChangesAsync(); if (model != null && model != "" && model != "undefined") { MobileModel mod = new MobileModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = mob.Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.MobileModels.Add(mod); await db.SaveChangesAsync(); } } else { var isNewModel = db.MobileModels.Any(x => x.model.Equals(model)); if (!isNewModel) { if (model != null && model != "" && model != "undefined") { var brandId = db.Mobiles.First(x => x.brand.Equals(brand)); MobileModel mod = new MobileModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = brandId.Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.MobileModels.Add(mod); await db.SaveChangesAsync(); } } } } else if (category == "Laptops") { var isNew = db.LaptopBrands.Any(x => x.brand.Equals(brand)); if (!isNew) { LaptopBrand mob = new LaptopBrand(); mob.brand = brand; mob.addedBy = User.Identity.GetUserId(); mob.status = "a"; mob.time = DateTime.UtcNow; db.LaptopBrands.Add(mob); await db.SaveChangesAsync(); if (model != null && model != "" && model != "undefined") { LaptopModel mod = new LaptopModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = mob.Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.LaptopModels.Add(mod); await db.SaveChangesAsync(); } } else { var isNewModel = db.LaptopModels.Any(x => x.model.Equals(model)); if (!isNewModel) { if (model != null && model != "" && model != "undefined") { LaptopModel mod = new LaptopModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = db.LaptopBrands.First(x => x.brand.Equals(brand)).Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.LaptopModels.Add(mod); await db.SaveChangesAsync(); } } } } else if (category == "Bikes") { var isNew = db.BikeBrands.Any(x => x.brand.Equals(brand)); if (!isNew) { BikeBrand mob = new BikeBrand(); mob.brand = brand; mob.addedBy = User.Identity.GetUserId(); mob.status = "a"; mob.time = DateTime.UtcNow; db.BikeBrands.Add(mob); await db.SaveChangesAsync(); if (model != null && model != "" && model != "undefined") { BikeModel mod = new BikeModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = mob.Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.BikeModels.Add(mod); await db.SaveChangesAsync(); } } else { var isNewModel = db.BikeModels.Any(x => x.model.Equals(model)); if (!isNewModel) { if (model != null && model != "" && model != "undefined") { BikeModel mod = new BikeModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = db.BikeBrands.First(x => x.brand.Equals(brand)).Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.BikeModels.Add(mod); await db.SaveChangesAsync(); } } } } else if (category == "Cars") { var isNew = db.CarBrands.Any(x => x.brand.Equals(brand)); if (!isNew) { CarBrand mob = new CarBrand(); mob.brand = brand; mob.addedBy = User.Identity.GetUserId(); mob.status = "a"; mob.time = DateTime.UtcNow; db.CarBrands.Add(mob); await db.SaveChangesAsync(); if (model != null && model != "" && model != "undefined") { CarModel mod = new CarModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = mob.Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.CarModels.Add(mod); await db.SaveChangesAsync(); } } else { var isNewModel = db.CarModels.Any(x => x.model.Equals(model)); if (!isNewModel) { if (model != null && model != "" && model != "undefined") { CarModel mod = new CarModel(); mod.addedBy = User.Identity.GetUserId(); mod.brandId = db.CarBrands.First(x => x.brand.Equals(brand)).Id; mod.status = "a"; mod.time = DateTime.UtcNow; mod.model = model; db.CarModels.Add(mod); await db.SaveChangesAsync(); } } } } return(true); }