public async Task <ProjectDto> GetProject(int projectId)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                IEnumerable <EmployeeInitialsDto> projectEmployees = context.UserInformation
                                                                     .Where(u => u.UserProjectRelation.Any(ur => ur.ProjectId == projectId))
                                                                     .Select(u => new EmployeeInitialsDto
                {
                    UserId     = u.UserId,
                    FirstName  = u.Name,
                    SecondName = u.SurName
                });

                ProjectDto project = await context.UserProject
                                     .Where(p => p.ProjectId == projectId)
                                     .Select(p => new ProjectDto
                {
                    Name        = p.Name,
                    ProjectId   = p.ProjectId,
                    StatusId    = p.StatusId,
                    CreateDate  = p.CreateDate,
                    EndDate     = p.EndDate,
                    Description = p.Description,
                    Employees   = projectEmployees,
                    CreatorId   = p.CreatorId
                })
                                     .FirstOrDefaultAsync();

                return(project);
            }
        }
        public async Task CreateUserAsync(RegisterDto registrationDto)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                if (registrationDto == null)
                {
                    throw new ArgumentNullException();
                }

                var company = await FindCompanyByName(registrationDto.CompanyName);

                string salt         = _hasher.GenerateSalt();
                string passwordHash = _hasher.ComputeHash(registrationDto.Password, salt);

                context.UserInformation.Add(new UserInformation
                {
                    RoleId      = ManagerRoleId,
                    CompanyId   = company.CompanyId,
                    ManagerId   = DefaultManagerId,
                    Email       = registrationDto.Email,
                    PhoneNumber = registrationDto.Phone,
                    Name        = registrationDto.FirstName,
                    SurName     = registrationDto.LastName,
                    Password    = passwordHash,
                    Salt        = salt,
                    IsConfirmed = true
                });
                await context.SaveChangesAsync();
            }
            await UpdateUser(registrationDto.Email);
        }
        public async Task UpdateProject(ProjectDto updatedProject)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                var project = new UserProject
                {
                    ProjectId   = updatedProject.ProjectId,
                    Name        = updatedProject.Name,
                    StatusId    = updatedProject.StatusId,
                    CreateDate  = updatedProject.CreateDate,
                    EndDate     = updatedProject.EndDate,
                    Description = updatedProject.Description,
                    CreatorId   = updatedProject.CreatorId
                };

                context.Entry(project).State = EntityState.Modified;
                await context.SaveChangesAsync();

                IEnumerable <int> employeesId = updatedProject.Employees.Select(e => e.UserId);

                foreach (int id in employeesId)
                {
                    context.UserProjectRelation.Add(new UserProjectRelation
                    {
                        UserId    = id,
                        ProjectId = updatedProject.ProjectId
                    });
                }
                await context.SaveChangesAsync();
            }
        }
        public async Task <int> CreateNewProject(ProjectDto project)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                var newProject = new UserProject
                {
                    StatusId    = project.StatusId,
                    Name        = project.Name,
                    CreateDate  = project.CreateDate,
                    EndDate     = project.EndDate,
                    Description = project.Description,
                    CreatorId   = project.CreatorId
                };

                context.UserProject.Add(newProject);

                IEnumerable <int> employeesId = project.Employees.Select(e => e.UserId);

                foreach (int empId in employeesId)
                {
                    context.UserProjectRelation.Add(new UserProjectRelation
                    {
                        UserId    = empId,
                        ProjectId = project.ProjectId
                    });
                }

                await context.SaveChangesAsync();

                return(newProject.ProjectId);
            }
        }
Esempio n. 5
0
        public async Task Save(TEntity entity)
        {
            using (IWorkFlowDbContext context = ContextFactory.CreateContext())
            {
                context.Set <TEntity>().Add(entity);

                await context.SaveChangesAsync();
            }
        }
Esempio n. 6
0
 public async Task <IEnumerable <UserProject> > GetProjects(int managerId)
 {
     using (IWorkFlowDbContext context = ContextFactory.CreateContext())
     {
         return(await context.UserProject
                .Where(p => p.CreatorId == managerId)
                .ToListAsync());
     }
 }
Esempio n. 7
0
 public async Task <IEnumerable <UserReport> > GetReports(int userId)
 {
     using (IWorkFlowDbContext context = ContextFactory.CreateContext())
     {
         return(await context.UserReport
                .Where(r => r.UserId == userId)
                .ToListAsync());
     }
 }
        public async Task <List <UserRole> > GetRolesList()
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                List <UserRole> roles = await context.UserRole.ToListAsync();

                return(roles);
            }
        }
Esempio n. 9
0
        public async Task SaveMany(IEnumerable <TEntity> entities)
        {
            using (IWorkFlowDbContext context = ContextFactory.CreateContext())
            {
                context.Set <TEntity>().AddRange(entities);

                await context.SaveChangesAsync();
            }
        }
