예제 #1
0
        public async Task <IActionResult> Add(Roles roles)
        {
            bool res = false;

            roles.UpdateDate = DateTime.Now;
            if (roles.Id > 0)
            {
                res = await _rolesServices.Update(roles);
            }
            else
            {
                var data = await _rolesServices.QueryByClause(m => m.RoleName == roles.RoleName);

                if (data != null)
                {
                    return(Error(roles.RoleName + "已经存在!"));
                }
                res = await _rolesServices.Add(roles);
            }
            if (res)
            {
                return(Success("保存成功"));
            }
            else
            {
                return(Error("保存失败"));
            }
        }
        public ActionResult Submit(RolesViewModel roles)
        {
            if (!caSession.AuthoriseSession())
            {
                return(Redirect((string)Session["ErrorUrl"]));
            }

            // get properties of current tenant
            var tenant = caCurrent.CurrentTenant();

            // get properties of user
            caUser user = caCurrent.CurrentUser();

            try
            {
                if (ModelState.IsValid)
                {
                    if (roles.Id <= 0)
                    {
                        //insert
                        roles.CreatedBy   = user.UserId;
                        roles.UpdatedBy   = user.UserId;
                        roles.DateCreated = DateTime.UtcNow;
                        roles.DateUpdated = DateTime.UtcNow;
                        roles.TenantId    = tenant.TenantId;
                        roles.Id          = _rolesServices.Insert(Mapper.Map <Roles>(roles), CurrentUserId);

                        ViewBag.Message = $"Successfully Added on {DateTime.Now}.";
                    }
                    else
                    {
                        //update
                        Roles newRole = _rolesServices.GetByRolesId(roles.Id);
                        newRole.RoleName    = roles.RoleName;
                        newRole.TenantId    = tenant.TenantId;
                        newRole.UpdatedBy   = user.UserId;
                        newRole.DateUpdated = DateTime.UtcNow;
                        _rolesServices.Update(Mapper.Map <Roles>(newRole), CurrentUserId);
                        ViewBag.Message = $"Successfully Updated on {DateTime.Now}.";
                    }

                    return(RedirectToAction("Index", new { message = ViewBag.Message }));
                }
                else //ModelState.IsValid is not valid
                {
                    return(View("_CreateEdit", Mapper.Map(_rolesServices.GetByRolesId(roles.Id), new RolesViewModel())));
                }
            }
            catch (Exception e)
            {
                //log exception
                var err = e.Message;

                return(View("_ErrorResult"));
            }
        }
        public async Task <IActionResult> Put(int id, RolesDTO rolesDto)
        {
            try
            {
                var roles = _mapper.Map <Roles>(rolesDto);
                roles.Rol_Id = id;

                await _rolesServices.Update(roles);

                var response = new GenericResponse <bool>(true);
                return(Ok(response));
            }
            catch (Exception ex)
            {
                throw new BusinessException(MessageCodes.PROPERTY_NO_VALID, GetErrorDescription(MessageCodes.PROPERTY_NO_VALID), ex.Message);
            }
        }