Beispiel #1
0
        }// create a user passed on params

        public async Task <bool> DeleteAsync(User user)
        {
            ObjectEmpty(user);
            if (int.TryParse(user.Id, out int id))
            {
                SqlUser sqlUser = await dbContext.SqlUsers.FirstOrDefaultAsync(x => x.Id == id);

                ObjectEmptyFromDb(sqlUser);
                if (sqlUser.DeactivationDate != null)
                {
                    throw new Exception(Resource.user_already_disabled);
                }

                sqlUser.DeactivationDate = DateTime.UtcNow;
                sqlUser.LastEdit         = DateTime.UtcNow;
                foreach (var ass in sqlUser.SqlAssignements.Where(x => x.DeactivationDate == null))
                {
                    ass.DeactivationDate = DateTime.UtcNow;
                    ass.LastEdit         = DateTime.UtcNow;
                }
                // mapper.Map(user, sqlUser);
                await dbContext.SaveChangesAsync();

                return(true);
            }
            throw new NullReferenceException(Resource.InvalidOperation);
        } // set deactivation date of a specific user
Beispiel #2
0
        }  // return all users present on db that are enabled

        public async Task <User> GetAsync(User user)
        {
            if (int.TryParse(user.Id, out int userId))
            {
                SqlUser sqlUser = await dbContext.SqlUsers.FirstOrDefaultAsync(x => x.Id == userId);

                ObjectEmptyFromDb(sqlUser);
                if (sqlUser.DeactivationDate == null)
                {
                    return(mapper.Map <User>(sqlUser));
                }
                throw new NullReferenceException(Resource.ObjectNullFromDb);
            }
            throw new NullReferenceException(Resource.InvalidOperation);
        } // return the user passed on params
Beispiel #3
0
        public async Task <User> CreateUserAsync(User user)
        {
            ObjectEmpty(user);
            SqlUser sqlUser = new SqlUser();

            user.LastEdit     = DateTime.UtcNow;
            user.CreationDate = DateTime.UtcNow;

            mapper.Map(user, sqlUser);

            dbContext.SqlUsers.Add(sqlUser);
            await dbContext.SaveChangesAsync();

            return(mapper.Map <User>(sqlUser));
        }// create a user passed on params
Beispiel #4
0
        public async Task <bool> ResetUserAsync(User user)
        {
            if (int.TryParse(user.Id, out int userId))
            {
                SqlUser sqlUser = await dbContext.SqlUsers.FirstOrDefaultAsync(x => x.Id == userId);

                sqlUser.DeactivationDate = null;
                foreach (var assignement in sqlUser.SqlAssignements)
                {
                    assignement.DeactivationDate = null;
                    await dbContext.SaveChangesAsync();
                }
                await dbContext.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
Beispiel #5
0
        } // set deactivation date of a specific user

        public async Task <User> EditAsync(User user)
        {
            ObjectEmpty(user);
            if (int.TryParse(user.Id, out int myId))
            {
                SqlUser sqlUser = await dbContext.SqlUsers.Where(x => x.DeactivationDate == null).FirstOrDefaultAsync(x => x.Id == myId);

                ObjectEmptyFromDb(sqlUser);
                if (sqlUser.DeactivationDate == null)
                {
                    user.LastEdit = DateTime.Now;
                    mapper.Map(user, sqlUser);
                    await dbContext.SaveChangesAsync();

                    return(mapper.Map <User>(sqlUser));
                }
                throw new NullReferenceException(Resource.ObjectNullFromDb);
            }
            throw new NullReferenceException(Resource.InvalidOperation);
        }  // edit a user by changes passed on params
Beispiel #6
0
        } // create new relation between user and account

        public async Task <ICollection <Assignement> > AssignementsByUserIdAsync(string id)
        {
            if (int.TryParse(id, out int userId))
            {
                List <Assignement> listAssignement = new List <Assignement>();
                SqlUser            sqlUser         = await dbContext.SqlUsers.FirstOrDefaultAsync(x => x.Id == userId);

                ObjectEmptyFromDb(sqlUser);
                ICollection <SqlAssignement> sqlAssignements = dbContext.SqlAssignements.Where(x => x.SqlUser == sqlUser).Where(x => x.DeactivationDate == null).ToList();
                if (sqlAssignements == null)
                {
                    throw new NullReferenceException(Resource.ObjectNullFromDb);
                }
                foreach (var acc in sqlAssignements)
                {
                    listAssignement.Add(mapper.Map <Assignement>(acc));
                }
                return(listAssignement);
            }
            throw new NullReferenceException(Resource.InvalidOperation);
        } //return a collection of user relation