2018-12-11 04:41:15 +00:00
|
|
|
|
using CsvHelper;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
using Roadie.Library.Configuration;
|
|
|
|
|
using Roadie.Library.Extensions;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
using Roadie.Library.Utility;
|
|
|
|
|
using System;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-22 03:08:22 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-01-18 19:31:05 +00:00
|
|
|
|
using System.Globalization;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
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;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
public int ArtistColumn
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
if (_artistColumn == null)
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 16:21:29 +00:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2020-06-21 20:39:14 +00:00
|
|
|
|
if (String.Equals(pos, ArtistPosition, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_artistColumn = looper;
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
|
|
|
|
return _artistColumn.Value;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
public string CacheKey => CacheUrn(RoadieId);
|
|
|
|
|
|
|
|
|
|
public string CacheRegion => CacheRegionUrn(RoadieId);
|
|
|
|
|
|
2019-07-31 16:44:25 +00:00
|
|
|
|
/// <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-07-31 16:44:25 +00:00
|
|
|
|
{
|
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-31 16:44:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
public int PositionColumn
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
if (_positionColumn == null)
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 16:21:29 +00:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2020-06-21 20:39:14 +00:00
|
|
|
|
if (String.Equals(pos, PositionPosition, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_positionColumn = looper;
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
|
|
|
|
return _positionColumn.Value;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ReleaseColumn
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
if (_releaseColumn == null)
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 16:21:29 +00:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2020-06-21 20:39:14 +00:00
|
|
|
|
if (String.Equals(pos, ReleasePosition, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_releaseColumn = looper;
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
|
|
|
|
return _releaseColumn.Value;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
|
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}";
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
|
2019-06-15 22:33:24 +00:00
|
|
|
|
public IEnumerable<PositionArtistRelease> PositionArtistReleases()
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
if (_positionArtistReleases == null)
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
2019-06-15 22:33:24 +00:00
|
|
|
|
var rows = new List<PositionArtistRelease>();
|
2019-07-03 16:21:29 +00:00
|
|
|
|
using (var sr = new StringReader(ListInCSV))
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
2020-01-18 19:31:05 +00:00
|
|
|
|
var configuration = new CsvHelper.Configuration.CsvConfiguration(new CultureInfo("en-US", false))
|
2019-05-22 03:08:22 +00:00
|
|
|
|
{
|
|
|
|
|
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))
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
2019-11-28 17:38:26 +00:00
|
|
|
|
while (csv.Read())
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
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))
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
|
|
|
|
_positionArtistReleases = rows;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
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()
|
|
|
|
|
{
|
2019-08-11 19:11:29 +00:00
|
|
|
|
return $"Id [{Id}], Name [{Name}], RoadieId [{RoadieId}]";
|
2019-01-26 20:55:58 +00:00
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
2019-07-03 16:21:29 +00:00
|
|
|
|
}
|