Exemplo n.º 1
0
        public ActionResult UpdateAccess(SupervisorUpdateAccessViewModel model)
        {
            if (!Request.IsAjaxRequest())
            {
                return(RedirectToAction("Index"));
            }

            AlertMessage alert = new AlertMessage();

            SupervisorBusiness business = new SupervisorBusiness();

            if (!ModelState.IsValid)
            {
                alert.Text = string.Join(System.Environment.NewLine, ModelState.Values
                                         .SelectMany(v => v.Errors)
                                         .Select(e => e.ErrorMessage));
            }
            else
            {
                business.SetUserAuth(ViewBag.UserAuth);

                alert = business.UpdateAccess(model);
            }

            return(new MyJsonResult(alert, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        public AlertMessage UpdateAccess(SupervisorUpdateAccessViewModel model)
        {
            AlertMessage alert = new AlertMessage();

            if (!IsAccessible(ModuleCode.Supervisor))
            {
                alert.Text = StaticMessage.ERR_ACCESS_DENIED;
                return(alert);
            }

            if (model == null || model.ListData.Count == 0)
            {
                alert.Text = StaticMessage.ERR_INVALID_INPUT;
                return(alert);
            }

            IRepository <FSS> repoFSS = _unitOfWork.GetRepository <FSS>();

            var orCondition = PredicateBuilder.False <FSS>();

            foreach (var item in model.ListData)
            {
                orCondition = orCondition.Or(x => x.NIK == item.NIK);
            }

            repoFSS.Condition = PredicateBuilder.True <FSS>().And(orCondition);

            List <FSS> listFSS = repoFSS.Find();

            if (listFSS == null)
            {
                alert.Text = StaticMessage.ERR_DATA_NOT_FOUND;
                return(alert);
            }

            SupervisorNIKValUpdateAccess        valAccess     = null;
            List <SupervisorNIKValUpdateAccess> listValAccess = new List <SupervisorNIKValUpdateAccess>();

            try
            {
                _unitOfWork.BeginTransaction();

                foreach (var fss in listFSS)
                {
                    valAccess = model.ListData.FirstOrDefault(x => x.NIK == fss.NIK);

                    if (valAccess != null)
                    {
                        if (valAccess.IsAllowed)
                        {
                            fss.AllowedByNIK  = _userAuth.NIK;
                            fss.UploadValidTo = valAccess.UploadValidTo;
                        }
                        else
                        {
                            fss.AllowedByNIK  = null;
                            fss.UploadValidTo = null;
                        }

                        repoFSS.Update(fss);
                    }

                    listValAccess.Add(new SupervisorNIKValUpdateAccess()
                    {
                        NIK       = fss.NIK,
                        IsAllowed = fss.AllowedByNIK == null ? false : true,
                        FormattedUploadValidTo = fss.UploadValidTo == null ? "" : fss.UploadValidTo.Value.ToString(AppConstant.DefaultFormatDate)
                    });
                }

                _unitOfWork.Commit();

                alert.Status = 1;
                alert.Data   = listValAccess;
            }
            catch (Exception ex)
            {
                _logger.Write("error", DateTime.Now, ex.Message, _userAuth.Fullname, ex);
                alert.Text = StaticMessage.ERR_SAVE_FAILED;
            }
            finally
            {
                _unitOfWork.Dispose();
            }

            return(alert);
        }