示例#1
0
        /// <summary>
        /// Gets the Notes of the entity
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="input">The input.</param>
        /// <param name="noteType">The noteType.</param>
        /// <param name="sortOrder">The sort order.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static List<Note> Notes( DotLiquid.Context context, object input, object noteType, string sortOrder = "desc", int? count = null )
        {
            int? entityId = null;

            if ( input is int )
            {
                entityId = Convert.ToInt32( input );
            }
            if ( input is IEntity )
            {
                IEntity entity = input as IEntity;
                entityId = entity.Id;
            }
            if ( !entityId.HasValue )
            {
                return null;
            }

            List<int> noteTypeIds = new List<int>();

            if ( noteType is int )
            {
                noteTypeIds.Add( (int)noteType );
            }

            if ( noteType is string )
            {
                noteTypeIds = ((string)noteType).Split( ',' ).Select( Int32.Parse ).ToList();
            }

            var notes = new NoteService( new RockContext() ).Queryable().Where( n => n.EntityId == entityId );

            if ( noteTypeIds.Count > 0 )
            {
                notes = notes.Where( n => noteTypeIds.Contains( n.NoteTypeId ) );
            }
            else
            {
                return null;
            }

            // add sort order
            if(sortOrder == "desc" )
            {
                notes = notes.OrderByDescending( n => n.CreatedDateTime );
            }
            else
            {
                notes = notes.OrderBy( n => n.CreatedDateTime );
            }

            var filterNotes = new List<Note>();
            foreach ( var note in notes )
            {
                if ( note.IsAuthorized( Authorization.VIEW, GetCurrentPerson( context ) ) )
                {
                    filterNotes.Add( note );
                }
            }

            if ( !count.HasValue )
            {
                return filterNotes;
            }
            else
            {
                return filterNotes.OrderBy(n => n.CreatedDateTime).Take( count.Value ).ToList();
            }
        }