Exemplo n.º 1
0
        public int Delete(TEntity id)
        {
            var entity = _dbContext.Find <TEntity>(id);

            var ok = _dbContext.Remove(entity);

            Save();
            return(ok.Collections.Count());
        }
Exemplo n.º 2
0
        public IActionResult AddDoctor(Doctor doctor)
        {
            if (_context.Find <Doctor>(doctor.IdDoctor) != null)
            {
                return(NotFound("Doctor with that Id already exists"));
            }

            _context.Add <Doctor>(doctor);
            _context.SaveChanges();

            return(Ok("Doctor has been inserted to a database"));
        }
Exemplo n.º 3
0
        public IStatusGeneric DeleteRole(string roleName, bool removeFromUsers,
                                         MyDbContext context)
        {
            var status = new StatusGenericHandler {
                Message = "Deleted role successfully."
            };
            var roleToUpdate = context.Find <RoleToPermissions>(roleName);

            if (roleToUpdate == null)
            {
                return(status.AddError("That role doesn't exists"));
            }

            var usersWithRoles = context.UserToRoles.Where(x => x.RoleName == roleName).ToList();

            if (usersWithRoles.Any())
            {
                if (!removeFromUsers)
                {
                    return(status.AddError($"That role is used by {usersWithRoles.Count} and you didn't ask for them to be updated."));
                }

                context.RemoveRange(usersWithRoles);
                status.Message = $"Removed role from {usersWithRoles.Count} user and then deleted role successfully.";
            }

            context.Remove(roleToUpdate);
            return(status);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This will update a role
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="description"></param>
        /// <param name="permissions"></param>
        public void UpdateRole(string roleName, string description, ICollection <Permissions> permissions)
        {
            var existingRole = _context.Find <RoleToPermissions>(roleName);

            if (existingRole == null)
            {
                throw new KeyNotFoundException($"Could not find the role {roleName} to update.");
            }
            existingRole.Update(description, permissions);
        }
Exemplo n.º 5
0
        public static IStatusGeneric <RoleToPermissions> CreateRoleWithPermissions(string roleName, string description, ICollection <Permissions> permissionInRole,
                                                                                   MyDbContext context)
        {
            var status = new StatusGenericHandler <RoleToPermissions>();

            if (context.Find <RoleToPermissions>(roleName) != null)
            {
                status.AddError("That role already exists");
                return(status);
            }

            return(status.SetResult(new RoleToPermissions(roleName, description, permissionInRole)));
        }
Exemplo n.º 6
0
        public static IStatusGeneric <UserToRole> AddRoleToUser(string userId, string roleName, MyDbContext context)
        {
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            var status = new StatusGenericHandler <UserToRole>();

            if (context.Find <UserToRole>(userId, roleName) != null)
            {
                status.AddError($"The user already has the Role '{roleName}'.");
                return(status);
            }
            var roleToAdd = context.Find <RoleToPermissions>(roleName);

            if (roleToAdd == null)
            {
                status.AddError($"I could not find the Role '{roleName}'.");
                return(status);
            }

            return(status.SetResult(new UserToRole(userId, roleToAdd)));
        }
Exemplo n.º 7
0
 public virtual T Obter(Guid id)
 {
     return((T)_Db.Find(typeof(T), id));
 }