Skip to content

Commit

Permalink
refactor: change abstract class EndpointGroupBase to interface IEndpo…
Browse files Browse the repository at this point in the history
…intGroupBase
  • Loading branch information
Quang nd committed Oct 18, 2024
1 parent edc2afd commit 09dfeb4
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/Domain/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
global using UserService.Domain.Enums;
global using UserService.Domain.Events;
global using UserService.Domain.Exceptions;
global using UserService.Domain.ValueObjects;
global using CleanArchitecture.Domain.ValueObjects;
58 changes: 29 additions & 29 deletions src/Domain/ValueObjects/Colour.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
namespace UserService.Domain.ValueObjects;
namespace CleanArchitecture.Domain.ValueObjects;

public class Colour(string code) : ValueObject
{
public static Colour From(string code)
{
var colour = new Colour(code);

if (!SupportedColours.Contains(colour))
{
throw new UnsupportedColourException(code);
}

return colour;
}

public static Colour White => new("#FFFFFF");

public static Colour Red => new("#FF5733");
Expand All @@ -18,34 +30,7 @@ public class Colour(string code) : ValueObject

public static Colour Grey => new("#999999");

public string Code { get; } = string.IsNullOrWhiteSpace(code) ? "#000000" : code;

protected static IEnumerable<Colour> SupportedColours
{
get
{
yield return White;
yield return Red;
yield return Orange;
yield return Yellow;
yield return Green;
yield return Blue;
yield return Purple;
yield return Grey;
}
}

public static Colour From(string code)
{
Colour colour = new Colour(code);

if (!SupportedColours.Contains(colour))
{
throw new UnsupportedColourException(code);
}

return colour;
}
public string Code { get; private set; } = string.IsNullOrWhiteSpace(code)?"#000000":code;

public static implicit operator string(Colour colour)
{
Expand All @@ -62,6 +47,21 @@ public override string ToString()
return Code;
}

protected static IEnumerable<Colour> SupportedColours
{
get
{
yield return White;
yield return Red;
yield return Orange;
yield return Yellow;
yield return Green;
yield return Blue;
yield return Purple;
yield return Grey;
}
}

protected override IEnumerable<object> GetEqualityComponents()
{
yield return Code;
Expand Down
6 changes: 3 additions & 3 deletions src/Web/Endpoints/TodoItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace UserService.Web.Endpoints;

public class TodoItems : EndpointGroupBase
public class TodoItems : IEndpointGroupBase
{
public override void Map(WebApplication app)
public void Map(WebApplication app)
{
app.MapGroup(this)
.RequireAuthorization()
// .RequireAuthorization()
.MapGet(GetTodoItemsWithPagination)
.MapPost(CreateTodoItem)
.MapPut(UpdateTodoItem, "{id}")
Expand Down
6 changes: 3 additions & 3 deletions src/Web/Endpoints/TodoLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace UserService.Web.Endpoints;

public class TodoLists : EndpointGroupBase
public class TodoLists : IEndpointGroupBase
{
public override void Map(WebApplication app)
public void Map(WebApplication app)
{
app.MapGroup(this)
.RequireAuthorization()
// .RequireAuthorization()
.MapGet(GetTodoLists)
.MapPost(CreateTodoList)
.MapPut(UpdateTodoList, "{id}")
Expand Down
4 changes: 2 additions & 2 deletions src/Web/Endpoints/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace UserService.Web.Endpoints;

public class Users : EndpointGroupBase
public class Users : IEndpointGroupBase
{
public override void Map(WebApplication app)
public void Map(WebApplication app)
{
app.MapGroup(this)
.MapIdentityApi<ApplicationUser>();
Expand Down
4 changes: 2 additions & 2 deletions src/Web/Endpoints/WeatherForecasts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace UserService.Web.Endpoints;

public class WeatherForecasts : EndpointGroupBase
public class WeatherForecasts : IEndpointGroupBase
{
public override void Map(WebApplication app)
public void Map(WebApplication app)
{
app.MapGroup(this)
.RequireAuthorization()
Expand Down
4 changes: 2 additions & 2 deletions src/Web/Infrastructure/EndpointGroupBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace UserService.Web.Infrastructure;

public abstract class EndpointGroupBase
public interface IEndpointGroupBase
{
public abstract void Map(WebApplication app);
public void Map(WebApplication app);
}
8 changes: 4 additions & 4 deletions src/Web/Infrastructure/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace UserService.Web.Infrastructure;

public static class WebApplicationExtensions
{
public static RouteGroupBuilder MapGroup(this WebApplication app, EndpointGroupBase group)
public static RouteGroupBuilder MapGroup(this WebApplication app, IEndpointGroupBase group)
{
string groupName = group.GetType().Name;

Expand All @@ -17,16 +17,16 @@ public static RouteGroupBuilder MapGroup(this WebApplication app, EndpointGroupB

public static WebApplication MapEndpoints(this WebApplication app)
{
Type endpointGroupType = typeof(EndpointGroupBase);
Type endpointGroupType = typeof(IEndpointGroupBase);

Assembly assembly = Assembly.GetExecutingAssembly();

IEnumerable<Type> endpointGroupTypes = assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(endpointGroupType));
.Where(t => endpointGroupType.IsAssignableFrom(t) && t is { IsInterface: false, IsAbstract: false });

foreach (Type type in endpointGroupTypes)
{
if (Activator.CreateInstance(type) is EndpointGroupBase instance)
if (Activator.CreateInstance(type) is IEndpointGroupBase instance)
{
instance.Map(app);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using UserService.Infrastructure;
using UserService.Infrastructure.Data;
using UserService.Web;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// builder.Configuration
// .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
// .AddEnvironmentVariables()
// .AddCommandLine(args);
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args);

// builder.Configuration.AddConsul();
builder.Configuration.AddConsul();

// Add services to the container.
builder.Services.AddKeyVaultIfConfigured(builder.Configuration);
Expand Down
63 changes: 9 additions & 54 deletions src/Web/wwwroot/api/specification.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@
}
}
}
},
"security": [
{
"JWT": []
}
]
}
},
"post": {
"tags": [
Expand Down Expand Up @@ -91,12 +86,7 @@
}
}
}
},
"security": [
{
"JWT": []
}
]
}
}
},
"/api/TodoItems/{id}": {
Expand Down Expand Up @@ -133,12 +123,7 @@
"200": {
"description": ""
}
},
"security": [
{
"JWT": []
}
]
}
},
"delete": {
"tags": [
Expand All @@ -161,12 +146,7 @@
"200": {
"description": ""
}
},
"security": [
{
"JWT": []
}
]
}
}
},
"/api/TodoItems/UpdateDetail/{id}": {
Expand Down Expand Up @@ -203,12 +183,7 @@
"200": {
"description": ""
}
},
"security": [
{
"JWT": []
}
]
}
}
},
"/api/TodoLists": {
Expand All @@ -228,12 +203,7 @@
}
}
}
},
"security": [
{
"JWT": []
}
]
}
},
"post": {
"tags": [
Expand Down Expand Up @@ -264,12 +234,7 @@
}
}
}
},
"security": [
{
"JWT": []
}
]
}
}
},
"/api/TodoLists/{id}": {
Expand Down Expand Up @@ -306,12 +271,7 @@
"200": {
"description": ""
}
},
"security": [
{
"JWT": []
}
]
}
},
"delete": {
"tags": [
Expand All @@ -334,12 +294,7 @@
"200": {
"description": ""
}
},
"security": [
{
"JWT": []
}
]
}
}
},
"/api/Users/register": {
Expand Down

0 comments on commit 09dfeb4

Please sign in to comment.