Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add application layer and add user basic actions #87

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/FluentCMS.Application/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/FluentCMS.Application/FluentCMS.Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentCMS.Domain\FluentCMS.Domain.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/FluentCMS.Application/Users/CreateUserCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MediatR;

namespace FluentCMS.Application.Users;
public class CreateUserCommand : IRequest<Guid>
{
public string Name { get; set; } = string.Empty;
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
8 changes: 8 additions & 0 deletions src/FluentCMS.Application/Users/GetUserByIdQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FluentCMS.Entities;
using MediatR;

namespace FluentCMS.Application.Users;
public class GetUserByIdQuery : IRequest<User?>
{
public Guid Id { get; set; }
}
7 changes: 7 additions & 0 deletions src/FluentCMS.Application/Users/GetUsersQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using FluentCMS.Entities;
using MediatR;

namespace FluentCMS.Application.Users;
public class GetUsersQuery : IRequest<IEnumerable<User>>
{
}
42 changes: 42 additions & 0 deletions src/FluentCMS.Application/Users/UserHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentCMS.Entities;
using FluentCMS.Services;
using MediatR;

namespace FluentCMS.Application.Users;
internal class UserHandlers :
IRequestHandler<GetUsersQuery, IEnumerable<User>>,
IRequestHandler<GetUserByIdQuery, User?>,
IRequestHandler<CreateUserCommand, Guid>
{
private readonly UserService _userService;

public UserHandlers(UserService userService)
{
_userService = userService;
}

public async Task<IEnumerable<User>> Handle(GetUsersQuery request, CancellationToken cancellationToken)
{
var users = await _userService.GetAll();
return users;
}

public async Task<User?> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
{
var user = await _userService.GetById(request.Id);
return user;
}

public async Task<Guid> 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;
}
}
37 changes: 37 additions & 0 deletions src/FluentCMS.Shared/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -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<ActionResult<IEnumerable<User>>> GetUsers()
{
var data = await _mediator.Send(new GetUsersQuery());
return Ok(data);
}

[HttpGet("{id}")]
public async Task<ActionResult<User?>> GetUserById([FromRoute] Guid id)
{
var data = await _mediator.Send(new GetUserByIdQuery { Id = id });
return Ok(data);
}

[HttpPost]
public async Task<ActionResult<Guid>> CreateUser(CreateUserCommand request)
{
var result = await _mediator.Send(request);
return Ok(result);
}
}
1 change: 1 addition & 0 deletions src/FluentCMS.Shared/FluentCMS.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentCMS.Application\FluentCMS.Application.csproj" />
<ProjectReference Include="..\FluentCMS.Domain\FluentCMS.Domain.csproj" />
</ItemGroup>
</Project>
3 changes: 0 additions & 3 deletions src/FluentCMS.Web.UI/Config/appsettings,Development.json

This file was deleted.

6 changes: 6 additions & 0 deletions src/FluentCMS.Web.UI/Config/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ConnectionStrings": {
"LiteDbFile": "bin/Debug/net8.0/.db/database.litedb"
},
"AllowedHosts": "*"
}
27 changes: 19 additions & 8 deletions src/FluentCMS.Web.UI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentCMS;
using FluentCMS.Application;
using FluentCMS.Repository.LiteDb;
using FluentCMS.Web.UI;

Expand All @@ -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()
Expand All @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions src/FluentCMS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down