aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/IHasItemFields.cs
blob: 8598ea262f2519dc2e4641ccb50dc59249f3fbaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Linq;
using MediaBrowser.Model.Querying;

namespace MediaBrowser.Api
{
    /// <summary>
    /// Interface IHasItemFields
    /// </summary>
    public interface IHasItemFields
    {
        /// <summary>
        /// Gets or sets the fields.
        /// </summary>
        /// <value>The fields.</value>
        string Fields { get; set; }
    }

    /// <summary>
    /// Class ItemFieldsExtensions.
    /// </summary>
    public static class ItemFieldsExtensions
    {
        /// <summary>
        /// Gets the item fields.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>IEnumerable{ItemFields}.</returns>
        public static ItemFields[] GetItemFields(this IHasItemFields request)
        {
            var val = request.Fields;

            if (string.IsNullOrEmpty(val))
            {
                return new ItemFields[] { };
            }

            return val.Split(',').Select(v =>
            {
                if (Enum.TryParse(v, true, out ItemFields value))
                {
                    return (ItemFields?)value;
                }
                return null;

            }).Where(i => i.HasValue).Select(i => i.Value).ToArray();
        }
    }
}