From 6d0587e2c583c3db3a871aed9244ff7c3ce15654 Mon Sep 17 00:00:00 2001 From: Mostafa Rowghanian Date: Mon, 30 Oct 2023 14:40:43 +0330 Subject: [PATCH] add application layer and add user basic actions --- src/FluentCMS.Application/Extensions.cs | 14 +++++++ .../FluentCMS.Application.csproj | 17 ++++++++ .../Users/CreateUserCommand.cs | 9 ++++ .../Users/GetUserByIdQuery.cs | 8 ++++ .../Users/GetUsersQuery.cs | 7 ++++ .../Users/UserHandlers.cs | 42 +++++++++++++++++++ .../Controllers/UsersController.cs | 37 ++++++++++++++++ src/FluentCMS.Shared/FluentCMS.Shared.csproj | 1 + .../Config/appsettings,Development.json | 3 -- .../Config/appsettings.Development.json | 6 +++ src/FluentCMS.Web.UI/Program.cs | 27 ++++++++---- src/FluentCMS.sln | 6 +++ 12 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 src/FluentCMS.Application/Extensions.cs create mode 100644 src/FluentCMS.Application/FluentCMS.Application.csproj create mode 100644 src/FluentCMS.Application/Users/CreateUserCommand.cs create mode 100644 src/FluentCMS.Application/Users/GetUserByIdQuery.cs create mode 100644 src/FluentCMS.Application/Users/GetUsersQuery.cs create mode 100644 src/FluentCMS.Application/Users/UserHandlers.cs create mode 100644 src/FluentCMS.Shared/Controllers/UsersController.cs delete mode 100644 src/FluentCMS.Web.UI/Config/appsettings,Development.json create mode 100644 src/FluentCMS.Web.UI/Config/appsettings.Development.json diff --git a/src/FluentCMS.Application/Extensions.cs b/src/FluentCMS.Application/Extensions.cs new file mode 100644 index 000000000..6efe627ca --- /dev/null +++ b/src/FluentCMS.Application/Extensions.cs @@ -0,0 +1,14 @@ +using FluentCMS.Repository; +using Microsoft.Extensions.DependencyInjection; + +namespace FluentCMS.Application; +public static class Extensions +{ + public static FluentCMSBuilder AddApplication(this FluentCMSBuilder fcBuilder) + { + // register mediatR + fcBuilder.Services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(Extensions).Assembly)); + + return fcBuilder; + } +} diff --git a/src/FluentCMS.Application/FluentCMS.Application.csproj b/src/FluentCMS.Application/FluentCMS.Application.csproj new file mode 100644 index 000000000..bbb787650 --- /dev/null +++ b/src/FluentCMS.Application/FluentCMS.Application.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/src/FluentCMS.Application/Users/CreateUserCommand.cs b/src/FluentCMS.Application/Users/CreateUserCommand.cs new file mode 100644 index 000000000..049638923 --- /dev/null +++ b/src/FluentCMS.Application/Users/CreateUserCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace FluentCMS.Application.Users; +public class CreateUserCommand : IRequest +{ + public string Name { get; set; } = string.Empty; + public string Username { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; +} diff --git a/src/FluentCMS.Application/Users/GetUserByIdQuery.cs b/src/FluentCMS.Application/Users/GetUserByIdQuery.cs new file mode 100644 index 000000000..4f699dc94 --- /dev/null +++ b/src/FluentCMS.Application/Users/GetUserByIdQuery.cs @@ -0,0 +1,8 @@ +using FluentCMS.Entities; +using MediatR; + +namespace FluentCMS.Application.Users; +public class GetUserByIdQuery : IRequest +{ + public Guid Id { get; set; } +} diff --git a/src/FluentCMS.Application/Users/GetUsersQuery.cs b/src/FluentCMS.Application/Users/GetUsersQuery.cs new file mode 100644 index 000000000..b9a76d2cf --- /dev/null +++ b/src/FluentCMS.Application/Users/GetUsersQuery.cs @@ -0,0 +1,7 @@ +using FluentCMS.Entities; +using MediatR; + +namespace FluentCMS.Application.Users; +public class GetUsersQuery : IRequest> +{ +} diff --git a/src/FluentCMS.Application/Users/UserHandlers.cs b/src/FluentCMS.Application/Users/UserHandlers.cs new file mode 100644 index 000000000..1603b98a9 --- /dev/null +++ b/src/FluentCMS.Application/Users/UserHandlers.cs @@ -0,0 +1,42 @@ +using FluentCMS.Entities; +using FluentCMS.Services; +using MediatR; + +namespace FluentCMS.Application.Users; +internal class UserHandlers : + IRequestHandler>, + IRequestHandler, + IRequestHandler +{ + private readonly UserService _userService; + + public UserHandlers(UserService userService) + { + _userService = userService; + } + + public async Task> Handle(GetUsersQuery request, CancellationToken cancellationToken) + { + var users = await _userService.GetAll(); + return users; + } + + public async Task Handle(GetUserByIdQuery request, CancellationToken cancellationToken) + { + var user = await _userService.GetById(request.Id); + return user; + } + + public async Task Handle(CreateUserCommand request, CancellationToken cancellationToken) + { + var user = new User + { + Id = Guid.NewGuid(), + Name = request.Name, + Username = request.Username, + Password = request.Password, + }; + await _userService.Create(user, cancellationToken); + return user.Id; + } +} diff --git a/src/FluentCMS.Shared/Controllers/UsersController.cs b/src/FluentCMS.Shared/Controllers/UsersController.cs new file mode 100644 index 000000000..139881136 --- /dev/null +++ b/src/FluentCMS.Shared/Controllers/UsersController.cs @@ -0,0 +1,37 @@ +using FluentCMS.Application.Users; +using FluentCMS.Entities; +using Microsoft.AspNetCore.Mvc; +using MediatR; + +namespace FluentCMS.Web.Controllers; + +public class UsersController : BaseController +{ + private readonly IMediator _mediator; + + public UsersController(IMediator mediator) + { + _mediator = mediator; + } + + [HttpGet] + public async Task>> GetUsers() + { + var data = await _mediator.Send(new GetUsersQuery()); + return Ok(data); + } + + [HttpGet("{id}")] + public async Task> GetUserById([FromRoute] Guid id) + { + var data = await _mediator.Send(new GetUserByIdQuery { Id = id }); + return Ok(data); + } + + [HttpPost] + public async Task> CreateUser(CreateUserCommand request) + { + var result = await _mediator.Send(request); + return Ok(result); + } +} diff --git a/src/FluentCMS.Shared/FluentCMS.Shared.csproj b/src/FluentCMS.Shared/FluentCMS.Shared.csproj index 0752c68a9..4d673c0cf 100644 --- a/src/FluentCMS.Shared/FluentCMS.Shared.csproj +++ b/src/FluentCMS.Shared/FluentCMS.Shared.csproj @@ -11,6 +11,7 @@ + diff --git a/src/FluentCMS.Web.UI/Config/appsettings,Development.json b/src/FluentCMS.Web.UI/Config/appsettings,Development.json deleted file mode 100644 index 4faf77acf..000000000 --- a/src/FluentCMS.Web.UI/Config/appsettings,Development.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "AllowedHosts": "*" -} diff --git a/src/FluentCMS.Web.UI/Config/appsettings.Development.json b/src/FluentCMS.Web.UI/Config/appsettings.Development.json new file mode 100644 index 000000000..aa821b775 --- /dev/null +++ b/src/FluentCMS.Web.UI/Config/appsettings.Development.json @@ -0,0 +1,6 @@ +{ + "ConnectionStrings": { + "LiteDbFile": "bin/Debug/net8.0/.db/database.litedb" + }, + "AllowedHosts": "*" +} diff --git a/src/FluentCMS.Web.UI/Program.cs b/src/FluentCMS.Web.UI/Program.cs index 68ab18939..56c44f6c2 100644 --- a/src/FluentCMS.Web.UI/Program.cs +++ b/src/FluentCMS.Web.UI/Program.cs @@ -1,4 +1,5 @@ using FluentCMS; +using FluentCMS.Application; using FluentCMS.Repository.LiteDb; using FluentCMS.Web.UI; @@ -10,8 +11,14 @@ // add FluentCms core services.AddFluentCMSCore() - .AddLiteDbRepository(b => b.SetFilePath( - builder.Configuration.GetConnectionString("LiteDbFile") ?? throw new Exception("LiteDb file not defined."))); + .AddApplication() + .AddLiteDbRepository(b => + { + var liteDbFilePath = builder.Configuration.GetConnectionString("LiteDbFile") + ?? throw new Exception("LiteDb file not defined."); + Directory.CreateDirectory(Path.GetDirectoryName(liteDbFilePath)!); + b.SetFilePath(liteDbFilePath); + }); // Add services to the container. services.AddRazorComponents() @@ -38,12 +45,16 @@ var app = builder.Build(); - -app.UseExceptionHandler("/Error", createScopeForErrors: true); - -//app.UseHsts(); - -app.UseHttpsRedirection(); +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +} +else +{ + app.UseHsts(); + app.UseHttpsRedirection(); + app.UseExceptionHandler("/Error", createScopeForErrors: true); +} app.UseStaticFiles(); diff --git a/src/FluentCMS.sln b/src/FluentCMS.sln index 2e6c1a205..e6a33972d 100644 --- a/src/FluentCMS.sln +++ b/src/FluentCMS.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCMS.Shared", "FluentC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCMS.Web.UI", "FluentCMS.Web.UI\FluentCMS.Web.UI.csproj", "{144A959D-F8C0-4478-B8DA-57A2959B8C7D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCMS.Application", "FluentCMS.Application\FluentCMS.Application.csproj", "{675100EE-B527-4941-9B5E-CCE683E1EF34}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {144A959D-F8C0-4478-B8DA-57A2959B8C7D}.Debug|Any CPU.Build.0 = Debug|Any CPU {144A959D-F8C0-4478-B8DA-57A2959B8C7D}.Release|Any CPU.ActiveCfg = Release|Any CPU {144A959D-F8C0-4478-B8DA-57A2959B8C7D}.Release|Any CPU.Build.0 = Release|Any CPU + {675100EE-B527-4941-9B5E-CCE683E1EF34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {675100EE-B527-4941-9B5E-CCE683E1EF34}.Debug|Any CPU.Build.0 = Debug|Any CPU + {675100EE-B527-4941-9B5E-CCE683E1EF34}.Release|Any CPU.ActiveCfg = Release|Any CPU + {675100EE-B527-4941-9B5E-CCE683E1EF34}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE