From 0932be2f2bf09359abd6162a200555f2abccfd96 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 7 Jul 2019 15:02:38 -0400 Subject: [PATCH] Added but did not complete the repository for cover art, updated migration script to create migration for CoverArt, and prepping for the cover art api --- Database/Contexts/CoverArtContext.cs | 25 ++++++ Database/Contexts/SongContext.cs | 9 ++ Database/Repositories/CoverArtRepository.cs | 91 +++++++++++++++++++++ Models/CoverArt.cs | 6 +- Models/Song.cs | 5 ++ Scripts/Migrations/Linux/AddUpdate.sh | 3 + Startup.cs | 4 + 7 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 Database/Contexts/CoverArtContext.cs create mode 100644 Database/Repositories/CoverArtRepository.cs diff --git a/Database/Contexts/CoverArtContext.cs b/Database/Contexts/CoverArtContext.cs new file mode 100644 index 0000000..01a1995 --- /dev/null +++ b/Database/Contexts/CoverArtContext.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; + +using Microsoft.EntityFrameworkCore; +using MySql.Data; +using MySql.Data.EntityFrameworkCore.Extensions; +using MySql.Data.MySqlClient; + +using Icarus.Models; + +namespace Icarus.Database.Contexts +{ + public class CoverArtContext : DbContext + { + public DbSet CoverArtImages { get; set; } + + public CoverArtContext(DbContextOptions options) : base(options) { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .ToTable("CoverArt"); + } + } +} diff --git a/Database/Contexts/SongContext.cs b/Database/Contexts/SongContext.cs index 1a70a88..a5b5824 100644 --- a/Database/Contexts/SongContext.cs +++ b/Database/Contexts/SongContext.cs @@ -46,6 +46,12 @@ namespace Icarus.Database.Contexts .HasForeignKey(s => s.YearId) .OnDelete(DeleteBehavior.SetNull); + modelBuilder.Entity() + .HasOne(s => s.SongCoverArt) + .WithMany(ca => ca.Songs) + .HasForeignKey(s => s.CoverArtId) + .OnDelete(DeleteBehavior.SetNull); + modelBuilder.Entity() .Property(s => s.Year) .IsRequired(false); @@ -61,6 +67,9 @@ namespace Icarus.Database.Contexts modelBuilder.Entity() .Property(s => s.AlbumId) .IsRequired(false); + modelBuilder.Entity() + .Property(s => s.CoverArtId) + .IsRequired(false); } } } diff --git a/Database/Repositories/CoverArtRepository.cs b/Database/Repositories/CoverArtRepository.cs new file mode 100644 index 0000000..e9ba3fd --- /dev/null +++ b/Database/Repositories/CoverArtRepository.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; + +using MySql.Data.MySqlClient; + +using Icarus.Models; + +namespace Icarus.Database.Repositories +{ + public class CoverArtRepository : BaseRepository + { + #region Constructors + public CoverArtRepository(string connectionString) + { + _connectionString = connectionString; + } + #endregion + + + #region Methods + public CoverArt GetCoverArt(Song song) + { + // TODO: Implement sql record retrieval + return null; + } + + public void DeleteCoverArt(CoverArt coverArt) + { + try + { + // TODO: Implement sql record deletion + } + catch (Exception ex) + { + var msg = ex.Message; + _logger.Error(msg, "An error occurred"); + } + } + + private List ParseData(MySqlDataReader reader) + { + if (reader.HasRows) + { + var coverArtList = new List(); + while (reader.Read()) + coverArtList.Add(new CoverArt + { + CoverArtId = Convert.ToInt32(reader["CoverArtId"]), + SongTitle = reader["SongTitle"].ToString(), + ImagePath = reader["ImagePath"].ToString() + }); + + return coverArtList; + } + + return null; + } + + private CoverArt ParseSingleData(MySqlDataReader reader) + { + if (reader.HasRows) + { + reader.Read(); + + return new CoverArt + { + CoverArtId = Convert.ToInt32(reader["CoverArtId"]), + SongTitle = reader["SongTitle"].ToString(), + ImagePath = reader["ImagePath}"].ToString() + }; + } + + return null; + } + + private bool? AnyCoverArt() + { + using (var conn = GetConnection()) + { + conn.Open(); + + var query = "SELECT * FROM CoverArt"; + + using (var cmd = new MySqlCommand(query, conn)) + using (var reader = cmd.ExecuteReader()) + return reader.HasRows; + } + } + #endregion + } +} diff --git a/Models/CoverArt.cs b/Models/CoverArt.cs index 25a3fef..dfe6480 100644 --- a/Models/CoverArt.cs +++ b/Models/CoverArt.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text; using Newtonsoft.Json; @@ -8,10 +9,13 @@ namespace Icarus.Models public class CoverArt { [JsonProperty("id")] - public int Id { get; set; } + public int CoverArtId { get; set; } [JsonProperty("title")] public string SongTitle { get; set; } [JsonIgnore] public string ImagePath { get; set; } + + [JsonIgnore] + public List Songs { get; set; } } } diff --git a/Models/Song.cs b/Models/Song.cs index d73815e..3eb0a0a 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -45,5 +45,10 @@ namespace Icarus.Models public Year SongYear { get; set; } [JsonIgnore] public int? YearId { get; set; } + + [JsonIgnore] + public CoverArt SongCoverArt { get; set; } + [JsonIgnore] + public int? CoverArtId { get; set; } } } diff --git a/Scripts/Migrations/Linux/AddUpdate.sh b/Scripts/Migrations/Linux/AddUpdate.sh index d3b195b..04e72bd 100755 --- a/Scripts/Migrations/Linux/AddUpdate.sh +++ b/Scripts/Migrations/Linux/AddUpdate.sh @@ -11,6 +11,8 @@ echo "Adding Genre migration" dotnet ef migrations add Genre --context GenreContext echo "Adding Year migration" dotnet ef migrations add Year --context YearContext +echo "Adding Cover art migration" +dotnet ef migrations add CoverArt --context CoverArtContext echo "Updating migrations.." echo "Updating User migration" @@ -20,4 +22,5 @@ echo "Updating Album migration" echo "Updating Artist migration" echo "Updating Genre migration" echo "Updating Year migration" +echo "Updating Cover art migration" dotnet ef database update --context SongContext diff --git a/Startup.cs b/Startup.cs index 64a6f26..056a957 100644 --- a/Startup.cs +++ b/Startup.cs @@ -119,6 +119,9 @@ namespace Icarus services.Add(new ServiceDescriptor(typeof(YearRepository), new YearRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(CoverArtRepository), + new CoverArtRepository(Configuration.GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(UserRepository), new UserRepository(Configuration.GetConnectionString("DefaultConnection")))); @@ -128,6 +131,7 @@ namespace Icarus services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); + services.AddDbContext(options => options.UseMySQL(connString)); } // Called by the runtime. Use this method to configure the HTTP request pipeline.