Skip to content

Commit

Permalink
11 Feb Added AutoMapper And MediatR
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihkabakk committed Feb 11, 2022
1 parent 82c40c8 commit b333a3c
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 21 deletions.
1 change: 1 addition & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.10">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
27 changes: 16 additions & 11 deletions API/Controllers/ActivitiesController.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Activities;
using Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Persistence;

namespace API.Controllers
{
public class ActivitiesController : BaseApiController
{
private readonly DataContext _context;

public ActivitiesController(DataContext context)
{
_context = context;
}

[HttpGet]
public async Task<ActionResult<List<Activity>>> GetActivities()
{
return await _context.Activities.ToListAsync();
return await Mediator.Send(new List.Query());
}

[HttpGet("{id}")]
public async Task<ActionResult<Activity>> GetActivity(Guid id)
{
return await _context.Activities.FindAsync(id);
return await Mediator.Send(new Details.Query { Id = id });
}

[HttpPost]
public async Task<IActionResult> CreateActivity(Activity activity)
{
return Ok(await Mediator.Send(new Create.Command { Activity = activity }));
}

[HttpPut("{id}")]
public async Task<IActionResult> EditActivity(Guid id, Activity activity)
{
activity.Id = id;
return Ok(await Mediator.Send(new Edit.Command { Activity = activity }));
}
}
}
7 changes: 5 additions & 2 deletions API/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BaseApiController : ControllerBase
{

private IMediator _mediator;
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.
GetService<IMediator>();
}
}
7 changes: 7 additions & 0 deletions API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Activities;
using Application.Core;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
Expand Down Expand Up @@ -47,6 +51,9 @@ public void ConfigureServices(IServiceCollection services)
policy.AllowAnyMethod().AllowAnyHeader().WithOrigins("https://localhost:3000");
});
});

services.AddMediatR(typeof(List.Handler).Assembly);
services.AddAutoMapper(typeof(MappingProfiles).Assembly);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Binary file modified API/reactivities.db
Binary file not shown.
38 changes: 38 additions & 0 deletions Application/Activities/Create.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Domain;
using MediatR;
using Persistence;

namespace Application.Activities
{
public class Create
{
public class Command : IRequest
{
public Activity Activity { get; set; }
}

public class Handler : IRequestHandler<Command>
{
private readonly DataContext _context;
public Handler(DataContext context)
{
_context = context;
}

public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
{
_context.Activities.Add(request.Activity);

await _context.SaveChangesAsync();

return Unit.Value;
}
}
}
}
34 changes: 34 additions & 0 deletions Application/Activities/Details.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Domain;
using MediatR;
using Persistence;

namespace Application.Activities
{
public class Details
{
public class Query : IRequest<Activity>
{
public Guid Id { get; set; }
}

public class Handler : IRequestHandler<Query, Activity>
{
private readonly DataContext _context;
public Handler(DataContext context)
{
_context = context;
}

public async Task<Activity> Handle(Query request, CancellationToken cancellationToken)
{
return await _context.Activities.FindAsync(request.Id);
}
}
}
}
41 changes: 41 additions & 0 deletions Application/Activities/Edit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Domain;
using MediatR;
using Persistence;

namespace Application.Activities
{
public class Edit
{
public class Command : IRequest
{
public Activity Activity { get; set; }
}

public class Handler : IRequestHandler<Command>
{
private readonly DataContext _context;
private readonly IMapper _mapper;
public Handler(DataContext context, IMapper mapper)
{
_mapper = mapper;
_context = context;
}

public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
{
var activity = await _context.Activities.FindAsync(request.Activity.Id);

_mapper.Map(request.Activity, activity);

await _context.SaveChangesAsync();
return Unit.Value;
}
}
}
}
35 changes: 35 additions & 0 deletions Application/Activities/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Domain;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Persistence;

namespace Application.Activities
{
public class List
{
public class Query : IRequest<List<Activity>>
{

}

public class Handler : IRequestHandler<Query, List<Activity>>
{
private readonly DataContext _context;

public Handler(DataContext context)
{
_context = context;
}

public async Task<List<Activity>> Handle(Query request, CancellationToken cancellationToken)
{
return await _context.Activities.ToListAsync();
}
}
}
}
5 changes: 5 additions & 0 deletions Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
Expand Down
8 changes: 0 additions & 8 deletions Application/Class1.cs

This file was deleted.

17 changes: 17 additions & 0 deletions Application/Core/MappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Domain;

namespace Application.Core
{
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Activity, Activity>();
}
}
}

0 comments on commit b333a3c

Please sign in to comment.