Skip to content

Commit

Permalink
Merge pull request #13 from pacna/cleanup-edge
Browse files Browse the repository at this point in the history
cleanup edge service
  • Loading branch information
pacna authored Oct 17, 2023
2 parents 627803a + 77d65b1 commit 13ce773
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 78 deletions.
13 changes: 0 additions & 13 deletions Edge.LitMusic/seeder/sample-music.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions Edge.LitMusic/src/Edge.LitMusic/Edge.LitMusic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 0 additions & 4 deletions Edge.LitMusic/src/Edge.LitMusic/GlobalUsings.cs

This file was deleted.

1 change: 1 addition & 0 deletions Edge.LitMusic/src/Edge.LitMusic/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;

namespace Edge.LitMusic;

public class HttpException : Exception
{
public HttpStatusCode StatusCode { get; }
Expand Down
2 changes: 1 addition & 1 deletion Edge.LitMusic/src/Edge.LitMusic/HttpExceptionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json;
using Microsoft.AspNetCore.Http;

namespace Edge.LitMusic;

internal class HttpExceptionMiddleware
{
private readonly RequestDelegate _next;
Expand Down
4 changes: 1 addition & 3 deletions Edge.LitMusic/src/Edge.LitMusic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Edge.LitMusic;

public class Program
{
public static void Main(string[] args)
Expand Down
16 changes: 8 additions & 8 deletions Edge.LitMusic/src/Edge.LitMusic/Repositories/MongoRepository.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using MongoDB.Driver;
using Edge.LitMusic.Settings;
using System.Threading;

namespace Edge.LitMusic.Repositories;
internal class MongoRepository<TDocument>
internal abstract class MongoRepository<TDocument>
{
private readonly IMongoCollection<TDocument> _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<TDocument>(collectionName);
this._collection = database.GetCollection<TDocument>(this.CollectionName);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to connect to Mongo {ex.ToString()}");
Console.WriteLine($"Unable to connect to Mongo {ex}");
}
}

Expand All @@ -40,9 +41,9 @@ public Task UpdateAsync(FilterDefinition<TDocument> filter, UpdateDefinition<TDo
return this._collection.UpdateOneAsync(filter, update);
}

public Task<TDocument> FindOneAndUpdateAsync(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
public Task<TDocument> FindOneAndUpdateAsync(FilterDefinition<TDocument> filter, IEnumerable<UpdateDefinition<TDocument>> update, CancellationToken cancellationToken = default)
{
return this._collection.FindOneAndUpdateAsync(filter, update, new FindOneAndUpdateOptions<TDocument, TDocument> { ReturnDocument = ReturnDocument.After }, cancellationToken);
return this._collection.FindOneAndUpdateAsync(filter, Builders<TDocument>.Update.Combine(update), new FindOneAndUpdateOptions<TDocument, TDocument> { ReturnDocument = ReturnDocument.After }, cancellationToken);
}

public async Task<TDocument> InsertOneAsync(TDocument doc)
Expand All @@ -55,5 +56,4 @@ public Task RemoveOneAsync(FilterDefinition<TDocument> filter)
{
return this._collection.DeleteOneAsync(filter);
}

}
34 changes: 21 additions & 13 deletions Edge.LitMusic/src/Edge.LitMusic/Repositories/MusicQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,55 @@ public static FilterDefinition<MusicDocument> BuildSearchQuery(IMusicSearchQuery
return filters.Any() ? filterBuilder.And(filters) : filterBuilder.Empty;
}

public static UpdateDefinition<MusicDocument> BuildUpdateQuery(IMusicUpdateQuery query)
public static IEnumerable<UpdateDefinition<MusicDocument>> BuildUpdateQuery(IMusicUpdateQuery query)
{
List<UpdateDefinition<MusicDocument>> 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<MusicDocument> BuildSortQuery(string sortBy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Edge.LitMusic.Repositories;
internal class MusicRepository : MongoRepository<MusicDocument>, IMusicRepository
{
public MusicRepository(IMongoDBSetting setting) : base(setting: setting, collectionName: "music")
protected override string CollectionName => "music";

public MusicRepository(IMongoDBSetting setting) : base(setting: setting)
{
}

Expand Down Expand Up @@ -37,7 +39,7 @@ public Task<MusicDocument> GetMusicAsync(string id)
public Task<MusicDocument> UpdateMusicAsync(string id, UpdateMusicRequest request)
{
FilterDefinition<MusicDocument> filter = MusicQueryBuilder.BuildEntityIdQuery(id: id);
UpdateDefinition<MusicDocument> update = MusicQueryBuilder.BuildUpdateQuery(query: request);
IEnumerable<UpdateDefinition<MusicDocument>> update = MusicQueryBuilder.BuildUpdateQuery(query: request);

return base.FindOneAndUpdateAsync(filter: filter, update: update);
}
Expand All @@ -51,7 +53,7 @@ public Task RemoveMusicAsync(string id)
public Task<MusicDocument> UpdateFavoriteAsync(string id, UpdateMusicRequest request)
{
FilterDefinition<MusicDocument> filter = MusicQueryBuilder.BuildEntityIdQuery(id: id);
UpdateDefinition<MusicDocument> update = MusicQueryBuilder.BuildUpdateQuery(query: request);
IEnumerable<UpdateDefinition<MusicDocument>> update = MusicQueryBuilder.BuildUpdateQuery(query: request);

return base.FindOneAndUpdateAsync(filter: filter, update: update);
}
Expand Down
45 changes: 24 additions & 21 deletions Edge.LitMusic/src/Edge.LitMusic/ServicesCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -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<IMusicService, MusicService>();
services.AddSingleton<IValidationService, ValidationService>();
services.AddControllers();

return services;
}

internal static void AddRepositories(this IServiceCollection services, bool useInMemory)
internal static IServiceCollection AddServices(this IServiceCollection services)
{
return services
.AddSingleton<IMusicService, MusicService>()
.AddSingleton<IValidationService, ValidationService>();
}

internal static IServiceCollection AddRepositories(this IServiceCollection services, bool useInMemory)
{
if (useInMemory)
{
services.AddSingleton<IMusicRepository, MusicInMemoryRepository>();
}
else
{
services.AddSingleton<IMusicRepository, MusicRepository>();
return services.AddSingleton<IMusicRepository, MusicInMemoryRepository>();
}

return services.AddSingleton<IMusicRepository, MusicRepository>();
}

internal static void AddSettings(this IServiceCollection services, IConfiguration configuration)
internal static IServiceCollection AddSettings(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<MongoDBSetting>(configuration.GetSection("MongoDBSetting"));
services.AddSingleton<IMongoDBSetting>(provider =>
provider.GetRequiredService<IOptions<MongoDBSetting>>().Value
);
return services
.Configure<MongoDBSetting>(configuration.GetSection("MongoDBSetting"))
.AddSingleton<IMongoDBSetting>(provider => provider.GetRequiredService<IOptions<MongoDBSetting>>().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
{
Expand All @@ -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 =>
Expand Down
19 changes: 8 additions & 11 deletions Edge.LitMusic/src/Edge.LitMusic/Startup.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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" : "");
Expand Down

0 comments on commit 13ce773

Please sign in to comment.