2018-12-10 22:41:15 -06:00
|
|
|
|
using CsvHelper;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-07-31 11:44:25 -05:00
|
|
|
|
using Roadie.Library.Configuration;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
using Roadie.Library.Enums;
|
2019-07-31 11:44:25 -05:00
|
|
|
|
using Roadie.Library.Extensions;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
using Roadie.Library.Utility;
|
|
|
|
|
using System;
|
2018-11-11 14:10:10 -06:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-21 22:08:22 -05:00
|
|
|
|
using System.Diagnostics;
|
2020-01-18 13:31:05 -06:00
|
|
|
|
using System.Globalization;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
using System.IO;
|
2018-11-11 14:10:10 -06:00
|
|
|
|
|
|
|
|
|
namespace Roadie.Library.Data
|
|
|
|
|
{
|
|
|
|
|
public partial class Collection
|
|
|
|
|
{
|
2019-11-10 17:54:15 -06: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 11:21:29 -05:00
|
|
|
|
public int? _artistColumn;
|
2018-11-11 14:10:10 -06:00
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
public int? _positionColumn;
|
|
|
|
|
public int? _releaseColumn;
|
|
|
|
|
private IEnumerable<PositionArtistRelease> _positionArtistReleases;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
public int ArtistColumn
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (_artistColumn == null)
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (pos.ToLower().Equals("artist")) _artistColumn = looper;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 11:21:29 -05:00
|
|
|
|
|
|
|
|
|
return _artistColumn.Value;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
public string CacheKey => CacheUrn(RoadieId);
|
|
|
|
|
|
|
|
|
|
public string CacheRegion => CacheRegionUrn(RoadieId);
|
|
|
|
|
|
2019-07-31 11:44:25 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a full file path to the Collection Image
|
|
|
|
|
/// </summary>
|
2019-11-10 08:48:07 -06:00
|
|
|
|
public string PathToImage(IRoadieSettings configuration, bool makeFolderIfNotExist = false)
|
2019-07-31 11:44:25 -05:00
|
|
|
|
{
|
2019-11-10 08:48:07 -06: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 11:44:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
public int PositionColumn
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (_positionColumn == null)
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (pos.ToLower().Equals("position")) _positionColumn = looper;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 11:21:29 -05:00
|
|
|
|
|
|
|
|
|
return _positionColumn.Value;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ReleaseColumn
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (_releaseColumn == null)
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
var looper = -1;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
foreach (var pos in ListInCSVFormat.Split(','))
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
looper++;
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (pos.ToLower().Equals("release")) _releaseColumn = looper;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 11:21:29 -05:00
|
|
|
|
|
|
|
|
|
return _releaseColumn.Value;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
public Collection()
|
|
|
|
|
{
|
|
|
|
|
Releases = new HashSet<CollectionRelease>();
|
2019-11-28 11:38:26 -06:00
|
|
|
|
MissingReleases = new HashSet<CollectionMissing>();
|
2019-07-03 11:21:29 -05:00
|
|
|
|
Comments = new HashSet<Comment>();
|
2019-11-28 11:38:26 -06:00
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
ListInCSVFormat = "Position,Release,Artist";
|
|
|
|
|
CollectionType = Enums.CollectionType.Rank;
|
|
|
|
|
}
|
2018-12-10 22:41:15 -06:00
|
|
|
|
|
2019-07-03 11:21:29 -05: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-10 22:41:15 -06:00
|
|
|
|
|
2019-06-15 17:33:24 -05:00
|
|
|
|
public IEnumerable<PositionArtistRelease> PositionArtistReleases()
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
2019-07-03 11:21:29 -05:00
|
|
|
|
if (_positionArtistReleases == null)
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
2019-06-15 17:33:24 -05:00
|
|
|
|
var rows = new List<PositionArtistRelease>();
|
2019-07-03 11:21:29 -05:00
|
|
|
|
using (var sr = new StringReader(ListInCSV))
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
2020-01-18 13:31:05 -06:00
|
|
|
|
var configuration = new CsvHelper.Configuration.CsvConfiguration(new CultureInfo("en-US", false))
|
2019-05-21 22:08:22 -05:00
|
|
|
|
{
|
|
|
|
|
MissingFieldFound = null,
|
|
|
|
|
HasHeaderRecord = false
|
|
|
|
|
};
|
|
|
|
|
configuration.BadDataFound = context =>
|
|
|
|
|
{
|
2019-11-28 11:38:26 -06:00
|
|
|
|
Trace.WriteLine($"PositionArtistReleases: Bad data found on row '{context.RawRow}'", "Warning");
|
2019-05-21 22:08:22 -05:00
|
|
|
|
};
|
2019-11-28 11:38:26 -06:00
|
|
|
|
using (var csv = new CsvReader(sr, configuration))
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
2019-11-28 11:38:26 -06:00
|
|
|
|
while (csv.Read())
|
2018-12-10 22:41:15 -06:00
|
|
|
|
{
|
2019-11-28 11:38:26 -06: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-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 11:21:29 -05:00
|
|
|
|
|
|
|
|
|
_positionArtistReleases = rows;
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
2019-01-26 14:55:58 -06:00
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
return _positionArtistReleases;
|
2019-01-26 14:55:58 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2019-08-11 14:11:29 -05:00
|
|
|
|
return $"Id [{Id}], Name [{Name}], RoadieId [{RoadieId}]";
|
2019-01-26 14:55:58 -06:00
|
|
|
|
}
|
2018-12-10 22:41:15 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 11:21:29 -05:00
|
|
|
|
|
|
|
|
|
}
|