Refactoring
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
@@ -11,6 +14,7 @@ namespace Icarus.Controllers.Managers
|
||||
public class ArtistManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
private ArtistContext _artistContext;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -19,10 +23,115 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ArtistManager(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
_artistContext = new ArtistContext(_connectionString);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public void SaveArtistToDatabase(ref Song song)
|
||||
{
|
||||
_logger.Info("Starting process to save the artist record of the song to the database");
|
||||
|
||||
var artist = new Artist();
|
||||
|
||||
artist.Name = song.Artist;
|
||||
artist.SongCount = 1;
|
||||
var artistTitle = artist.Name;
|
||||
|
||||
var artistRetrieved = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(artistTitle));
|
||||
|
||||
if (artistRetrieved == null)
|
||||
{
|
||||
artist.SongCount = 1;
|
||||
_artistContext.Add(artist);
|
||||
_artistContext.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
artist.ArtistID = artistRetrieved.ArtistID;
|
||||
}
|
||||
|
||||
song.ArtistID = artist.ArtistID;
|
||||
}
|
||||
|
||||
public Artist UpdateArtistInDatabase(Song oldSongRecord, Song newSongRecord)
|
||||
{
|
||||
var oldArtistRecord = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(oldSongRecord.AlbumTitle));
|
||||
var oldArtistName = oldArtistRecord.Name;
|
||||
var newArtistName = newSongRecord.Artist;
|
||||
|
||||
if (string.IsNullOrEmpty(newArtistName) || oldArtistName.Equals(newArtistName))
|
||||
{
|
||||
_logger.Info("No change to the song's Artist");
|
||||
return oldArtistRecord;
|
||||
}
|
||||
|
||||
_logger.Info("Change to the song's record found");
|
||||
|
||||
if (oldArtistRecord.SongCount <= 1)
|
||||
{
|
||||
_logger.Info("Deleting artist record that no longer has any songs");
|
||||
|
||||
_artistContext.Remove(oldArtistRecord);
|
||||
_artistContext.SaveChanges();
|
||||
}
|
||||
|
||||
if (!(_artistContext.Artists.FirstOrDefault(art => art.Name.Equals(oldSongRecord.AlbumTitle)) != null))
|
||||
{
|
||||
_logger.Info("Creating new artist record");
|
||||
|
||||
var newArtistRecord = new Artist
|
||||
{
|
||||
Name = newArtistName
|
||||
};
|
||||
|
||||
_artistContext.Add(newArtistRecord);
|
||||
_artistContext.SaveChanges();
|
||||
|
||||
return newArtistRecord;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info("Updating existing artist record");
|
||||
|
||||
var existingArtistRecord = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(newSongRecord.AlbumTitle));
|
||||
|
||||
_artistContext.Update(existingArtistRecord);
|
||||
_artistContext.SaveChanges();
|
||||
|
||||
return existingArtistRecord;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteArtistFromDatabase(Song song)
|
||||
{
|
||||
if (!(_artistContext.Artists.FirstOrDefault(art => art.Name.Equals(song.Artist)) != null))
|
||||
{
|
||||
_logger.Info("Cannot delete the artist record because it does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
var artist = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(song.Artist));
|
||||
|
||||
if (SongsOfArtist(artist) <= 1)
|
||||
{
|
||||
_artistContext.Remove(artist);
|
||||
_artistContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
private int SongsOfArtist(Artist artist)
|
||||
{
|
||||
var sngContext = new SongContext(_connectionString);
|
||||
var songs = sngContext.Songs.Where(sng => sng.ArtistID == artist.ArtistID).ToList();
|
||||
|
||||
return songs.Count;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user