Exemple #1
0
        public ActionResult UpdateAccess(ASMUpdateAccessViewModel model)
        {
            if (!Request.IsAjaxRequest())
            {
                return(RedirectToAction("Index"));
            }

            AlertMessage alert = new AlertMessage();

            ASMBusiness business = new ASMBusiness();

            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));
        }
Exemple #2
0
        public AlertMessage UpdateAccess(ASMUpdateAccessViewModel 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 <ASM> repoASM = _unitOfWork.GetRepository <ASM>();

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

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

            repoASM.Condition = PredicateBuilder.True <ASM>().And(orCondition);

            List <ASM> listASM = repoASM.Find();

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

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

            try
            {
                _unitOfWork.BeginTransaction();

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

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

                        repoASM.Update(asm);
                    }

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

                _unitOfWork.Commit();

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

            return(alert);
        }