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

Backend.Tewodros.imp-backend-assesment [Tewodros Alemu] #325

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CineFlex.Application\CineFlex.Application.csproj" />
<ProjectReference Include="..\CineFlex.Domain\CineFlex.Domain.csproj" />
<ProjectReference Include="..\CineFlex.Identity\CineFlex.Identity.csproj" />
<ProjectReference Include="..\CineFlex.Persistence\CineFlex.Persistence.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CineFlex.Application.Contracts.Identity;
using CineFlex.Application.Models.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace CineFlex.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly IAuthService _authenticationService;
public AccountController(IAuthService authenticationService)
{
_authenticationService = authenticationService;
}

[HttpPost("login")]
public async Task<ActionResult<AuthResponse>> Login(AuthRequest request)
{
return Ok(await _authenticationService.Login(request));
}

[HttpPost("register")]
public async Task<ActionResult<RegistrationResponse>> Register(RegistrationRequest request)
{
return Ok(await _authenticationService.Register(request));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CineFlex.Application.Features.Seats.CQRS.Commands;
using CineFlex.Application.Features.Seats.CQRS.Queries;
using CineFlex.Application.Features.Seats.DTO;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace CineFlex.API.Controllers
{
[Route("api/[Controller]")]
[ApiController]
public class SeatController : BaseApiController
{
private readonly IMediator _mediator;

public SeatController(IMediator mediator)
{
_mediator = mediator;
}



[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
return HandleResult(await _mediator.Send(new GetSeatRequest { Id = id }));

}

[HttpPost]
public async Task<IActionResult> Post([FromBody] CreateSeatDto createSeatDto)
{
var command = new CreateSeatCommand { CreateSeatDto = createSeatDto };
return HandleResult(await _mediator.Send(command));
}

[HttpPut]
public async Task<IActionResult> Put([FromBody] UpdateSeatDto updateSeatDto)
{


var command = new UpdateSeatCommand { UpdateSeatDto = updateSeatDto };
return HandleResult(await _mediator.Send(command));
}

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var command = new DeleteSeatCommand { Id = id };
return HandleResult(await _mediator.Send(command));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using CineFlex.Application.Exceptions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace CineFlex.Api.Middleware
{
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
public ExceptionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception ex)
{
await HandleExceptionAsync(httpContext, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
string result = JsonConvert.SerializeObject(new ErrorDeatils
{
ErrorMessage = exception.Message,
ErrorType = "Failure"
});

switch (exception)
{
case BadRequestException badRequestException:
statusCode = HttpStatusCode.BadRequest;
break;
case ValidationException validationException:
statusCode = HttpStatusCode.BadRequest;
result = JsonConvert.SerializeObject(validationException.Errors);
break;
case NotFoundException notFoundException:
statusCode = HttpStatusCode.NotFound;
break;
default:
break;
}

context.Response.StatusCode = (int)statusCode;
return context.Response.WriteAsync(result);
}
}

public class ErrorDeatils
{
public string ErrorType { get; set; }
public string ErrorMessage { get; set; }
}
}
6 changes: 6 additions & 0 deletions Backend/backend_assessment/CineFlex/CineFlex.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using CineFlex.Application;
using CineFlex.Persistence;
using CineFlex.Identity;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Identity;
using CineFlex.Api.Middleware;
using Microsoft.AspNetCore.Cors.Infrastructure;

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.ConfigureApplicationServices();
builder.Services.ConfigurePersistenceServices(builder.Configuration);
builder.Services.ConfigureIdentityServices(builder.Configuration);

builder.Services.AddHttpContextAccessor();
AddSwaggerDoc(builder.Services);
builder.Services.AddControllers();
Expand Down Expand Up @@ -40,6 +45,7 @@
app.UseHttpsRedirection();

app.UseAuthorization();
app.UseMiddleware<ExceptionMiddleware>();


app.MapControllers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"CineFlexConnectionString": "User ID=postgres;Password=1234;Server=localhost;Port=5432;Database=CineFlex;Integrated Security=true;Pooling=true;"
"CineFlexConnectionString": "User ID=postgres;Password=postgres;Server=localhost;Port=5432;Database=CineFlex;Integrated Security=true;Pooling=true;",
"CineFlexIdentityConnectionString": "User ID=postgres;Password=postgres;Server=localhost;Port=5432;Database=CineFlexIdentity;Integrated Security=true;Pooling=true;"
},
"Logging": {
"LogLevel": {
Expand All @@ -12,5 +13,12 @@
"compilationOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"JwtSettings": {
"Key": "2J9JFA9THQTH9AHRHTQ9YAQTJ",
"Issuer": "CineFlexApi",
"Audience": "CineFlexApiUser",
"DurationInMinutes": 60
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

<ItemGroup>
<Folder Include="Contracts\Identity\" />
<Folder Include="Features\Bookings\CQRS\Handlers\" />
<Folder Include="Features\Bookings\CQRS\Commands\" />
<Folder Include="Features\Bookings\CQRS\Queries\" />
<Folder Include="Features\Bookings\DTO\Validators\" />
<Folder Include="Features\Genres\CQRS\Commands\" />
<Folder Include="Features\Genres\CQRS\Handlers\" />
<Folder Include="Features\Genres\CQRS\Queries\" />
<Folder Include="Features\_Indices\CQRS\Commands\" />
<Folder Include="Features\_Indices\CQRS\Handlers\" />
<Folder Include="Features\_Indices\CQRS\Queries\" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CineFlex.Application.Models.Identity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Contracts.Identity
{
public interface IAuthService
{
Task<AuthResponse> Login(AuthRequest request);
Task<RegistrationResponse> Register(RegistrationRequest request);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CineFlex.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Contracts.Persistence
{
public interface IBookingRepository : IGenericRepository<Booking>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CineFlex.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Contracts.Persistence
{
public interface IGenreRepository : IGenericRepository<Genre>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CineFlex.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Contracts.Persistence
{
public interface ISeatRepository : IGenericRepository<Seat>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public interface IUnitOfWork : IDisposable
{
IMovieRepository MovieRepository { get; }
ICinemaRepository CinemaRepository { get; }
ISeatRepository SeatRepository { get; }
IBookingRepository BookingRepository { get; }
IGenreRepository GenreRepository { get; }

Task<int> Save();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CineFlex.Application.Features.Common;
using CineFlex.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Features.Bookings.DTO
{
public class BookingDto : BaseDto, IBookingDto
{
public DateTime BookingTime { get; set; }
public decimal TotalPrice { get; set; }
public int MovieId { get; set; }
public List<int> SeatIds { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }


}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Features.Bookings.DTO
{
public class CreateBookingDto
{
public int MovieId { get; set; }
public List<int> SeatIds { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Features.Bookings.DTO
{
public interface IBookingDto
{
int Id { get; set; }
DateTime BookingTime { get; set; }
decimal TotalPrice { get; set; }
int MovieId { get; set; }
List<int> SeatIds { get; set; }
string CustomerName { get; set; }
string CustomerEmail { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CineFlex.Application.Features.Common;
using CineFlex.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Features.Bookings.DTO
{
public class UpdateBookingDto : BaseDto, IBookingDto
{
public DateTime BookingTime { get; set; }
public decimal TotalPrice { get; set; }
public int MovieId { get; set; }
public List<int> SeatIds { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.Features.Genres.DTO
{
public class CreateGenreDto
{
public string Name { get; set; }
}
}
Loading