示例#1
0
        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 EditOffers(int id, bool isEnd, OfferCounterModel formData)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    int currentNeed;
                    string helpText;
                    LocalisationOffer offerType;
                    var context = ModelFactory.GetUnitOfWork();
                    var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(context);
                    var localisation = lRepo.Get(id);
                    formData.RefreshData(localisation);

                    if (formData.NeedAddOffer(out offerType, out currentNeed, out helpText))
                    {
                        var newForm = this.RenderRazorViewToString(MVC.Offer.Views._AjaxAdd, new OfferFormViewModel(formData.IsSharedOffice, offerType, currentNeed) { LocId = id });
                        return Json(new { help = helpText, form = newForm });
                    }
                    else
                    {
                        if (isEnd)
                        {
                            if (formData.OfferLists.Count == 0)
                            {
                                throw new Exception( Worki.Resources.Views.Localisation.LocalisationString.MustAddOffer);
                            }
                        }
                        return Json(new { help = "", form = "", completed = "true", editionType = formData.EditionType });
                    }
                }
                catch (Exception ex)
                {
                    _Logger.Error("EditOffers", ex);
                    ModelState.AddModelError("", ex.Message);
                    throw new ModelStateException(ModelState);
                }
            }
            throw new ModelStateException(ModelState);
        }