Extension Methods

There are a number of extension methods to help you get Content, Media and User's much easier and using less code. They are extension methods off of Content, IMediator

Content Extensions

    /// <summary>
    /// Retrieves a list of media items from a content property.
    /// </summary>
    /// <param name="content">The content item that holds the media property.</param>
    /// <param name="alias">The alias of the media property to fetch.</param>
    /// <param name="mediator">The mediator to send queries to retrieve media information.</param>
    /// <param name="fallBackUrl">A fallback URL to use if no media is found.</param>
    /// <returns>Returns a list of media items if found; otherwise, returns a list containing a single media item with the fallback URL.</returns>
    public static async Task<List<Media.Models.Media>> GetMedias(this Content.Models.Content content, string? alias,
        IMediator mediator, string? fallBackUrl = null)
    {
        if (!string.IsNullOrEmpty(alias))
        {
            var mediaIds = content.GetValue<List<Guid>>(alias);
            if (mediaIds != null && mediaIds.Count != 0)
            {
                var result = await mediator.Send(new QueryMediaCommand { Ids = mediaIds, AmountPerPage = mediaIds.Count, Cached = true });
                return result.Items.ToList();
            }   
        }
        
        if (!fallBackUrl.IsNullOrWhiteSpace())
        {
            return [new Media.Models.Media{Name = fallBackUrl, Url = fallBackUrl}];
        }

        
        return [];
    }
    
    /// <summary>
    /// Extension to get content items
    /// </summary>
    /// <param name="content"></param>
    /// <param name="propertyAlias"></param>
    /// <param name="mediator"></param>
    /// <returns></returns>
    public static async Task<List<Content.Models.Content>> GetContents(this Content.Models.Content content, string? propertyAlias, IMediator mediator)
    {
        if (!string.IsNullOrEmpty(propertyAlias))
        {
            var ids = content.GetValue<List<Guid>>(propertyAlias);
            if (ids != null && ids.Count != 0)
            {
                var result = await mediator.Send(new QueryContentCommand { Ids = ids, AmountPerPage = ids.Count, Cached = true});
                return result.Items.ToList();
            }
        }

        return [];
    }
    
    /// <summary>
    /// Extension to get users
    /// </summary>
    /// <param name="content"></param>
    /// <param name="propertyAlias"></param>
    /// <param name="mediator"></param>
    /// <returns></returns>
    public static async Task<List<User>> GetUsers(this Content.Models.Content content, string propertyAlias, IMediator mediator)
    {
        if (!string.IsNullOrEmpty(propertyAlias))
        {
            var ids = content.GetValue<List<Guid>>(propertyAlias);
            if (ids != null && ids.Count != 0)
            {
                var result = await mediator.Send(new QueryUsersCommand { Ids = ids, AmountPerPage = ids.Count, Cached = true});
                return result.Items.ToList();
            }
        }

        return [];
    }

    /// <summary>
    /// Extension to get a single user
    /// </summary>
    /// <param name="content">The content containing user data</param>
    /// <param name="propertyAlias">The property alias to retrieve user ids</param>
    /// <param name="mediator">The mediator to handle user queries</param>
    /// <returns>A single user or null if no users are found</returns>
    public static async Task<User?> GetUser(this Content.Models.Content content, string propertyAlias, IMediator mediator)
    {
        return (await content.GetUsers(propertyAlias, mediator)).FirstOrDefault();
    }
    

    /// <summary>
    /// Extension to get a single media item
    /// </summary>
    /// <param name="content">The content containing media data</param>
    /// <param name="propertyAlias">The property alias to retrieve media ids</param>
    /// <param name="mediator">The mediator to handle media queries</param>
    /// <returns>A single media item or null if no media items are found</returns>
    public static async Task<Media.Models.Media?> GetMedia(this Content.Models.Content content, string propertyAlias, IMediator mediator)
    {
        return (await content.GetMedias(propertyAlias, mediator)).FirstOrDefault();
    }

    /// <summary>
    /// Extension to get a single content item
    /// </summary>
    /// <param name="content">The content containing content data</param>
    /// <param name="propertyAlias">The property alias to retrieve content ids</param>
    /// <param name="mediator">The mediator to handle content queries</param>
    /// <returns>A single content item or null if no content items are found</returns>
    public static async Task<Content.Models.Content?> GetContent(this Content.Models.Content content, string propertyAlias, IMediator mediator)
    {
        return (await content.GetContents(propertyAlias, mediator)).FirstOrDefault();
    }

