diff --git a/Edge.LitMusic/seeder/sample-music.json b/Edge.LitMusic/seeder/sample-music.json
deleted file mode 100644
index 82f70be..0000000
--- a/Edge.LitMusic/seeder/sample-music.json
+++ /dev/null
@@ -1,13 +0,0 @@
-[
- {
- "al": "Unknown",
- "a": "Unknown",
- "aai": 20,
- "if": false,
- "l": 1,
- "p": "https://www.w3schools.com/tags/horse.mp3",
- "t": "horse",
- "cd": "2022-02-08 06:58:24.252Z",
- "up": "2022-02-09 07:15:28.410Z"
- }
-]
\ No newline at end of file
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Controllers/MusicController.cs b/Edge.LitMusic/src/Edge.LitMusic/Controllers/MusicController.cs
index a20e2f9..f453314 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Controllers/MusicController.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Controllers/MusicController.cs
@@ -1,6 +1,5 @@
using Edge.LitMusic.Controllers.Models;
using Edge.LitMusic.Services;
-using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Edge.LitMusic.Controllers;
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Edge.LitMusic.csproj b/Edge.LitMusic/src/Edge.LitMusic/Edge.LitMusic.csproj
index 3581ade..c292800 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Edge.LitMusic.csproj
+++ b/Edge.LitMusic/src/Edge.LitMusic/Edge.LitMusic.csproj
@@ -2,6 +2,7 @@
net7.0
+ enable
diff --git a/Edge.LitMusic/src/Edge.LitMusic/GlobalUsings.cs b/Edge.LitMusic/src/Edge.LitMusic/GlobalUsings.cs
deleted file mode 100644
index 8ed637e..0000000
--- a/Edge.LitMusic/src/Edge.LitMusic/GlobalUsings.cs
+++ /dev/null
@@ -1,4 +0,0 @@
-global using System;
-global using System.Collections.Generic;
-global using System.Linq;
-global using System.Threading.Tasks;
\ No newline at end of file
diff --git a/Edge.LitMusic/src/Edge.LitMusic/HttpException.cs b/Edge.LitMusic/src/Edge.LitMusic/HttpException.cs
index 971dfcb..bf4ca60 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/HttpException.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/HttpException.cs
@@ -1,6 +1,7 @@
using System.Net;
namespace Edge.LitMusic;
+
public class HttpException : Exception
{
public HttpStatusCode StatusCode { get; }
diff --git a/Edge.LitMusic/src/Edge.LitMusic/HttpExceptionMiddleware.cs b/Edge.LitMusic/src/Edge.LitMusic/HttpExceptionMiddleware.cs
index 8822390..d4c40e5 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/HttpExceptionMiddleware.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/HttpExceptionMiddleware.cs
@@ -1,7 +1,7 @@
using System.Text.Json;
-using Microsoft.AspNetCore.Http;
namespace Edge.LitMusic;
+
internal class HttpExceptionMiddleware
{
private readonly RequestDelegate _next;
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Program.cs b/Edge.LitMusic/src/Edge.LitMusic/Program.cs
index 545373b..178cb80 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Program.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Program.cs
@@ -1,7 +1,5 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Hosting;
-
namespace Edge.LitMusic;
+
public class Program
{
public static void Main(string[] args)
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MongoRepository.cs b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MongoRepository.cs
index aa4edf1..134e810 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MongoRepository.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MongoRepository.cs
@@ -1,22 +1,23 @@
using MongoDB.Driver;
using Edge.LitMusic.Settings;
-using System.Threading;
namespace Edge.LitMusic.Repositories;
-internal class MongoRepository
+internal abstract class MongoRepository
{
private readonly IMongoCollection _collection;
- public MongoRepository(IMongoDBSetting setting, string collectionName)
+ protected abstract string CollectionName { get; }
+
+ public MongoRepository(IMongoDBSetting setting)
{
try
{
IMongoDatabase database = new MongoClient(connectionString: setting.ConnectionString).GetDatabase(name: setting.DatabaseName);
- this._collection = database.GetCollection(collectionName);
+ this._collection = database.GetCollection(this.CollectionName);
}
catch (Exception ex)
{
- Console.WriteLine($"Unable to connect to Mongo {ex.ToString()}");
+ Console.WriteLine($"Unable to connect to Mongo {ex}");
}
}
@@ -40,9 +41,9 @@ public Task UpdateAsync(FilterDefinition filter, UpdateDefinition FindOneAndUpdateAsync(FilterDefinition filter, UpdateDefinition update, CancellationToken cancellationToken = default)
+ public Task FindOneAndUpdateAsync(FilterDefinition filter, IEnumerable> update, CancellationToken cancellationToken = default)
{
- return this._collection.FindOneAndUpdateAsync(filter, update, new FindOneAndUpdateOptions { ReturnDocument = ReturnDocument.After }, cancellationToken);
+ return this._collection.FindOneAndUpdateAsync(filter, Builders.Update.Combine(update), new FindOneAndUpdateOptions { ReturnDocument = ReturnDocument.After }, cancellationToken);
}
public async Task InsertOneAsync(TDocument doc)
@@ -55,5 +56,4 @@ public Task RemoveOneAsync(FilterDefinition filter)
{
return this._collection.DeleteOneAsync(filter);
}
-
}
\ No newline at end of file
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicQueryBuilder.cs b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicQueryBuilder.cs
index 5338947..7cec331 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicQueryBuilder.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicQueryBuilder.cs
@@ -32,47 +32,55 @@ public static FilterDefinition BuildSearchQuery(IMusicSearchQuery
return filters.Any() ? filterBuilder.And(filters) : filterBuilder.Empty;
}
- public static UpdateDefinition BuildUpdateQuery(IMusicUpdateQuery query)
+ public static IEnumerable> BuildUpdateQuery(IMusicUpdateQuery query)
{
- List> updates = new();
+ bool hasChanged = false;
if (!string.IsNullOrEmpty(query.Album))
{
- updates.Add(updateBuilder.Set(m => m.Album, query.Album));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.Album, query.Album);
}
if (!string.IsNullOrEmpty(query.Artist))
{
- updates.Add(updateBuilder.Set(m => m.Artist, query.Artist));
- updates.Add(updateBuilder.Set(m => m.ArtistAlphabetIndex, MusicHelper.CalculateAlphabetIndex(artistFirstChar: query.Artist[0])));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.Artist, query.Artist);
+ yield return updateBuilder.Set(m => m.ArtistAlphabetIndex, MusicHelper.CalculateAlphabetIndex(artistFirstChar: query.Artist[0]));
+
}
if (query.Length > 0)
{
- updates.Add(updateBuilder.Set(m => m.Length, query.Length));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.Length, query.Length);
+
}
if (!string.IsNullOrEmpty(query.Path))
{
- updates.Add(updateBuilder.Set(m => m.Path, query.Path));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.Path, query.Path);
+
}
if (!string.IsNullOrEmpty(query.Title))
{
- updates.Add(updateBuilder.Set(m => m.Title, query.Title));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.Title, query.Title);
}
if (query.IsFavorite.HasValue)
{
- updates.Add(updateBuilder.Set(m => m.IsFavorite, query.IsFavorite));
+ hasChanged = true;
+ yield return updateBuilder.Set(m => m.IsFavorite, query.IsFavorite);
+
}
- if (updates.Any())
+ if (hasChanged)
{
- updates.Add(updateBuilder.Set(m => m.UpdatedDate, DateTime.UtcNow));
+ yield return updateBuilder.Set(m => m.UpdatedDate, DateTime.UtcNow);
}
-
- return updates.Any() ? updateBuilder.Combine(updates) : null;
}
public static SortDefinition BuildSortQuery(string sortBy)
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicRepository.cs b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicRepository.cs
index 3a52223..0f8421d 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicRepository.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicRepository.cs
@@ -6,7 +6,9 @@
namespace Edge.LitMusic.Repositories;
internal class MusicRepository : MongoRepository, IMusicRepository
{
- public MusicRepository(IMongoDBSetting setting) : base(setting: setting, collectionName: "music")
+ protected override string CollectionName => "music";
+
+ public MusicRepository(IMongoDBSetting setting) : base(setting: setting)
{
}
@@ -37,7 +39,7 @@ public Task GetMusicAsync(string id)
public Task UpdateMusicAsync(string id, UpdateMusicRequest request)
{
FilterDefinition filter = MusicQueryBuilder.BuildEntityIdQuery(id: id);
- UpdateDefinition update = MusicQueryBuilder.BuildUpdateQuery(query: request);
+ IEnumerable> update = MusicQueryBuilder.BuildUpdateQuery(query: request);
return base.FindOneAndUpdateAsync(filter: filter, update: update);
}
@@ -51,7 +53,7 @@ public Task RemoveMusicAsync(string id)
public Task UpdateFavoriteAsync(string id, UpdateMusicRequest request)
{
FilterDefinition filter = MusicQueryBuilder.BuildEntityIdQuery(id: id);
- UpdateDefinition update = MusicQueryBuilder.BuildUpdateQuery(query: request);
+ IEnumerable> update = MusicQueryBuilder.BuildUpdateQuery(query: request);
return base.FindOneAndUpdateAsync(filter: filter, update: update);
}
diff --git a/Edge.LitMusic/src/Edge.LitMusic/ServicesCollectionExtension.cs b/Edge.LitMusic/src/Edge.LitMusic/ServicesCollectionExtension.cs
index 719bd0a..6d4d111 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/ServicesCollectionExtension.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/ServicesCollectionExtension.cs
@@ -1,45 +1,48 @@
-using System.Net.Http;
using Edge.LitMusic.Repositories;
using Edge.LitMusic.Services;
using Edge.LitMusic.Settings;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Microsoft.OpenApi.Models;
namespace Edge.LitMusic;
+
internal static class ServicesCollectionExtension
{
- internal static void AddServices(this IServiceCollection services)
+ internal static IServiceCollection AddMVC(this IServiceCollection services)
{
- services.AddSingleton();
- services.AddSingleton();
+ services.AddControllers();
+
+ return services;
}
- internal static void AddRepositories(this IServiceCollection services, bool useInMemory)
+ internal static IServiceCollection AddServices(this IServiceCollection services)
+ {
+ return services
+ .AddSingleton()
+ .AddSingleton();
+ }
+
+ internal static IServiceCollection AddRepositories(this IServiceCollection services, bool useInMemory)
{
if (useInMemory)
{
- services.AddSingleton();
- }
- else
- {
- services.AddSingleton();
+ return services.AddSingleton();
}
+
+ return services.AddSingleton();
}
- internal static void AddSettings(this IServiceCollection services, IConfiguration configuration)
+ internal static IServiceCollection AddSettings(this IServiceCollection services, IConfiguration configuration)
{
- services.Configure(configuration.GetSection("MongoDBSetting"));
- services.AddSingleton(provider =>
- provider.GetRequiredService>().Value
- );
+ return services
+ .Configure(configuration.GetSection("MongoDBSetting"))
+ .AddSingleton(provider => provider.GetRequiredService>().Value);
}
- internal static void AddSwagger(this IServiceCollection services)
+ internal static IServiceCollection AddSwagger(this IServiceCollection services)
{
- services.AddSwaggerGen(options =>
+ return services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
@@ -50,9 +53,9 @@ internal static void AddSwagger(this IServiceCollection services)
});
}
- internal static void AddCors(this IServiceCollection services, ICORSPolicySettings corsPolicySettings)
+ internal static IServiceCollection AddCors(this IServiceCollection services, ICORSPolicySettings corsPolicySettings)
{
- services.AddCors(options =>
+ return services.AddCors(options =>
{
options.AddPolicy(name: corsPolicySettings.PolicyName,
builder =>
diff --git a/Edge.LitMusic/src/Edge.LitMusic/Startup.cs b/Edge.LitMusic/src/Edge.LitMusic/Startup.cs
index 5a2885e..7c6803f 100644
--- a/Edge.LitMusic/src/Edge.LitMusic/Startup.cs
+++ b/Edge.LitMusic/src/Edge.LitMusic/Startup.cs
@@ -1,11 +1,7 @@
using Edge.LitMusic.Settings;
-using Microsoft.AspNetCore.Builder;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
namespace Edge.LitMusic;
+
public class Startup
{
private IConfiguration Configuration { get; }
@@ -24,12 +20,13 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
- services.AddControllers();
- services.AddServices();
- services.AddRepositories(useInMemory: this.UseInMemory);
- services.AddSwagger();
- services.AddCors(corsPolicySettings: this.CORSPolicySettings);
- services.AddSettings(configuration: this.Configuration);
+ services
+ .AddMVC()
+ .AddServices()
+ .AddRepositories(useInMemory: this.UseInMemory)
+ .AddSwagger()
+ .AddCors(corsPolicySettings: this.CORSPolicySettings)
+ .AddSettings(configuration: this.Configuration);
#if DEBUG
Console.WriteLine(this.UseInMemory ? "Setting up InMemory datastore" : "");