예제 #1
0
        public void AssignBenefitToYear(AssignBenefitToYear command)
        {
            if (_state.PlanYearBenefits.Exists(pyb => pyb.PlanYear == command.PlanYear))
            {
                throw new DomainException(string.Format("This benefit is already assigned to the {0} plan year.", command.PlanYear));
            }

            if (command.HasMaxAnnualAmount == false && _state.HasMaxAnnualAmount)
            {
                throw new DomainException(
                    "You are attempting to create a plan year benefit with no limit on the annual amount, but the benefit has a limit on the annual amount.");
            }

            if (command.HasMaxAnnualAmount && _state.HasMaxAnnualAmount &&
                command.MaxAnnualAmount.Dollars() > _state.MaxAnnualAmount)
            {
                throw new DomainException(
                    string.Format(
                        "You are attempting to create a plan year benefit with a maximum amount that is greater than the maximum amount for the benefit."));
            }

            ApplyEvent(new BenefitAssignedToYearEvent
                {
                    BenefitId = command.BenefitId,
                    HasMaxAnnualAmount = command.HasMaxAnnualAmount,
                    MaxAnnualAmount = command.MaxAnnualAmount,
                    PlanYear = command.PlanYear,
                    StartDate = command.StartDate,
                    PlanId = command.PlanId,
                    CompanyId = command.CompanyId,
                    
                },
                @event => _state.Apply(@event));
        }
        public ActionResult AssignToYear(AssignBenefitToYear command, FormCollection formCollection)
        {
            try
            {
                //push our DefineNewBenefit command message on to the bus.
                command.CompanyId = MvcApplication.CompanyId;
                CommandResponse response = _commandSender.Send(command);

                if (response.CommandStatus == CommandStatusEnum.Failed)
                {
                    //set the error message in the ViewData and return to the view
                    ModelState.AddModelError("ResponseError", response.Message);
                    SetUpBenefitViewData(command.BenefitId);
                    SetUpPlanYearsViewData(command.PlanId);
                    return View();
                }

                return RedirectToAction("List");
            }
            catch (TimeoutException toe)
            {
                ModelState.AddModelError("TimeOutError", toe.Message);
                SetUpBenefitViewData(command.BenefitId);
                SetUpPlanYearsViewData(command.PlanId);
                return View();
            }
            catch
            {
                SetUpBenefitViewData(command.BenefitId);
                SetUpPlanYearsViewData(command.PlanId);
                return View();
            }
        }