public ActionResult Edit(string id) { ObjectId objectId = new ObjectId(id); var applicationUser = this.userManager.GetUserAsync(HttpContext.User).Result; var sponsor = this.mongoContext.Sponsors.Find(s => s.Id.Equals(applicationUser.ActorId)).First(); if (!sponsor.SponsorShips.Contains(objectId)) { TempData["Error"] = "This sponsorship isn't yours."; return(RedirectToAction(nameof(MySponsorShips))); } var sponsorship = this.mongoContext.SponsorShips.Find(s => s.Id.Equals(objectId)).First(); var model = new SponsorShipViewModel { Banner = sponsorship.Banner, BrandName = sponsorship.CreditCard.BrandName, CvvCode = sponsorship.CreditCard.CvvCode, ExpirationMonth = sponsorship.CreditCard.ExpirationMonth, ExpirationYear = sponsorship.CreditCard.ExpirationYear, HolderName = sponsorship.CreditCard.HolderName, Number = sponsorship.CreditCard.Number }; return(View(model)); }
public ActionResult Edit(string id, SponsorShipViewModel collection) { if (ModelState.IsValid) { try { // TODO: Add update logic here ObjectId objectId = new ObjectId(id); var applicationUser = this.userManager.GetUserAsync(HttpContext.User).Result; var sponsor = this.mongoContext.Sponsors.Find(s => s.Id.Equals(applicationUser.ActorId)).First(); if (!sponsor.SponsorShips.Contains(objectId)) { TempData["Error"] = "This sponsorship isn't yours."; return(RedirectToAction(nameof(MySponsorShips))); } CreditCard creditCard = new CreditCard { HolderName = collection.HolderName, BrandName = collection.BrandName, Number = collection.Number, ExpirationMonth = collection.ExpirationMonth, ExpirationYear = collection.ExpirationYear, CvvCode = collection.CvvCode }; UpdateDefinition <SponsorShip> updateDefinition = Builders <SponsorShip> .Update.Set(s => s.Banner, collection.Banner) .Set(s => s.CreditCard, creditCard); this.mongoContext.SponsorShips.FindOneAndUpdate(s => s.Id.Equals(objectId), updateDefinition); return(RedirectToAction(nameof(MySponsorShips))); } catch { return(View()); } } return(View()); }
public ActionResult Create(SponsorShipViewModel collection) { if (ModelState.IsValid) { try { // TODO: Add insert logic here var applicationUser = this.userManager.GetUserAsync(HttpContext.User).Result; var sponsor = this.mongoContext.Sponsors.Find(f => f.Id.Equals(applicationUser.ActorId)).First(); CreditCard creditCard = new CreditCard { HolderName = collection.HolderName, BrandName = collection.BrandName, Number = collection.Number, ExpirationMonth = collection.ExpirationMonth, ExpirationYear = collection.ExpirationYear, CvvCode = collection.CvvCode }; SponsorShip sponsorShip = new SponsorShip { Id = ObjectId.GenerateNewId(), Banner = collection.Banner, CreditCard = creditCard }; sponsor.SponsorShips.Add(sponsorShip.Id); this.mongoContext.SponsorShips.InsertOne(sponsorShip); this.mongoContext.Sponsors.FindOneAndUpdate(s => s.Id.Equals(sponsor.Id), Builders <Sponsor> .Update.Set(s => s.SponsorShips, sponsor.SponsorShips)); return(RedirectToAction(nameof(MySponsorShips))); } catch { return(View(collection)); } } return(View(collection)); }