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

Implement MediatR, Refactor CreateCommunity #33

Merged
merged 3 commits into from
Nov 14, 2024
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
21 changes: 8 additions & 13 deletions src/LinkMe/LinkMe.Api/Controllers/CommunitiesController.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
using LinkMe.ApplicationServices;
using LinkMe.ApplicationServices.Communities;
using LinkMe.Domain;

using MediatR;

using Microsoft.AspNetCore.Mvc;

namespace LinkMe.Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class CommunitiesController: ControllerBase
{
private readonly CreateCommunity.Handler _createCommunityHandler;

public CommunitiesController(CreateCommunity.Handler createCommunityHandler)
{
_createCommunityHandler = createCommunityHandler;
}


public class CommunitiesController(ISender sender) : ControllerBase
{
[HttpPost]
public Community RegisterCommunity(CreateCommunity.Command community)
{
return _createCommunityHandler.Handle(community);
}
public async Task<Community> RegisterCommunity(RegisterCommunity.Request request) =>
await sender.Send(request);
}
}
1 change: 1 addition & 0 deletions src/LinkMe/LinkMe.Api/LinkMe.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/LinkMe/LinkMe.Api/LinkMe.Api.http
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@LinkMe.Api_HostAddress = http://localhost:5143

GET {{LinkMe.Api_HostAddress}}/weatherforecast/
GET {{LinkMe.Api_HostAddress}}/communities/
Accept: application/json

###
11 changes: 3 additions & 8 deletions src/LinkMe/LinkMe.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@

using LinkMe.ApplicationServices;
using LinkMe.ApplicationServices.Communities;
using LinkMe.Domain.Contracts;
using LinkMe.Infrastructure;
using LinkMe.Infrastructure.Database;
using LinkMe.Infrastructure.Sqlite;
using LinkMe.Infrastructure.SqlServer;

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace LinkMe.Api
{
Expand All @@ -22,6 +17,8 @@ public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMediatR(configuration=>configuration.RegisterServicesFromAssembly(typeof(RegisterCommunity).Assembly));

//Add services to the container.

if (builder.Environment.IsDevelopment())
Expand Down Expand Up @@ -51,8 +48,6 @@ public static void Main(string[] args)
builder.Services.AddDbContext<LinkMeDbContext, LinkMeSqlServerDbContext>();

builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
builder.Services.AddScoped<CreateCommunity.Handler>();


builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using LinkMe.Domain;
using LinkMe.Domain.Contracts;

using MediatR;

namespace LinkMe.ApplicationServices.Communities
{
public class RegisterCommunity
{
public record Request(string Name, string Description)
: IRequest<Community>
{
//public string Category { get; set; }
public Community ToCommunity() =>
new() { Name = Name, Description = Description };
}

public class Handler(IBaseRepository<Community> _repository)
: IRequestHandler<Request, Community>
{
public async Task<Community> Handle(Request request, CancellationToken cancellationToken)
{
var entity = request.ToCommunity();
var result = await _repository.AddAsync(entity);
return result;
}
}
}
}
31 changes: 0 additions & 31 deletions src/LinkMe/LinkMe.ApplicationServices/CreateCommunity.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\LinkMe.Domain\LinkMe.Domain.csproj" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/LinkMe/LinkMe.Domain/Contracts/IBaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface IBaseRepository<T> where T : DomainEntity
{
T Get(int id);
//ICollection<T> GetMany(int id);
T Add(T item);
Task<T> AddAsync(T item);
T Update(T item);
void Delete(T item);
}
Expand Down
4 changes: 2 additions & 2 deletions src/LinkMe/LinkMe.Infrastructure/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public BaseRepository(LinkMeDbContext dbContext)
_dbContext = dbContext;
}

public T Add(T item)
public async Task<T> AddAsync(T item)
{
var entity = _dbContext.Add(item).Entity;
_dbContext.SaveChanges();
await _dbContext.SaveChangesAsync();
return entity;
}

Expand Down
2 changes: 1 addition & 1 deletion src/LinkMe/LinkMe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkMe.Infrastructure.SqlSe
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{FF07EE0E-7140-4398-A94C-B14117492853}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkMe.Architecture.Tests", "LinkMe.Architecture.Tests\LinkMe.Architecture.Tests.csproj", "{C5347917-E6BE-45B9-93D9-5D05E70AFDB6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkMe.Architecture.Tests", "LinkMe.Architecture.Tests\LinkMe.Architecture.Tests.csproj", "{C5347917-E6BE-45B9-93D9-5D05E70AFDB6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down