roadie/Roadie.Api.Library/Data/CollectionPartial.cs

171 lines
5.6 KiB
C#
Raw Normal View History

using CsvHelper;
using Roadie.Library.Configuration;
using Roadie.Library.Extensions;
using Roadie.Library.Utility;
using System;
2018-11-11 20:10:10 +00:00
using System.Collections.Generic;
using System.Diagnostics;
2020-01-18 19:31:05 +00:00
using System.Globalization;
using System.IO;
2018-11-11 20:10:10 +00:00
namespace Roadie.Library.Data
{
public partial class Collection
{
2020-06-21 20:39:14 +00:00
public const string ArtistPosition = "artist";
public const string PositionPosition = "position";
public const string ReleasePosition = "release";
2019-11-10 23:54:15 +00:00
/// <summary>
/// If the given value in either Artist or Release starts with this then the next value is the database Id, example "1,~4,~19"
/// </summary>
public static string DatabaseIdKey = "~";
2019-07-03 16:21:29 +00:00
public int? _artistColumn;
2018-11-11 20:10:10 +00:00
2019-07-03 16:21:29 +00:00
public int? _positionColumn;
public int? _releaseColumn;
private IEnumerable<PositionArtistRelease> _positionArtistReleases;
2019-07-03 16:21:29 +00:00
public int ArtistColumn
{
get
{
2019-07-03 16:21:29 +00:00
if (_artistColumn == null)
{
var looper = -1;
2019-07-03 16:21:29 +00:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2020-06-21 20:39:14 +00:00
if (String.Equals(pos, ArtistPosition, StringComparison.OrdinalIgnoreCase))
{
_artistColumn = looper;
}
}
}
2019-07-03 16:21:29 +00:00
return _artistColumn.Value;
}
}
2019-07-03 16:21:29 +00:00
public string CacheKey => CacheUrn(RoadieId);
public string CacheRegion => CacheRegionUrn(RoadieId);
/// <summary>
/// Returns a full file path to the Collection Image
/// </summary>
2019-11-10 14:48:07 +00:00
public string PathToImage(IRoadieSettings configuration, bool makeFolderIfNotExist = false)
{
2019-11-10 14:48:07 +00:00
var folder = configuration.CollectionImageFolder;
if (!Directory.Exists(folder) && makeFolderIfNotExist)
{
Directory.CreateDirectory(folder);
}
return Path.Combine(folder, $"{ (SortName ?? Name).ToFileNameFriendly() } [{ Id }].jpg");
}
2019-07-03 16:21:29 +00:00
public int PositionColumn
{
get
{
2019-07-03 16:21:29 +00:00
if (_positionColumn == null)
{
var looper = -1;
2019-07-03 16:21:29 +00:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2020-06-21 20:39:14 +00:00
if (String.Equals(pos, PositionPosition, StringComparison.OrdinalIgnoreCase))
{
_positionColumn = looper;
}
}
}
2019-07-03 16:21:29 +00:00
return _positionColumn.Value;
}
}
public int ReleaseColumn
{
get
{
2019-07-03 16:21:29 +00:00
if (_releaseColumn == null)
{
var looper = -1;
2019-07-03 16:21:29 +00:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2020-06-21 20:39:14 +00:00
if (String.Equals(pos, ReleasePosition, StringComparison.OrdinalIgnoreCase))
{
_releaseColumn = looper;
}
}
}
2019-07-03 16:21:29 +00:00
return _releaseColumn.Value;
}
}
2019-07-03 16:21:29 +00:00
public Collection()
{
Releases = new HashSet<CollectionRelease>();
2019-11-28 17:38:26 +00:00
MissingReleases = new HashSet<CollectionMissing>();
2019-07-03 16:21:29 +00:00
Comments = new HashSet<Comment>();
2019-11-28 17:38:26 +00:00
2019-07-03 16:21:29 +00:00
ListInCSVFormat = "Position,Release,Artist";
CollectionType = Enums.CollectionType.Rank;
}
2019-07-03 16:21:29 +00:00
public static string CacheRegionUrn(Guid Id)
{
return string.Format("urn:collection:{0}", Id);
}
public static string CacheUrn(Guid Id)
{
return $"urn:collection_by_id:{Id}";
}
public IEnumerable<PositionArtistRelease> PositionArtistReleases()
{
2019-07-03 16:21:29 +00:00
if (_positionArtistReleases == null)
{
var rows = new List<PositionArtistRelease>();
2019-07-03 16:21:29 +00:00
using (var sr = new StringReader(ListInCSV))
{
var index = 0;
2020-01-18 19:31:05 +00:00
var configuration = new CsvHelper.Configuration.CsvConfiguration(new CultureInfo("en-US", false))
{
MissingFieldFound = null,
HasHeaderRecord = false
};
2021-05-08 13:52:24 +00:00
configuration.BadDataFound = context => Trace.WriteLine($"PositionArtistReleases: Bad data found on row '{ context.Context.Parser.RawRow}'", "Warning");
2019-11-28 17:38:26 +00:00
using (var csv = new CsvReader(sr, configuration))
{
2019-11-28 17:38:26 +00:00
while (csv.Read())
{
2019-11-28 17:38:26 +00:00
index++;
rows.Add(new PositionArtistRelease
{
Index = index,
Position = csv.GetField<int>(PositionColumn),
Artist = SafeParser.ToString(csv.GetField<string>(ArtistColumn)),
Release = SafeParser.ToString(csv.GetField<string>(ReleaseColumn))
});
}
}
}
2019-07-03 16:21:29 +00:00
_positionArtistReleases = rows;
}
2019-01-26 20:55:58 +00:00
2019-07-03 16:21:29 +00:00
return _positionArtistReleases;
2019-01-26 20:55:58 +00:00
}
public override string ToString()
{
return $"Id [{Id}], Name [{Name}], RoadieId [{RoadieId}]";
2019-01-26 20:55:58 +00:00
}
}
2019-07-03 16:21:29 +00:00
}