public virtual ActionResult AjaxAdd(int id, OfferFormViewModel offerFormViewModel) { _ObjectStore.Store<PictureDataContainer>(PictureData.GetKey(ProviderType.Offer), new PictureDataContainer(offerFormViewModel.Offer)); if (ModelState.IsValid) { try { var context = ModelFactory.GetUnitOfWork(); var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(context); var loc = lRepo.Get(id); var offerType = (LocalisationOffer)offerFormViewModel.Offer.Type; try { loc.Offers.Add(offerFormViewModel.Offer); if (offerFormViewModel.DuplicateCount > 0) { var toAdd = offerFormViewModel.Offer.Replicate(offerFormViewModel.DuplicateCount); foreach (var offer in toAdd) { loc.Offers.Add(offer); } } //force online when one offer added loc.MainLocalisation.IsOffline = false; context.Commit(); } catch (Exception ex) { _Logger.Error(ex.Message); context.Complete(); throw ex; } _ObjectStore.Delete(PictureData.GetKey(ProviderType.Offer)); var newContext = ModelFactory.GetUnitOfWork(); lRepo = ModelFactory.GetRepository<ILocalisationRepository>(newContext); loc = lRepo.Get(id); var offerCountModel = new OfferCounterModel(loc); int currentNeed; string helpText; LocalisationOffer offerTypeToAdd; var newList = this.RenderRazorViewToString(MVC.Offer.Views._OfferList, offerCountModel); if (offerCountModel.NeedAddThisOffer(offerType, out currentNeed, out helpText)) { var newForm = this.RenderRazorViewToString(MVC.Offer.Views._AjaxAdd, new OfferFormViewModel(loc.IsSharedOffice(), offerType, currentNeed) { LocId = id }); return Json(new { help = helpText, form = newForm, newList = newList, newBounds = offerCountModel.GetJson() }); } else if (offerCountModel.NeedAddOffer(out offerTypeToAdd, out currentNeed, out helpText)) { var newForm = this.RenderRazorViewToString(MVC.Offer.Views._AjaxAdd, new OfferFormViewModel(loc.IsSharedOffice(), offerTypeToAdd, currentNeed) { LocId = id }); return Json(new { help = helpText, form = newForm, newList = newList, newBounds = offerCountModel.GetJson() }); } else { return Json(new { help = "", form = "", newList = newList, newBounds = offerCountModel.GetJson() }); } } catch (Exception ex) { _Logger.Error("AjaxAdd", ex); ModelState.AddModelError("", ex.Message); throw new ModelStateException(ModelState); } } throw new ModelStateException(ModelState); }
public virtual ActionResult AjaxDelete(int id) { try { var context = ModelFactory.GetUnitOfWork(); var oRepo = ModelFactory.GetRepository<IOfferRepository>(context); try { var offer = oRepo.Get(id); var locId = offer.LocalisationId; var isShared = offer.Localisation.IsSharedOffice(); var removedType = (LocalisationOffer)offer.Type; oRepo.Delete(id); context.Commit(); var newContext = ModelFactory.GetUnitOfWork(); var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(newContext); var loc = lRepo.Get(locId); var offerCountModel = new OfferCounterModel(loc); var newList = this.RenderRazorViewToString(MVC.Offer.Views._OfferList, offerCountModel); return Json(new { help = "", form = "", newList = newList, newBounds = offerCountModel.GetJson(), toDecrement = removedType.ToString() }); } catch (Exception ex) { _Logger.Error("AjaxDelete", ex); context.Complete(); throw ex; } } catch (Exception ex) { _Logger.Error("AjaxDelete", ex); ModelState.AddModelError("", ex.Message); throw new ModelStateException(ModelState); } }