Lidarr/src/Lidarr.Api.V1/Albums/AlbumResource.cs

139 lines
4.8 KiB
C#
Raw Normal View History

2017-09-04 02:20:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2017-10-31 01:28:29 +00:00
using Lidarr.Api.V1.Artist;
2017-09-04 02:20:56 +00:00
using Lidarr.Http.REST;
using Newtonsoft.Json;
2017-09-04 02:20:56 +00:00
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Music;
2017-09-04 02:20:56 +00:00
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.Albums
2017-09-04 02:20:56 +00:00
{
public class AlbumResource : RestResource
{
public string Title { get; set; }
public string Disambiguation { get; set; }
public string Overview { get; set; }
2017-09-04 02:20:56 +00:00
public int ArtistId { get; set; }
2017-09-24 19:44:25 +00:00
public string ForeignAlbumId { get; set; }
2017-09-04 02:20:56 +00:00
public bool Monitored { get; set; }
public bool AnyReleaseOk { get; set; }
2017-09-04 02:20:56 +00:00
public int ProfileId { get; set; }
public int Duration { get; set; }
public string AlbumType { get; set; }
public List<string> SecondaryTypes { get; set; }
public int MediumCount
{
get
{
if (Media == null)
{
return 0;
}
return Media.Where(s => s.MediumNumber > 0).Count();
}
}
2017-09-04 02:20:56 +00:00
public Ratings Ratings { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<AlbumReleaseResource> Releases { get; set; }
2017-09-04 02:20:56 +00:00
public List<string> Genres { get; set; }
public List<MediumResource> Media { get; set; }
2017-09-04 02:20:56 +00:00
public ArtistResource Artist { get; set; }
public List<MediaCover> Images { get; set; }
public List<Links> Links { get; set; }
2017-09-04 02:20:56 +00:00
public AlbumStatisticsResource Statistics { get; set; }
2019-12-16 21:21:32 +00:00
public AddAlbumOptions AddOptions { get; set; }
public string RemoteCover { get; set; }
// Hiding this so people don't think its usable (only used to set the initial state)
2017-09-04 02:20:56 +00:00
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool Grabbed { get; set; }
}
public static class AlbumResourceMapper
2017-09-04 02:20:56 +00:00
{
public static AlbumResource ToResource(this Album model)
{
if (model == null)
{
return null;
}
2017-09-04 02:20:56 +00:00
var selectedRelease = model.AlbumReleases?.Value.Where(x => x.Monitored).SingleOrDefault();
2017-09-04 02:20:56 +00:00
return new AlbumResource
{
Id = model.Id,
ArtistId = model.ArtistId,
2017-09-24 19:44:25 +00:00
ForeignAlbumId = model.ForeignAlbumId,
2017-09-04 02:20:56 +00:00
ProfileId = model.ProfileId,
Monitored = model.Monitored,
AnyReleaseOk = model.AnyReleaseOk,
2017-09-04 02:20:56 +00:00
ReleaseDate = model.ReleaseDate,
Genres = model.Genres,
Title = model.Title,
Disambiguation = model.Disambiguation,
Overview = model.Overview,
2017-09-04 02:20:56 +00:00
Images = model.Images,
Links = model.Links,
2017-09-04 02:20:56 +00:00
Ratings = model.Ratings,
Duration = selectedRelease?.Duration ?? 0,
AlbumType = model.AlbumType,
SecondaryTypes = model.SecondaryTypes.Select(s => s.Name).ToList(),
Releases = model.AlbumReleases?.Value.ToResource() ?? new List<AlbumReleaseResource>(),
Media = selectedRelease?.Media.ToResource() ?? new List<MediumResource>(),
Artist = model.Artist?.Value.ToResource()
2017-09-04 02:20:56 +00:00
};
}
public static Album ToModel(this AlbumResource resource)
{
if (resource == null)
{
return null;
}
2019-12-16 21:21:32 +00:00
var artist = resource.Artist?.ToModel() ?? new NzbDrone.Core.Music.Artist();
return new Album
{
Id = resource.Id,
ForeignAlbumId = resource.ForeignAlbumId,
Title = resource.Title,
Disambiguation = resource.Disambiguation,
Overview = resource.Overview,
Images = resource.Images,
2019-12-16 21:21:32 +00:00
AlbumType = resource.AlbumType,
Monitored = resource.Monitored,
AnyReleaseOk = resource.AnyReleaseOk,
2019-12-16 21:21:32 +00:00
AlbumReleases = resource.Releases.ToModel(),
AddOptions = resource.AddOptions,
Artist = artist,
ArtistMetadata = artist.Metadata.Value
};
}
public static Album ToModel(this AlbumResource resource, Album album)
{
var updatedAlbum = resource.ToModel();
album.ApplyChanges(updatedAlbum);
album.AlbumReleases = updatedAlbum.AlbumReleases;
return album;
}
2017-09-04 02:20:56 +00:00
public static List<AlbumResource> ToResource(this IEnumerable<Album> models)
{
return models?.Select(ToResource).ToList();
}
2017-09-04 02:20:56 +00:00
public static List<Album> ToModel(this IEnumerable<AlbumResource> resources)
{
return resources.Select(ToModel).ToList();
2017-09-04 02:20:56 +00:00
}
}
}