IMediator Extensions

    /// <summary>
    /// Retrieves the global settings from the mediator.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the GetGlobalDataCommand.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains an instance of GlobalSettings.</returns>
    public static async Task<GlobalSettings> GetGlobalSettings(this IMediator mediator)
    {
        var globalData = await mediator.Send(new GetGlobalDataCommand { Alias = Constants.GlobalSettings });
        if (globalData?.Data != null)
        {
            return globalData.GetValue<GlobalSettings>() ?? new GlobalSettings();
        }

        return new GlobalSettings();
    }

    /// <summary>
    /// Saves the global settings using the mediator.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the SaveGlobalDataCommand.</param>
    /// <param name="settings">The global settings to be saved.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains a boolean indicating the success of the save operation.</returns>
    public static async Task<bool> SaveGlobalSettings(this IMediator mediator, GlobalSettings settings)
    {
        var result = await mediator.Send(new SaveGlobalDataCommand
        {
            Alias = Constants.GlobalSettings,
            Data = JsonSerializer.Serialize(settings)
        });
        return result.Success;
    }

    /// <summary>
    /// Retrieves global data from the mediator using the specified alias.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the GetGlobalDataCommand.</param>
    /// <param name="alias">The alias associated with the global data to be retrieved.</param>
    /// <typeparam name="T">The type to which the global data should be converted.</typeparam>
    /// <returns>A task that represents the asynchronous operation. The task result contains an instance of the specified type.</returns>
    public static async Task<T?> GetGlobalData<T>(this IMediator mediator, string alias)
    {
        var globalData = await mediator.Send(new GetGlobalDataCommand { Alias = alias });
        if (globalData?.Data != null)
        {
            return globalData.GetValue<T>();
        }

        return default;
    }
    
    /// <summary>
    /// Gets the currently logged in user
    /// </summary>
    /// <param name="mediator"></param>
    /// <returns></returns>
    public static async Task<User?> GetCurrentUser(this IMediator mediator)
    {
        return await mediator.Send(new GetCurrentUserCommand());
    }

    /// <summary>
    /// Retrieves a user based on the specified ID.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the GetUserCommand.</param>
    /// <param name="id">The unique identifier of the user to retrieve. If null, no user will be returned.</param>
    /// <param name="cached">Indicates whether to use cached data. Default value is true.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains an instance of User, or null if no user is found.</returns>
    public static async Task<User?> GetUser(this IMediator mediator, Guid? id, bool cached = true)
    {
        if (id != null)
        {
            return await mediator.Send(new GetUserCommand { Id = id.Value, Cached = cached});
        }

        return null;
    }

    /// <summary>
    /// Retrieves the media with the specified ID from the mediator.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the GetMediaCommand.</param>
    /// <param name="id">The ID of the media to retrieve.</param>
    /// <param name="cached">A boolean value indicating whether the cached version should be retrieved if available.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains an instance of Media or null if the ID is null or the media is not found.</returns>
    public static async Task<Media.Models.Media?> GetMedia(this IMediator mediator, Guid? id, bool cached = true)
    {
        if (id != null)
        {
            return await mediator.Send(new GetMediaCommand { Id = id.Value, Cached = cached });
        }

        return null;
    }

    /// <summary>
    /// Retrieves content information based on the provided content ID.
    /// </summary>
    /// <param name="mediator">The mediator instance used to send the GetContentCommand.</param>
    /// <param name="id">The unique identifier of the content to retrieve.</param>
    /// <param name="cached">Indicates whether to use cached content data or not.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains an instance of Content if found; otherwise, null.</returns>
    public static async Task<Content.Models.Content?> GetContent(this IMediator mediator, Guid? id, bool cached = true)
    {
        if (id != null)
        {
            return await mediator.Send(new GetContentCommand { Id = id.Value, Cached = cached });
        }

        return null;
    }

Last updated