mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 09:01:18 +03:00
nice refactorings
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace PracticeCalendar.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ApiControllerBase : ControllerBase
|
||||
{
|
||||
private ISender _mediator = null!;
|
||||
|
||||
protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetRequiredService<ISender>();
|
||||
}
|
||||
}
|
||||
@@ -1,111 +1,61 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PracticeCalendar.API.Model;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Entities.Specifications;
|
||||
using PracticeCalendar.API.Controllers;
|
||||
using PracticeCalendar.Application.PracticeEvents.Commands;
|
||||
using PracticeCalendar.Application.PracticeEvents.Queries;
|
||||
using PracticeCalendar.Application.PracticeEvents.Queries.GetPracticeEvents;
|
||||
|
||||
namespace PrcaticeCalendar.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class EventsController : ControllerBase
|
||||
public class EventsController : ApiControllerBase
|
||||
{
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
private readonly IMapper mapper;
|
||||
private readonly ILogger<EventsController> logger;
|
||||
|
||||
public EventsController(IRepository<PracticeEvent> eventsRepo,
|
||||
IMapper mapper,
|
||||
ILogger<EventsController> logger)
|
||||
public EventsController(ILogger<EventsController> logger)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
this.mapper = mapper;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetAll")]
|
||||
public async Task<ActionResult<List<EventModel>>> Get()
|
||||
public async Task<ActionResult<List<PracticeEventDto>>> Get()
|
||||
{
|
||||
|
||||
var spec = new PracticeEventsWithAttendees();
|
||||
var repoList = await eventsRepo.ListAsync(spec);
|
||||
var evList = repoList.Select(x=> {
|
||||
var model = mapper.Map<EventModel>(x);
|
||||
model.Attendees = x.Attendees.Select(m=>mapper.Map<AttendeeModel>(m)).ToArray();
|
||||
return model;
|
||||
})
|
||||
.ToList();
|
||||
return evList;
|
||||
return await Mediator.Send(new GetPracticeEventsQuery());
|
||||
}
|
||||
|
||||
[HttpPost(Name = "Create practice event")]
|
||||
public async Task<IActionResult> CreateEvent(EventModel eventModel)
|
||||
public async Task<ActionResult<PracticeEventDto>> CreateEvent(PracticeEventDto eventModel)
|
||||
{
|
||||
var practiceEvent = new PracticeEvent(eventModel.Title, eventModel.Description);
|
||||
foreach (var att in eventModel.Attendees)
|
||||
{
|
||||
practiceEvent.AddAttendee(new Attendee(att.Name, att.EmailAddress));
|
||||
}
|
||||
var result = await eventsRepo.AddAsync(practiceEvent);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok(mapper.Map<EventModel>(result));
|
||||
return await Mediator.Send(new CreatePracticeEventCommand(eventModel));
|
||||
}
|
||||
|
||||
[HttpPut(Name = "Update practice event")]
|
||||
public async Task<IActionResult> UpdateEvent(EventModel eventModel)
|
||||
public async Task<ActionResult<PracticeEventDto>> UpdateEvent(PracticeEventDto eventModel)
|
||||
{
|
||||
var practiceEvent = await eventsRepo.GetByIdAsync(eventModel.Id);
|
||||
if(practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.UpdateTitleAndDescription(eventModel.Title, eventModel.Description);
|
||||
await eventsRepo.UpdateAsync(practiceEvent);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok(mapper.Map<EventModel>(practiceEvent));
|
||||
return await Mediator.Send(new UpdatePracticeEventCommand(eventModel));
|
||||
}
|
||||
|
||||
[HttpDelete(Name = "Delete practice event")]
|
||||
public async Task<IActionResult> DeleteEvent(int practiceEventId)
|
||||
public async Task<ActionResult> DeleteEvent(int practiceEventId)
|
||||
{
|
||||
var org = await eventsRepo.GetByIdAsync(practiceEventId);
|
||||
if (org == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
await eventsRepo.DeleteAsync(org);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
await Mediator.Send(new DeletePracticeEventCommand(practiceEventId));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("accept/{eventId}/{attendeeId}")]
|
||||
public async Task<IActionResult> AttendeeAcceptEvent(int eventId, int attendeeId)
|
||||
public async Task<ActionResult> AttendeeAcceptEvent(int eventId, int attendeeId)
|
||||
{
|
||||
var spec = new PracticeEventByIdWithAttendees(eventId);
|
||||
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.AttendeeAcceptEvent(attendeeId);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
await Mediator.Send(new AttendeeAcceptEventCommand(eventId, attendeeId));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("decline/{eventId}/{attendeeId}")]
|
||||
public async Task<IActionResult> AttendeeDeclineEvent(int eventId, int attendeeId)
|
||||
public async Task<ActionResult> AttendeeDeclineEvent(int eventId, int attendeeId)
|
||||
{
|
||||
var spec = new PracticeEventByIdWithAttendees(eventId);
|
||||
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.AttendeeDeclineEvent(attendeeId);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
await Mediator.Send(new AttendeeDeclineEventCommand(eventId, attendeeId));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
using AutoMapper;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
namespace PracticeCalendar.API.Model
|
||||
{
|
||||
public class EventModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime EndTime { get; set; }
|
||||
|
||||
public AttendeeModel[] Attendees { get; set; } = Array.Empty<AttendeeModel>();
|
||||
}
|
||||
|
||||
public class AttendeeModel
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string EmailAddress { get; set; } = string.Empty;
|
||||
public bool IsAttending { get; set; }
|
||||
}
|
||||
|
||||
public class EventModelProfile : Profile
|
||||
{
|
||||
public EventModelProfile()
|
||||
{
|
||||
CreateMap<PracticeEvent, EventModel>();
|
||||
CreateMap<EventModel, PracticeEvent>();
|
||||
CreateMap<Attendee, AttendeeModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
|
||||
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
|
||||
<PackageReference Include="Mapster.Core" Version="1.2.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
@@ -19,4 +19,8 @@
|
||||
<ProjectReference Include="..\PracticeCalendar.Infrastructure\PracticeCalendar.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Model\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Hellang.Middleware.ProblemDetails;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using PracticeCalendar.Application;
|
||||
using PracticeCalendar.Infrastructure;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
using System;
|
||||
@@ -16,14 +17,18 @@ namespace PracticeCalendar
|
||||
|
||||
builder.Services.AddProblemDetails();
|
||||
|
||||
builder.Services.AddAutoMapper(typeof(Program));
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//inject application
|
||||
builder.Services.AddApplicationServices();
|
||||
//inject infrastructure
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddInfrastructureServices(builder.Configuration);
|
||||
|
||||
//mapster
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user