Rock.CheckIn.KioskLocationAttendance.Read C# (CSharp) Method

Read() public static method

Reads the specified id.
public static Read ( int id ) : KioskLocationAttendance
id int The id.
return KioskLocationAttendance
        public static KioskLocationAttendance Read( int id )
        {
            string cacheKey = KioskLocationAttendance.CacheKey( id );

            ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default;
            KioskLocationAttendance locationAttendance = cache[cacheKey] as KioskLocationAttendance;

            if ( locationAttendance != null )
            {
                return locationAttendance;
            }
            else
            {
                using ( var rockContext = new Rock.Data.RockContext() )
                {
                    var location = new LocationService( rockContext ).Get( id );
                    if ( location != null )
                    {
                        locationAttendance = new KioskLocationAttendance();
                        locationAttendance.LocationId = location.Id;
                        locationAttendance.LocationName = location.Name;
                        locationAttendance.Groups = new List<KioskGroupAttendance>();

                        var attendanceService = new AttendanceService( rockContext );
                        foreach ( var attendance in attendanceService.GetByDateAndLocation( RockDateTime.Today, location.Id ) )
                        {
                            AddAttendanceRecord( locationAttendance, attendance );
                        }

                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes( 2 );
                        cache.Set( cacheKey, locationAttendance, cachePolicy );

                        return locationAttendance;
                    }
                }
            }

            return null;
        }

Usage Example

 /// <summary>
 /// Adds the attendance.
 /// </summary>
 /// <param name="attendance">The attendance.</param>
 public static void AddAttendance(Attendance attendance)
 {
     if (attendance.LocationId.HasValue)
     {
         var location = KioskLocationAttendance.Read(attendance.LocationId.Value);
         if (location != null)
         {
             AddAttendanceRecord(location, attendance);
         }
     }
 }