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

165 lines
5.2 KiB
C#
Raw Normal View History

using CsvHelper;
using Newtonsoft.Json;
using Roadie.Library.Configuration;
using Roadie.Library.Enums;
using Roadie.Library.Extensions;
using Roadie.Library.Utility;
using System;
2018-11-11 14:10:10 -06:00
using System.Collections.Generic;
using System.Diagnostics;
2020-01-18 13:31:05 -06:00
using System.Globalization;
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;
2019-07-03 11:21:29 -05:00
public int ArtistColumn
{
get
{
2019-07-03 11:21:29 -05:00
if (_artistColumn == null)
{
var looper = -1;
2019-07-03 11:21:29 -05:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2019-07-03 11:21:29 -05:00
if (pos.ToLower().Equals("artist")) _artistColumn = looper;
}
}
2019-07-03 11:21:29 -05:00
return _artistColumn.Value;
}
}
2019-07-03 11:21:29 -05: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 08:48:07 -06:00
public string PathToImage(IRoadieSettings configuration, bool makeFolderIfNotExist = false)
{
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-03 11:21:29 -05:00
public int PositionColumn
{
get
{
2019-07-03 11:21:29 -05:00
if (_positionColumn == null)
{
var looper = -1;
2019-07-03 11:21:29 -05:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2019-07-03 11:21:29 -05:00
if (pos.ToLower().Equals("position")) _positionColumn = looper;
}
}
2019-07-03 11:21:29 -05:00
return _positionColumn.Value;
}
}
public int ReleaseColumn
{
get
{
2019-07-03 11:21:29 -05:00
if (_releaseColumn == null)
{
var looper = -1;
2019-07-03 11:21:29 -05:00
foreach (var pos in ListInCSVFormat.Split(','))
{
looper++;
2019-07-03 11:21:29 -05:00
if (pos.ToLower().Equals("release")) _releaseColumn = looper;
}
}
2019-07-03 11:21:29 -05:00
return _releaseColumn.Value;
}
}
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;
}
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}";
}
public IEnumerable<PositionArtistRelease> PositionArtistReleases()
{
2019-07-03 11:21:29 -05:00
if (_positionArtistReleases == null)
{
var rows = new List<PositionArtistRelease>();
2019-07-03 11:21:29 -05:00
using (var sr = new StringReader(ListInCSV))
{
var index = 0;
2020-01-18 13:31:05 -06:00
var configuration = new CsvHelper.Configuration.CsvConfiguration(new CultureInfo("en-US", false))
{
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-11-28 11:38:26 -06:00
using (var csv = new CsvReader(sr, configuration))
{
2019-11-28 11:38:26 -06:00
while (csv.Read())
{
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))
});
}
}
}
2019-07-03 11:21:29 -05:00
_positionArtistReleases = rows;
}
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()
{
return $"Id [{Id}], Name [{Name}], RoadieId [{RoadieId}]";
2019-01-26 14:55:58 -06:00
}
}
2019-07-03 11:21:29 -05:00
}