public async Task <ActionResult> UpdateUser(Dto.WorkMontForUpdate workMonth)
        {
            if (workMonth == null)
            {
                return(BadRequest());
            }
            Models.WorkMonth workMonthModel = new Models.WorkMonth {
                Id = workMonth.Id, Accepted = workMonth.Accepted, Month = workMonth.Month, UserId = workMonth.UserId
            };
            if (await Repo.UpdateWorkMonth(workMonthModel))
            {
                if (!workMonth.Accepted)
                {
                    Models.User user = await Repo.GetUserFromWorkMonth(workMonthModel);

                    if (user == null)
                    {
                        return(BadRequest());
                    }
                    if (Repo.SendMail(workMonth.Body, user))
                    {
                        return(Ok());
                    }
                }
                return(Ok());
            }
            return(BadRequest());
        }
        public async Task <Models.WorkMonth> GetWorkMonths(Models.User user, DateTime Time)
        {
            if (user == null)
            {
                return(null);
            }
            if (user.Id == null)
            {
                return(null);
            }
            if (user.Id == "")
            {
                return(null);
            }
            int currentMonth = (Time.Year * 12) + Time.Month;

            Models.WorkMonth workMonth = null;
            try
            {
                workMonth = await TimeSheetContext.WorkMonth.Where(x => x.UserId == user.Id && x.Month == currentMonth).SingleOrDefaultAsync();
            }
            catch (Exception e)
            {
                return(null);
            }
            if (workMonth == null)
            {
                return(null);
            }
            return(workMonth);
        }
        public async Task <bool> UpdateWorkMonth(Models.WorkMonth workMonth)
        {
            if (workMonth == null)
            {
                return(false);
            }
            if (workMonth.Id == null)
            {
                return(false);
            }
            if (workMonth.Id == "")
            {
                return(false);
            }
            try
            {
                Models.WorkMonth workMonthOld = await TimeSheetContext.WorkMonth.Where(x => x.Id == workMonth.Id).SingleOrDefaultAsync();

                workMonth.Month  = workMonthOld.Month;
                workMonth.UserId = workMonthOld.UserId;
                TimeSheetContext.Entry(workMonthOld).State = EntityState.Detached;
                workMonthOld = null;
                TimeSheetContext.Update(workMonth);
                await TimeSheetContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                return(false);
            }
            //.User.Include(x => x.Logs).SingleAsync(x => x.Id == input.Id);
            return(true);
        }
        public async Task <bool> IsAllowed(Models.User user, Models.Log log)
        {
            if (user == null)
            {
                return(false);
            }
            if (log == null)
            {
                return(false);
            }
            int Month = (log.Start.Year * 12) + log.Start.Month;

            try
            {
                Models.WorkMonth workMonth = await TimeSheetContext.WorkMonth.Where(x => x.Month == Month && x.UserId == user.Id).SingleOrDefaultAsync();

                if (workMonth == null)
                {
                    return(false);
                }
                return(workMonth.Accepted);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        public async Task <ActionResult <List <Dto.UserForGetHR> > > GetConsultants()
        {
            if (User.FindFirst(ClaimTypes.Role).Value == Config.GetSection("Role:Consultant:name").Value)
            {
                return(Unauthorized());
            }

            /*
             * var users = Task.Run(()=> Repo.GetAllConsultant());
             * var workMonths = Task.Run(() => Repo.GetAllWorkMonths());
             * await Task.WhenAll(users, workMonths);
             */
            var users = await Repo.GetAllConsultant();

            var workMonths = await Repo.GetAllWorkMonths();

            List <Dto.UserForGetHR> userDto = new List <Dto.UserForGetHR>();

            if (users == null)
            {
                return(BadRequest());
            }
            foreach (Models.User user in users)
            {
                Dto.UserForGetHR userForGetHR = new UserForGetHR {
                    Id = user.Id, Email = user.Email, Name = user.Name, ChangeHistory = user.ChangeHistory
                };
                //Mapper.Map<Dto.UserForGetHR>(user);
                try
                {
                    Models.WorkMonth workMonthModel = workMonths.Where(x => x.UserId == user.Id).SingleOrDefault();
                    Dto.WorkMonth    workMonth      = new WorkMonth {
                        Id = workMonthModel.Id, Month = workMonthModel.Month, Accepted = workMonthModel.Accepted, UserId = workMonthModel.UserId
                    };
                    //Mapper.Map<Dto.WorkMonth>(workMonths.Result.Where(x => x.UserId == user.Id));
                    workMonth.Salary       = Repo.GetSalary(user);
                    workMonth.TotalHours   = Repo.GetTotalTime(user);
                    userForGetHR.WorkMonth = workMonth;
                }
                catch (Exception e)
                {
                    return(BadRequest());
                }
                userDto.Add(userForGetHR);
            }
            return(Ok(userDto));
        }
        public async Task <Models.User> GetUserFromWorkMonth(Models.WorkMonth workMonth)
        {
            string UserId = await TimeSheetContext.WorkMonth.Where(x => x.Id == workMonth.Id).Select(x => x.UserId).SingleOrDefaultAsync();

            return(await TimeSheetContext.User.Where(x => x.Id == UserId).SingleOrDefaultAsync());
        }