Esempio n. 10
0
        public async Task <CompanyInformation> FindCompanyByName(string companyName)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                var company = await context.CompanyInformation
                              .FirstOrDefaultAsync(ci => ci.Name == companyName);

                return(company);
            }
        }
Esempio n. 11
0
        public async Task <List <string> > GetUserRoles()
        {
            List <string> roles;

            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                roles = await context.UserRole.Select(r => r.Name).ToListAsync();
            }
            return(roles);
        }
Esempio n. 12
0
        public async Task <CompanyInformation> FindCompanyById(int id)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                var company = await context.CompanyInformation
                              .FirstOrDefaultAsync(ci => ci.CompanyId == id);

                return(company);
            }
        }
Esempio n. 13
0
        public async Task <UserInformation> FindUserByEmail(string userEmail)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                var user = await context.UserInformation
                           .FirstOrDefaultAsync(ui => ui.Email == userEmail);

                return(user);
            }
        }
Esempio n. 14
0
 public async Task SaveToken(int userId, string token)
 {
     using (IWorkFlowDbContext context = _db.CreateContext())
     {
         context.RegistrationToken.Add(new RegistrationToken
         {
             Token  = token,
             UserId = userId
         });
         await context.SaveChangesAsync();
     }
 }
Esempio n. 15
0
 public async Task <IReadOnlyList <IRoleDto> > GetUserRolesFull()
 {
     using (IWorkFlowDbContext context = _db.CreateContext())
     {
         return(await context.UserRole
                .Where(ur => ur.Name == "Manager" || ur.Name == "Employee")
                .Select(ur => new RoleDto
         {
             RoleId = ur.RoleId,
             RoleName = ur.Name
         }).ToListAsync());
     }
 }
Esempio n. 16
0
 public async Task <IReadOnlyList <WorkItemStatusDto> > FetchTaskStatuses()
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         return(await _context.TaskStatus
                .Select(s => new WorkItemStatusDto
         {
             Id = s.StatusId,
             Name = s.Name
         })
                .ToListAsync());
     }
 }
Esempio n. 17
0
 public async Task <IReadOnlyCollection <EmployeeInitialsDto> > GetEmployees(int managerId, int[] employeesId)
 {
     using (IWorkFlowDbContext context = _db.CreateContext())
     {
         return(await context.UserInformation
                .Where(ui => ui.ManagerId == managerId && ui.IsConfirmed && !employeesId.Contains(ui.UserId))
                .Select(ui => new EmployeeInitialsDto
         {
             UserId = ui.UserId,
             FirstName = ui.Name,
             SecondName = ui.SurName
         }).ToListAsync());
     }
 }
Esempio n. 18
0
        public async Task UpdateEmployeeInfo(UserInfoDto userToUpdate)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                UserInformation user = context.UserInformation
                                       .Find(userToUpdate.UserId);

                user.Name    = userToUpdate.Name;
                user.SurName = userToUpdate.SurName;
                user.RoleId  = userToUpdate.RoleId;

                await context.SaveChangesAsync();
            }
        }
Esempio n. 19
0
 public async Task <IReadOnlyList <IProjectDto> > FetchEmployeeProjectsList(int userId)
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         return(await _context.UserProject
                .Where(p => p.UserProjectRelation
                       .Any(r => r.UserId == userId))
                .Select(p => new ProjectNameDto
         {
             ProjectId = p.ProjectId,
             Name = p.Name,
         })
                .ToListAsync());
     }
 }
Esempio n. 20
0
        public async Task <bool> TokenExists(string token)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                if (token == null)
                {
                    throw new ArgumentNullException();
                }

                var existingToken = await context.RegistrationToken
                                    .FirstOrDefaultAsync(rt => rt.Token == token);

                return(existingToken != null);
            }
        }
Esempio n. 21
0
 public async Task <IReadOnlyList <IEmployeeDto> > FetchEmployeeNames(int managerId)
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         return(await _context.UserInformation
                .Where(t => t.IsConfirmed &&
                       t.ManagerId == managerId)
                .Select(t => new EmployeeInitialsDto
         {
             UserId = t.UserId,
             FirstName = t.Name,
             SecondName = t.SurName
         })
                .ToListAsync());
     }
 }
Esempio n. 22
0
        public async Task CreateCompanyAsync(RegisterDto registrationDto)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                if (registrationDto == null)
                {
                    throw new ArgumentNullException();
                }

                context.CompanyInformation.Add(new CompanyInformation
                {
                    Name = registrationDto.CompanyName
                });
                await context.SaveChangesAsync();
            }
        }
Esempio n. 23
0
        public async Task <UserInformation> FindUser(string userEmail, string password)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                if (await FindUserByEmail(userEmail) != null)
                {
                    var salt = await GetSaltAsync(userEmail);

                    var hash = _hasher.ComputeHash(password, salt);
                    var user = await context.UserInformation
                               .FirstOrDefaultAsync(ui =>
                                                    ui.Email == userEmail && ui.Password == hash);

                    return(user);
                }
                return(null);
            }
        }
