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

165 lines
4.9 KiB
C#
Raw Normal View History

using CsvHelper;
using Newtonsoft.Json;
using Roadie.Library.Enums;
using Roadie.Library.Utility;
using System;
2018-11-11 20:10:10 +00:00
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2018-11-11 20:10:10 +00:00
namespace Roadie.Library.Data
{
public partial class Collection
{
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++;
2019-07-03 16:21:29 +00:00
if (pos.ToLower().Equals("artist")) _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);
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++;
2019-07-03 16:21:29 +00:00
if (pos.ToLower().Equals("position")) _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++;
2019-07-03 16:21:29 +00:00
if (pos.ToLower().Equals("release")) _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>();
Comments = new HashSet<Comment>();
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;
var configuration = new CsvHelper.Configuration.Configuration
{
MissingFieldFound = null,
HasHeaderRecord = false
};
configuration.BadDataFound = context =>
{
Trace.WriteLine($"PositionArtistReleases: Bad data found on row '{context.RawRow}'");
};
var csv = new CsvReader(sr, configuration);
while (csv.Read())
{
index++;
rows.Add(new PositionArtistRelease
{
Index = index,
2019-07-03 16:21:29 +00:00
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()
{
2019-07-03 16:21:29 +00:00
return $"Id [{Id}], Name [{Name}]";
2019-01-26 20:55:58 +00:00
}
}
[Serializable]
public class PositionArtistRelease
{
2019-07-03 16:21:29 +00:00
public string Artist { get; set; }
/// <summary>
2019-07-03 16:21:29 +00:00
/// This is the index (position in the list regardless of the position number)
/// </summary>
[JsonIgnore]
public int Index { get; set; }
2019-07-03 16:21:29 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// This is the position number for the list (can be a year "1984" can be a number "14")
/// </summary>
public int Position { get; set; }
2019-07-03 16:21:29 +00:00
public string Release { get; set; }
2019-07-03 16:21:29 +00:00
[JsonIgnore] public Statuses Status { get; set; }
[JsonProperty("Status")] public string StatusVerbose => Status.ToString();
public override string ToString()
{
2019-07-03 16:21:29 +00:00
return string.Format("Position [{0}], Artist [{1}], Release [{2}]", Position, Artist, Release);
}
2018-11-11 20:10:10 +00:00
}
2019-07-03 16:21:29 +00:00
}