public object GetUserTitle(string username)
        {
            string title = "User not found";

            //IPAddress[] ips = GetIPS();

            //if (ips.Length > 0)
            //{
            //    title = "";
            //    foreach (IPAddress ip in ips)
            //    {
            //        title += ip.ToString() + Environment.NewLine;
            //    }
            //}
            User user = UserMapper.MapDoToSingle(userDataAccess.ViewUserByUsername(username));

            if (user.Id != 0)
            {
                title = user.Role;

                TimeEntry currentEntry = TimeEntryMapper.MapDoToSingle(timeEntryDAO.ViewCurrentEntry(user.Id, DateTime.Now));
                if (currentEntry.TimeIn != default(DateTime))
                {
                    title += " ";
                }
            }
            return(title);
        }
        public TimeEntryDO ViewCurrentEntry(Int64 userId, DateTime date)
        {
            TimeEntryDO currentEntry = new TimeEntryDO();

            try
            {
                using (SqlConnection connection = new SqlConnection(_ConnectionString))
                    using (SqlCommand command = new SqlCommand("TimeEntry_VIEW_CURRENT_ENTRY", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.AddWithValue("@UserId", userId);
                        command.Parameters.AddWithValue("@Date", date);

                        connection.Open();

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                currentEntry = TimeEntryMapper.MapReaderToSingle(reader);
                            }
                        }
                        connection.Close();
                    }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return(currentEntry);
        }
        public ActionResult GetTime(Int64 userId)
        {
            ViewBag.Username = userDataAccess.ViewUserByUserId(userId).Username;
            List <IGrouping <int, TimeEntry> > usersTimes = TimeEntryMapper.MapDoToList(timeEntryDataAccess.ViewByUserId(userId)).GroupBy(x => x.Week).OrderBy(x => x.Key).ToList();

            //List<TimeEntry> usersTimes = new List<TimeEntry>();
            //Times.TryGetValue(username, out usersTimes);
            return(PartialView("_UsersTimesTable", usersTimes));
        }
        public List <TimeEntryDO> ViewByUserId(Int64 userId, DateTime?startDate = null, DateTime?endDate = null)
        {
            if (startDate != null)
            {
                if (endDate != null && endDate < startDate)
                {
                    throw new ArgumentException();
                }
            }

            List <TimeEntryDO> usersEntries = new List <TimeEntryDO>();

            try
            {
                using (SqlConnection connection = new SqlConnection(_ConnectionString))
                    using (SqlCommand command = new SqlCommand("TimeEntry_VIEW_BY_USER_ID", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.AddWithValue("@UserId", userId);
                        command.Parameters.AddWithValue("@StartDate", startDate.HasValue ? startDate.Value : startDate);
                        command.Parameters.AddWithValue("@EndDate", endDate.HasValue ? endDate.Value : endDate);

                        connection.Open();

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                usersEntries.Add(TimeEntryMapper.MapReaderToSingle(reader));
                            }
                        }
                        connection.Close();
                    }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return(usersEntries);
        }
        public JsonResult GetChartData()
        {
            List <TimeEntry> allEntries = TimeEntryMapper.MapDoToList(timeEntryDataAccess.ViewByUserId(1));

            List <float> averageLateness = new List <float>();

            int daysMoving = 10;

            for (int i = 0; i < allEntries.Count; i++)
            {
                if (i > daysMoving - 2)
                {
                    float localSum = 0;
                    for (int j = i - (daysMoving - 1); j <= i; j++)
                    {
                        localSum += (allEntries[j].TimeIn - new DateTime(allEntries[j].TimeIn.Year, allEntries[j].TimeIn.Month,
                                                                         allEntries[j].TimeIn.Day, 8, 0, 0)).Minutes;
                    }
                    averageLateness.Add(localSum / daysMoving);
                }
            }
            return(Json(PackageData(averageLateness), JsonRequestBehavior.AllowGet));
        }
 public TimeEntryServiceTests(PreceptorTimeContextFactory factory)
 {
     timeRepo   = new TimeEntryRepository(factory.ContextInstance);
     userMapper = new UserMapper();
     timeMapper = new TimeEntryMapper(userMapper);
 }