Ejemplo n.º 1
0
        public ActionResult Update(QuestionnaireUpdateModel model)
        {
            try
            {
                using (var dataContext = new HuntingEntities())
                {
                    var user = AclUserContext.GetDetail(dataContext, User.Identity.Name);
                    if (user == null)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(RedirectToAction("Index", "Home"));
                    }
                    var questionnaire = QuestionnaireContext.GetDetail(dataContext, model.Id);
                    if (questionnaire == null)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_FOUND);
                        return(RedirectToAction("Index", "Home"));
                    }
                    if (user.CanUpdateTerritory(questionnaire.MapItemType.Territory) == false)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(RedirectToAction("Index", "Territory", new { id = questionnaire.MapItemType.TerritoryId }));
                    }

                    if (ModelState.IsValid)
                    {
                        var newItemId = QuestionnaireContext.Update(dataContext, questionnaire, model, user);
                        if (newItemId.HasValue)
                        {
                            ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Success, QuestionnaireRes.SUCCESS_UPDATE);
                            return(RedirectToAction("Index", "Questionnaire", new { id = model.MapItemTypeId }));
                        }
                        else
                        {
                            ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, QuestionnaireRes.ERROR_UPDATE);
                        }
                    }
                    return(View(model));
                }
            }
            catch (Exception exception)
            {
                logger.Error(exception, "QuestionnaireController");
                ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION);
                return(RedirectToAction("Index", "Home"));
            }
        }