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);
        }
Exemple #2
0
        public static TimeEntry MapDoToSingle(TimeEntryDO from)
        {
            TimeEntry to = new TimeEntry();

            to.Id      = from.Id;
            to.TimeIn  = from.TimeIn;
            to.TimeOut = from.TimeOut;
            to.UserId  = from.UserId;
            return(to);
        }
Exemple #3
0
        public static TimeEntryDO MapReaderToSingle(SqlDataReader reader)
        {
            TimeEntryDO oResult = new TimeEntryDO();

            oResult.Id     = reader.GetInt64(0);
            oResult.UserId = reader.GetInt64(1);
            oResult.TimeIn = reader.GetDateTime(2);
            if (!reader.IsDBNull(3))
            {
                oResult.TimeOut = reader.GetDateTime(3);
            }
            return(oResult);
        }
        public ActionResult ClockIn(LoginForm form)
        {
            bool   successful = false;
            string message = string.Empty;
            var    userTimeEntries = new[] { new { TimeIn = "", TimeOut = "" } }.ToList();

            if (ModelState.IsValid)
            {
                UserDO user = userDataAccess.ViewUserByUsername(form.Username);
                if (user != null && user.Id != 0 && user.Password == form.Password)
                {
                    successful = true;
                    //Attempt to get todays entry, if none create one.

                    TimeEntryDO currentEntry = timeEntryDataAccess.ViewCurrentEntry(user.Id, DateTime.Now);

                    if (currentEntry.Id != 0)
                    {
                        currentEntry.TimeOut = DateTime.Now;
                        timeEntryDataAccess.Update(currentEntry);
                        message = "Successfully logged out";
                        //update.
                    }
                    else
                    {
                        currentEntry.UserId = user.Id;
                        currentEntry.TimeIn = DateTime.Now;
                        timeEntryDataAccess.Create(currentEntry);
                        message = "Successfully logged in";
                    }
                    userTimeEntries = timeEntryDataAccess.ViewByUserId(user.Id, new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), DateTime.Now)
                                      .Select(t => new { TimeIn = t.TimeIn.ToString("hh:mm:ss") + " " + t.TimeIn.ToString("tt", CultureInfo.InvariantCulture), TimeOut = t.TimeOut.ToString("hh:mm:ss") + " " + t.TimeOut.ToString("tt", CultureInfo.InvariantCulture) }).OrderByDescending(t => t.TimeIn).Take(4).ToList();
                    userTimeEntries.Reverse();
                }
                else
                {
                    successful = false;
                    message    = "Username or password is incorrect.";
                }
            }
            else
            {
                successful = false;
                message    = "Form not filled out properly.";
            }
            return(Json(new { success = successful, message = message, timeEntries = userTimeEntries }));
        }
        public void Create(TimeEntryDO timeEntry)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(_ConnectionString))
                    using (SqlCommand command = new SqlCommand("TimeEntry_CREATE", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.AddWithValue("@UserId", timeEntry.UserId);
                        command.Parameters.AddWithValue("@TimeIn", timeEntry.TimeIn);
                        command.Parameters.AddWithValue("@TimeOut", timeEntry.TimeOut == default(DateTime) ? (object)DBNull.Value : timeEntry.TimeOut);

                        connection.Open();
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }