public ActionResult NewStorageLot(NewStorageLotModel model) { if (ModelState.IsValid) { using (var db = new BGS_DBContext()) { string userIdentityId = User.Identity.GetUserId(); var seller = db.Users.Single(u => u.Id == userIdentityId); for (int i = 0; i < model.LotCount; i++) { StorageModel newStorageModel = GameUtilCreater.StorageModelFromModel(model); db.StorageModels.Add(newStorageModel); db.SaveChanges(); var lot = new LotModel() { Seller = seller, ItemId = newStorageModel.Id, Price = model.Price, SellerId = seller.GameId, Status = LotStatus.Available, Type = LotType.Storage }; db.LotModels.Add(lot); db.SaveChanges(); newStorageModel.LotId = lot.Id; } db.SaveChanges(); return(RedirectToAction("Index", "Market")); } } ModelState.AddModelError("", "Что то не правильно"); return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { string userName = string.Join("", model.Email.TakeWhile(c => c != '@')); var user = new ApplicationUser { UserName = userName, Email = model.Email }; using (var ctx_db = new BGS_DBContext()) { var new_user = GameUtilCreater.CreateNewUser(model.Email, model.Password, user); ctx_db.Users.Add(new_user); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // Дополнительные сведения о включении подтверждения учетной записи и сброса пароля см. на странице https://go.microsoft.com/fwlink/?LinkID=320771. // Отправка сообщения электронной почты с этой ссылкой // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Подтверждение учетной записи", "Подтвердите вашу учетную запись, щелкнув <a href=\"" + callbackUrl + "\">здесь</a>"); ctx_db.SaveChanges(); return(RedirectToAction("Index", "Home")); } AddErrors(result); } } // Появление этого сообщения означает наличие ошибки; повторное отображение формы return(View(model)); }
public ActionResult NewModificationLot(NewModificationLotModel model) { if (ModelState.IsValid) { using (var db = new BGS_DBContext()) { string userIdentityId = User.Identity.GetUserId(); var seller = db.Users.Single(u => u.Id == userIdentityId); for (int i = 0; i < model.LotCount; i++) { switch (model.Type) { case ModificationType.Aim: AimModificationModel newAimModModel = new AimModificationModel() { Item = model.Item, WeaponType = model.WeaponType }; db.AimModificationModels.Add(newAimModModel); db.SaveChanges(); var aimLot = new LotModel() { Seller = seller, ItemId = newAimModModel.Id, Price = model.Price, SellerId = seller.GameId, Status = LotStatus.Available, Type = LotType.Modification }; db.LotModels.Add(aimLot); db.SaveChanges(); newAimModModel.LotId = aimLot.Id; break; case ModificationType.Magazine: MagazineModificationModel newMagazineModModel = new MagazineModificationModel() { Item = model.Item, WeaponType = model.WeaponType }; db.MagazineModificationModels.Add(newMagazineModModel); db.SaveChanges(); var maganineLot = new LotModel() { Seller = seller, ItemId = newMagazineModModel.Id, Price = model.Price, SellerId = seller.GameId, Status = LotStatus.Available, Type = LotType.Modification }; db.LotModels.Add(maganineLot); db.SaveChanges(); newMagazineModModel.LotId = maganineLot.Id; break; case ModificationType.Barrel: BarrelModificationModel newBarrelModModel = new BarrelModificationModel() { Item = model.Item, WeaponType = model.WeaponType }; db.BarrelModificationModels.Add(newBarrelModModel); db.SaveChanges(); var barrelLot = new LotModel() { Seller = seller, ItemId = newBarrelModModel.Id, Price = model.Price, SellerId = seller.GameId, Status = LotStatus.Available, Type = LotType.Modification }; db.LotModels.Add(barrelLot); db.SaveChanges(); newBarrelModModel.LotId = barrelLot.Id; break; case ModificationType.Butt: ButtModificationModel newButtModModel = new ButtModificationModel() { Item = model.Item, WeaponType = model.WeaponType }; db.ButtModificationModels.Add(newButtModModel); db.SaveChanges(); var buttLot = new LotModel() { Seller = seller, ItemId = newButtModModel.Id, Price = model.Price, SellerId = seller.GameId, Status = LotStatus.Available, Type = LotType.Modification }; db.LotModels.Add(buttLot); db.SaveChanges(); newButtModModel.LotId = buttLot.Id; break; } } db.SaveChanges(); return(RedirectToAction("Index", "Market")); } } ModelState.AddModelError("", "Что то не правильно"); return(View(model)); }
public ActionResult BuyLot(int lotId) { using (var db = new BGS_DBContext()) { var id = User.Identity.GetUserId(); var lot = db.LotModels.Single(p => p.Id == lotId); User user = db.Users.Single(u => u.Id == id); if (user.AccountBalance - lot.Price > 0) { switch (lot.Type) { default: throw new Exception("No this lot"); case LotType.Unit: UnitModel unitModel = db.UnitModels.Single(u => u.LotId == lotId); unitModel.LotId = null; unitModel.Owner = user; break; case LotType.Armor: ArmorModel armorModel = db.ArmorModels.Single(u => u.LotId == lotId); armorModel.LotId = null; armorModel.Owner = user; break; case LotType.Accessory: AccessoryModel accessoryModel = db.AccessoryModels.Single(u => u.LotId == lotId); accessoryModel.LotId = null; accessoryModel.Owner = user; break; case LotType.Weapon: WeaponModel weaponModel = db.WeaponModels.Single(u => u.LotId == lotId); weaponModel.LotId = null; weaponModel.Owner = user; break; case LotType.Storage: StorageModel storageModel = db.StorageModels.Single(u => u.LotId == lotId); storageModel.LotId = null; storageModel.Owner = user; break; case LotType.Modification: AimModificationModel aim = db.AimModificationModels.SingleOrDefault(a => a.LotId == lot.Id); MagazineModificationModel magazine = db.MagazineModificationModels.SingleOrDefault(a => a.LotId == lot.Id); BarrelModificationModel barrel = db.BarrelModificationModels.SingleOrDefault(a => a.LotId == lot.Id); ButtModificationModel butt = db.ButtModificationModels.SingleOrDefault(a => a.LotId == lot.Id); if (aim != null) { aim.LotId = null; aim.Owner = user; } if (magazine != null) { magazine.LotId = null; magazine.Owner = user; } if (barrel != null) { barrel.LotId = null; barrel.Owner = user; } if (butt != null) { butt.LotId = null; butt.Owner = user; } break; } lot.Status = LotStatus.Closed; lot.BuyerId = user.GameId; user.AccountBalance -= lot.Price; lot.Seller.AccountBalance += lot.Price; db.SaveChanges(); return(RedirectToAction("Index", "Market", new { buy = $"Лот #{lot.Id} успешно куплен за {lot.Price}$4" })); } else { return(RedirectToAction("Index", "Market", new { error = "У вас не хватает средств для этой покупки" })); } } }