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

Yeabsira.asessment #332

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
@@ -0,0 +1,42 @@
using CineFlex.Application.Features.Bookings.CQRS.Handlers;
using CineFlex.Application.Features.Bookings.CQRS.Requests.Commands;
using CineFlex.Application.Features.Bookings.CQRS.Requests.Queries;
using CineFlex.Application.Features.Bookings.DTOs;
using CineFlex.Application.Features.Movies.CQRS.Commands;
using CineFlex.Application.Features.Movies.CQRS.Queries;
using CineFlex.Application.Features.Movies.DTOs;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CineFlex.API.Controllers;

public class BookingsController : BaseApiController
{
private readonly IMediator _mediator;

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

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

}

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

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var command = new DeleteBookingCommand() { Id = id };
return HandleResult(await _mediator.Send(command));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ public async Task<IActionResult> Get(int id)
[HttpPost]
public async Task<IActionResult> Post([FromBody] CreateMovieDto createMovie)
{
// TODO:
var command = new CreateMovieCommand { MovieDto = createMovie };
return HandleResult(await _mediator.Send(command));
}

[HttpPut]
public async Task<IActionResult> Put([FromBody] UpdateMovieDto movieDto)
{


var command = new UpdateMovieCommand { MovieDto = movieDto };
return HandleResult(await _mediator.Send(command));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CineFlex.Application.Features.Movies.CQRS.Commands;
using CineFlex.Application.Features.Movies.CQRS.Queries;
using CineFlex.Application.Features.Movies.DTOs;
using CineFlex.Application.Features.Seats.CQRS.Requests.Commands;
using CineFlex.Application.Features.Seats.CQRS.Requests.Queries;
using CineFlex.Application.Features.Seats.DTOs;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CineFlex.API.Controllers;

public class SeatsController: BaseApiController
{

private readonly IMediator _mediator;

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

[HttpPost("all")]
public async Task<ActionResult<List<SeatDetailsDto>>> Get([FromBody] GetAllSeatsDto getAllSeatsDto)
{
return HandleResult(await _mediator.Send(new GetAllSeatsQuery(){GetAllSeatsDto = getAllSeatsDto}));
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
return HandleResult(await _mediator.Send(new GetSeatDetailsQuery { 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,68 @@
using System.Security.Claims;
using CineFlex.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace CineFlex.API.Controllers;

public class UsersController: BaseApiController
{

private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;

public UsersController(UserManager<User> userManager, SignInManager<User> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}

[HttpPost("/login")]
public async Task<ActionResult> Login([FromBody] LoginInfo loginInfo)
{
var user = await _userManager.FindByEmailAsync(email: loginInfo.email);

if (user == null)
return NotFound("User not found");

var signInResult = await _signInManager.PasswordSignInAsync(user, loginInfo.password, false, false);

if (signInResult.Succeeded)
return Ok("SUCCEEDED");

return Ok("DENIED");
}

[HttpPost("/signup")]
public async Task<ActionResult> Signup([FromBody] SignupInfo signupInfo)
{
var newUser = new User { UserName = signupInfo.username, Email = signupInfo.email};

var result = await _userManager.CreateAsync(newUser, signupInfo.password);

if (result.Succeeded)
{
var claimResult = await _userManager.AddClaimAsync(newUser, new Claim("Role", "User"));

if (claimResult.Succeeded)
return Ok("Created");
}

return Ok(result.Errors.First().Description);
}

public class LoginInfo
{
public string email { get; set; }
public string password { get; set; }
}

public class SignupInfo
{
public string username { get; set; }
public string email { get; set; }
public string password { get; set; }
}

}
7 changes: 7 additions & 0 deletions Backend/backend_assessment/CineFlex/CineFlex.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CineFlex.Application;
using CineFlex.Domain;
using CineFlex.Persistence;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Identity;
Expand All @@ -12,6 +13,12 @@
AddSwaggerDoc(builder.Services);
builder.Services.AddControllers();

// builder.Services.AddIdentity<User, IdentityRole>(options =>
// {
// options.User.RequireUniqueEmail = true;
// options.SignIn.RequireConfirmedAccount = false;
// })
// .AddEntityFrameworkStores<CineFlexDbContex>();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"CineFlexConnectionString": "User ID=postgres;Password=1234;Server=localhost;Port=5432;Database=CineFlex;Integrated Security=true;Pooling=true;"
"CineFlexConnectionString": "User ID=postgres;Password=dr;Server=localhost;Database=CineFlex"
},
"Logging": {
"LogLevel": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@
<ProjectReference Include="..\CineFlex.Domain\CineFlex.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Mocks\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CineFlex.Application.Contracts.Persistence;
using CineFlex.Domain;
using Moq;

namespace BlogApp.Tests.Mocks;

public static class MockSeatRepository
{
public static Mock<ISeatRepository> GetBlogRepository()
{
var seats = new List<Seat>
{
new ()
{
Movie = new Movie(){Genre = "genre", Title = "Title"},
Cinema = new CinemaEntity() {Location = "the location", Name = "The name"},
Location = "kjdkjsd",
Taken = false,
}
};

var mockRepo = new Mock<ISeatRepository>();

mockRepo.Setup(r => r.GetAll()).ReturnsAsync(seats);

mockRepo.Setup(r => r.Add(It.IsAny<Seat>())).ReturnsAsync((Seat seat) =>
{
seat.Id = seats.Count() + 1;
seats.Add(seat);
return seat;
});

mockRepo.Setup(r => r.Update(It.IsAny<Seat>())).Callback((Seat seat) =>
{
var newSeats = seats.Where((r) => r.Id != seat.Id).ToList();
newSeats.Add(seat);
});

mockRepo.Setup(r => r.Delete(It.IsAny<Seat>())).Callback((Seat seat) =>
{
if (seats.Exists(b => b.Id == seat.Id))
seats.Remove(seats.Find(b => b.Id == seat.Id)!);
});

mockRepo.Setup(r => r.Exists(It.IsAny<int>())).ReturnsAsync((int id) =>
{
var rate = seats.FirstOrDefault((r) => r.Id == id);
return rate != null;
});

mockRepo.Setup(r => r.Get(It.IsAny<int>()))!.ReturnsAsync((int id) =>
{
return seats.FirstOrDefault((r) => r.Id == id);
});

return mockRepo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using CineFlex.Application.Contracts.Persistence;
using Moq;

namespace BlogApp.Tests.Mocks
{
public static class MockUnitOfWork
{
public static Mock<IUnitOfWork> GetUnitOfWork()
{
var mockUow = new Mock<IUnitOfWork>();
var mockSeatRepo = MockSeatRepository.GetBlogRepository();
mockUow.Setup(r => r.SeatRepository).Returns(mockSeatRepo.Object);
mockUow.Setup(r => r.Save()).ReturnsAsync(1);
return mockUow;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using AutoMapper;
using Shouldly;
using Moq;
using Xunit;
using BlogApp.Tests.Mocks;
using CineFlex.Application.Contracts.Persistence;
using CineFlex.Application.Features.Seats.CQRS.Handlers.Commands;
using CineFlex.Application.Features.Seats.DTOs;
using CineFlex.Application.Profiles;

namespace BlogApp.Tests.Blog.Command;

public class CreateBlogCommandHandlerTest
{
private IMapper _mapper { get; set; }
private Mock<IUnitOfWork> _mockUnitOfWork { get; set; }
private CreateSeatCommandHandler _handler { get; set; }


public CreateBlogCommandHandlerTest()
{
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

_mapper = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
}).CreateMapper();

_handler = new CreateSeatCommandHandler(_mockUnitOfWork.Object, _mapper);
}


[Fact]
public async Task CreateBlogValid()
{

CreateSeatDto createBlogDto = new()
{
Movie = 1,
Cinema = 1,
Location = "dkjsd"
};

// var result = await _handler.Handle(new CreateBlogCommand() { CreateBlogDto = createBlogDto }, CancellationToken.None);
//
// result.Value.Content.ShouldBeEquivalentTo(createBlogDto.Content);
// result.Value.Title.ShouldBeEquivalentTo(createBlogDto.Title);
//
// (await _mockUnitOfWork.Object.BlogRepository.GetAll()).Count.ShouldBe(3);
}

// [Fact]
// public async Task CreateBlogInvalid()
// {
//
// CreateBlogDto createBlogDto = new()
// {
// Title = "", // Title can't be empty
// Content = "Body of the new blog",
// Publish = true,
// };
//
// var result = await _handler.Handle(new CreateBlogCommand() { CreateBlogDto = createBlogDto }, CancellationToken.None);
//
// result.Value.ShouldBe(null);
// }
}


Loading