Esempio n. 24
0
 public async Task CreateOrUpdateTaskInformation(TaskDto taskObject)
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         UserTask task = new UserTask
         {
             TaskId      = taskObject.TaskId,
             Name        = taskObject.TaskName,
             ProjectId   = taskObject.ProjectId,
             UserId      = taskObject.UserId,
             StatusId    = taskObject.StatusId,
             Description = taskObject.Description
         };
         _context.Entry(task).State = (taskObject.TaskId != 0) ?
                                      EntityState.Modified :
                                      EntityState.Added;
         await _context.SaveChangesAsync();
     }
 }
Esempio n. 25
0
        public async Task SelfRegisterUserAsync(SelfRegistrationDto selfRegistrationDto)
        {
            using (IWorkFlowDbContext context = _db.CreateContext())
            {
                if (selfRegistrationDto == null)
                {
                    throw new ArgumentNullException();
                }

                var token = await context.RegistrationToken
                            .FirstOrDefaultAsync(rt => rt.Token == selfRegistrationDto.RegistrationToken);

                if (token == null)
                {
                    throw new ArgumentNullException();
                }

                string salt         = _hasher.GenerateSalt();
                string passwordHash = _hasher.ComputeHash(selfRegistrationDto.Password, salt);

                var userId = token.UserId;

                var user = await context.UserInformation
                           .FirstOrDefaultAsync(ui => ui.UserId == userId);

                if (user != null)
                {
                    user.Password    = passwordHash;
                    user.Salt        = salt;
                    user.Name        = selfRegistrationDto.FirstName;
                    user.SurName     = selfRegistrationDto.LastName;
                    user.Address     = selfRegistrationDto.Address;
                    user.PhoneNumber = selfRegistrationDto.PhoneNumber;
                    user.IsConfirmed = true;
                    context.RegistrationToken.Remove(token);
                    await context.SaveChangesAsync();
                }
                else
                {
                    throw new ArgumentNullException();
                }
            }
        }
Esempio n. 26
0
 public async Task <UserInfoDto> FetchEmployeeInfo(int userId)
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         return(await _context.UserInformation
                .Where(u => u.UserId == userId)
                .Select(u => new UserInfoDto
         {
             UserId = u.UserId,
             Name = u.Name,
             SurName = u.SurName,
             Address = u.Address,
             PhoneNumber = u.PhoneNumber,
             Email = u.Email,
             RoleId = u.RoleId,
             RoleName = _context.UserRole
                        .Where(r => r.RoleId == u.RoleId)
                        .Select(r => r.Name).FirstOrDefault()
         })
                .FirstOrDefaultAsync());
     }
 }
Esempio n. 27
0
 public async Task <ITaskDto> FetchEmployeeTask(int taskId)
 {
     using (IWorkFlowDbContext _context = _db.CreateContext())
     {
         return(await _context.UserTask
                .Where(t => t.TaskId == taskId)
                .Select(t => new TaskDescriptionDto
         {
             TaskId = taskId,
             TaskName = t.Name,
             ProjectId = t.ProjectId,
             ProjectName = t.UserProject.Name,
             StatusId = t.StatusId,
             StatusName = t.TaskStatus.Name,
             Description = t.Description,
             EmployeeId = t.UserId,
             EmployeeFirstName = t.UserInformation.Name,
             EmployeeSecondName = t.UserInformation.SurName
         })
                .FirstOrDefaultAsync());
     }
 }
Esempio n. 28
0
        public async Task <IPagerListDto> FetchEmployeeProjects(int userId, int pageNum, int numPerPage)
        {
            using (IWorkFlowDbContext _context = _db.CreateContext())
            {
                int totalProjects = await _context.UserProjectRelation
                                    .Where(upr => upr.UserId == userId)
                                    .CountAsync();

                var projectItems = await _context.UserProject
                                   .Where(p => p.UserProjectRelation
                                          .Any(r => r.UserId == userId))
                                   .OrderBy(p => p.ProjectId)
                                   .Skip((pageNum - 1) * numPerPage)
                                   .Take(numPerPage)
                                   .Select(p => new ProjectWithTasksDto
                {
                    ProjectId   = p.ProjectId,
                    ProjectName = p.Name,
                    CreateDate  = p.CreateDate,
                    Tasks       = p.UserTask
                                  .Where(t => t.UserId == userId)
                                  .Select(t => new TaskNameDto
                    {
                        TaskId   = t.TaskId,
                        TaskName = t.Name
                    })
                })
                                   .ToListAsync();

                return(new PagerListDto
                {
                    TotalCount = totalProjects,
                    Items = projectItems
                });
            }
        }
Esempio n. 29
0
 public SelfRegistrationServiceFixture BuildWorkFlowDbContextFactory(IWorkFlowDbContext contextToReturn)
 {
     Mock.Get(dbFactory).Setup(f => f.CreateContext()).Returns(contextToReturn);
     return(this);
 }