public ActionResult Edit(OfferCodeViewModel model) { if (String.IsNullOrWhiteSpace(model.Code)) { ModelState.AddModelError("Code", "Code is required"); } if (!ModelState.IsValid) { return(View(model)); } var offerService = new OfferService(); var offerCode = new OfferCodeExtended(); if (model.Id != 0)//not creating a new offerCode, but rather editing { offerCode = offerService.GetOfferCode(model.Id); if (offerCode != null) { if (!offerIsOwnedByUserCompany(offerCode.OfferId)) { return(RedirectToAction("List", new { offerId = offerCode.OfferId })); } } else//if offercode == null { return(RedirectToAction("List", new { offerId = model.OfferId })); } model.SuccessMessage = "Success - Offer Code saved."; } else //creating new offer code { if (!offerIsOwnedByUserCompany(model.OfferId)) { return(RedirectToAction("List", new { offerId = model.OfferId })); } } offerCode.Code = model.Code; offerCode.OfferId = model.OfferId; offerCode.Id = model.Id; offerCode.BuyerEmail = model.BuyerEmail; offerService.SaveOfferCode(offerCode); model.SuccessMessage = "Success - Offer Code saved."; return(RedirectToAction("List", new { offerId = model.OfferId })); }
public void GetCsv() { var Offer = new Offer(); var testTitle = "Necklaces"; var testDescription = "80% off these shiny necklaces!"; var testUrl = "fakeurl.bamazon.necklaces/"; var testCategory = "Jewelry"; var testCompanyId = RandomInteger(); Offer.Title = testTitle; Offer.Description = testDescription; Offer.Url = testUrl; Offer.Category = testCategory; Offer.CompanyId = testCompanyId; //create (offercode) //codes that will be claimed var offerCode1 = new OfferCodeExtended(); offerCode1.Code = "ABCDEF" + RandomDigits(); var offerCode2 = new OfferCodeExtended(); offerCode2.Code = "GHIJKL" + RandomDigits(); var offerCode3 = new OfferCodeExtended(); offerCode3.Code = "MNOPQR" + RandomDigits(); //codes that will be unclaimed var offerCode4 = new OfferCodeExtended(); offerCode4.Code = "STUVW" + RandomDigits(); var offerCode5 = new OfferCodeExtended(); offerCode5.Code = "XYZ" + RandomDigits(); offerCode1.OfferId = Offer.Id; offerCode2.OfferId = Offer.Id; offerCode3.OfferId = Offer.Id; OfferService offerService = new OfferService(); offerService.SaveOffer(Offer); Assert.IsTrue(Offer.Id != 0); offerCode1.OfferId = Offer.Id; offerCode2.OfferId = Offer.Id; offerCode3.OfferId = Offer.Id; offerService.SaveOfferCode(offerCode1); offerService.SaveOfferCode(offerCode2); offerService.SaveOfferCode(offerCode3); Assert.IsTrue(offerCode1.Id != 0); Assert.IsTrue(offerCode2.Id != 0); Assert.IsTrue(offerCode3.Id != 0); var userId1 = "Jackie Bolton"; var email1 = userId1 + "@gmail.com"; var code1 = offerService.ClaimNextCode(Offer.Id, userId1); Assert.IsTrue(!String.IsNullOrEmpty(code1)); //Verify that the first code is claimed by Jackie offerCode1 = offerService.GetOfferCode(offerCode1.Id); Assert.IsTrue(offerCode1.ClaimingUser.Equals(userId1)); var userId2 = "Sandra Lollygagger"; var email2 = userId2 + "@gmail.com"; var code2 = offerService.ClaimNextCode(Offer.Id, userId2); Assert.IsTrue(!String.IsNullOrEmpty(code2)); //Verify that the second code is claimed by Sandra offerCode2 = offerService.GetOfferCode(offerCode2.Id); Assert.IsTrue(offerCode2.ClaimingUser.Equals(userId2)); Assert.IsTrue(!offerCode1.ClaimingUser.Equals(offerCode2.ClaimingUser)); var userId3 = "Kevin Spaceman"; var email3 = userId3 + "@gmail.com"; var code3 = offerService.ClaimNextCode(Offer.Id, userId3); Assert.IsTrue(!String.IsNullOrEmpty(code3)); //Verify that the third code is claimed by Kevin offerCode3 = offerService.GetOfferCode(offerCode3.Id); Assert.IsTrue(offerCode3.ClaimingUser.Equals(userId3)); Assert.IsTrue(!offerCode1.ClaimingUser.Equals(offerCode3.ClaimingUser)); var userId4 = "Richmond Nocode"; var email4 = userId4 + "@gmail.com"; var code4 = offerService.ClaimNextCode(Offer.Id, userId4); string codesCsv = offerService.GetAllOfferCodesCsv(Offer.Id); //This time verifying that code4 IS empty because we have no more codes in the offer. Assert.IsTrue(String.IsNullOrEmpty(code4)); offerService.DeleteOfferCode(offerCode1.Id); offerService.DeleteOfferCode(offerCode2.Id); offerService.DeleteOfferCode(offerCode3.Id); offerService.DeleteOffer(Offer.Id); Console.Out.WriteLine(codesCsv); }