Пример #1
0
        public virtual async Task <IResponseResult> AddAsync(TDto model)
        {
            try
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty("Id");
                if (propertyInfo.PropertyType == typeof(string))
                {
                    propertyInfo.SetValue(model, Convert.ChangeType(Guid.NewGuid().ToString(), propertyInfo.PropertyType), null);
                }

                T entity = Mapper.Map <TDto, T>(model);
                _unitOfWork.Repository.Add(entity);
                int affectedRows = await _unitOfWork.SaveChanges();

                if (affectedRows > 0)
                {
                    result = new ResponseResult(result: null, status: HttpStatusCode.Created, message: "Data Inserted Successfully");
                }

                result.Data = model;
                return(result);
            }
            catch (Exception e)
            {
                result.Message = e.InnerException != null ? e.InnerException.Message : e.Message;
                result         = new ResponseResult(null, HttpStatusCode.InternalServerError, e, result.Message);
                return(result);
            }
        }
Пример #2
0
        public async Task <IResponseResult> SaveScreens(ScreensAssignedParameters parameters)
        {
            if (parameters.ScreenAssigned != null)
            {
                foreach (var ScreenId in parameters.ScreenAssigned)
                {
                    var isExists = await _menuRoleUnitOfWork.Repository.FirstOrDefaultAsync(q => q.MenuId == ScreenId && q.RoleId == parameters.RoleId) != null;

                    if (!isExists)
                    {
                        var obj = new MenuRoles()
                        {
                            Id = Guid.NewGuid().ToString(), RoleId = parameters.RoleId, MenuId = ScreenId
                        };
                        _menuRoleUnitOfWork.Repository.Add(obj);
                    }
                }
            }
            if (parameters.ScreenAssignedRemove != null)
            {
                var dataRemoved = await _menuRoleUnitOfWork.Repository.FindAsync(q => parameters.ScreenAssignedRemove.Contains(q.MenuId) && q.RoleId == parameters.RoleId);

                _menuRoleUnitOfWork.Repository.RemoveRange(dataRemoved);
            }

            await _menuRoleUnitOfWork.SaveChanges();

            return(ResponseResult.GetRepositoryActionResult(true, status: HttpStatusCode.Created, message: HttpStatusCode.Created.ToString()));
        }
Пример #3
0
        public async Task <IResponseResult> SaveUserAssigned(AssignUserOnRoleParameters parameters)
        {
            var role = await _roleUnitOfWork.Repository.FirstOrDefaultAsync(q => q.Id == parameters.RoleId, include : source => source.Include(a => a.AspNetUsersRoles), disableTracking : false);

            if (parameters.AssignedUser != null)
            {
                foreach (var user in parameters.AssignedUser)
                {
                    var isExist = role.AspNetUsersRoles.Any(q => q.UserId == user);
                    if (!isExist)
                    {
                        var userRole = new AspNetUsersRoles()
                        {
                            Id = Guid.NewGuid().ToString(), UserId = user, RoleId = parameters.RoleId
                        };
                        role.AspNetUsersRoles.Add(userRole);
                    }
                }
            }
            if (role.AspNetUsersRoles.Any())
            {
                await _roleUnitOfWork.SaveChanges();
            }

            var userRemove = parameters.AssignedUser is null ? role.AspNetUsersRoles : role.AspNetUsersRoles.Where(q => !parameters.AssignedUser.Contains(q.UserId));

            if (userRemove.Any())
            {
                _userRolesUnitOfWork.Repository.RemoveRange(userRemove);
                await _userRolesUnitOfWork.SaveChanges();
            }

            return(ResponseResult.GetRepositoryActionResult(true, status: HttpStatusCode.Created, message: HttpStatusCode.Created.ToString()));
        }