Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,4 @@ FodyWeavers.xsd
*/**/bin/Release
*/**/obj/Debug
*/**/obj/Release
/api-cinema-challenge/api-cinema-challenge/Migrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using api_cinema_challenge.Data;
using api_cinema_challenge.DTOs.Auth;
using api_cinema_challenge.Enums;
using api_cinema_challenge.Models;
using api_cinema_challenge.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Data;

namespace api_cinema_challenge.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly CinemaContext _context;
private readonly TokenService _tokenService;

public UsersController(UserManager<ApplicationUser> userManager, CinemaContext context,
TokenService tokenService, ILogger<UsersController> logger)
{
_userManager = userManager;
_context = context;
_tokenService = tokenService;
}


[HttpPost]
[Route("register")]
public async Task<IActionResult> Register(RegistrationRequest request)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var result = await _userManager.CreateAsync(
new ApplicationUser { UserName = request.Username, Email = request.Email, Role = request.Role },
request.Password!
);

if (result.Succeeded)
{
request.Password = "";
return CreatedAtAction(nameof(Register), new { email = request.Email, role = Role.User }, request);
}

foreach (var error in result.Errors)
{
ModelState.AddModelError(error.Code, error.Description);
}

return BadRequest(ModelState);
}


[HttpPost]
[Route("login")]
public async Task<ActionResult<AuthResponse>> Authenticate([FromBody] AuthRequest request)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var managedUser = await _userManager.FindByEmailAsync(request.Email!);

if (managedUser == null)
{
return BadRequest("Bad credentials");
}

var isPasswordValid = await _userManager.CheckPasswordAsync(managedUser, request.Password!);

if (!isPasswordValid)
{
return BadRequest("Bad credentials");
}

var userInDb = _context.Users.FirstOrDefault(u => u.Email == request.Email);

if (userInDb is null)
{
return Unauthorized();
}

var accessToken = _tokenService.CreateToken(userInDb);
await _context.SaveChangesAsync();

return Ok(new AuthResponse
{
Username = userInDb.UserName,
Email = userInDb.Email,
Token = accessToken,
});
}
}
}
15 changes: 15 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Auth/AuthRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.Data;

namespace api_cinema_challenge.DTOs.Auth;

public class AuthRequest
{
public string? Email { get; set; }
public string? Password { get; set; }

public bool IsValid()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api_cinema_challenge.DTOs.Auth;

public class AuthResponse
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Token { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using api_cinema_challenge.Enums;
using System.ComponentModel.DataAnnotations;
using System.Data;

namespace api_cinema_challenge.DTOs.Auth;




public class RegistrationRequest
{
[Required]
public string? Email { get; set; }

[Required]
public string? Username { get { return this.Email; } set { } }

[Required]
public string? Password { get; set; }

public Role Role { get; set; } = Role.User;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace api_cinema_challenge.DTOs.Customer
{
public class CustomerDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs.Customer
{
public class CustomerPostDto
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs.Customer
{
public class CustomerPutDto
{
public string? Name { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
}
}
15 changes: 15 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Movie/MovieDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using api_cinema_challenge.DTOs.Screening;

namespace api_cinema_challenge.DTOs.Movie
{
public class MovieDto
{
public int Id { get; set; }
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using api_cinema_challenge.DTOs.Screening;

namespace api_cinema_challenge.DTOs.Movie
{
public class MoviePostDto
{
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
public ICollection<ScreeningPostDto> Screenings { get; set; } = new List<ScreeningPostDto>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace api_cinema_challenge.DTOs.Movie
{
public class MoviePutDto
{
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace api_cinema_challenge.DTOs.Screening
{
public class ScreeningDto
{
public int Id { get; set; }
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime StartsAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs.Screening
{
public class ScreeningPostDto
{
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime StartsAt { get; set; }
}
}
10 changes: 10 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Ticket/TicketDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace api_cinema_challenge.DTOs.Ticket
{
public class TicketDto
{
public int Id { get; set; }
public int NumSeats { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace api_cinema_challenge.DTOs.Ticket
{
public class TicketPostDto
{
public int NumSeats { get; set; }
}
}
54 changes: 43 additions & 11 deletions api-cinema-challenge/api-cinema-challenge/Data/CinemaContext.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
using Microsoft.EntityFrameworkCore;
using api_cinema_challenge.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

namespace api_cinema_challenge.Data
{
public class CinemaContext : DbContext
// IdentityUserContext instead of Db in workshop
public class CinemaContext : IdentityUserContext<ApplicationUser>
{
private string _connectionString;
public CinemaContext(DbContextOptions<CinemaContext> options) : base(options)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
_connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString")!;
this.Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_connectionString);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<ApplicationUser>()
.Property(u => u.Role)
.HasConversion<string>();

// relations
modelBuilder.Entity<Ticket>()
.HasOne(t => t.Customer)
.WithMany(c => c.Tickets)
.HasForeignKey(t => t.CustomerId);

modelBuilder.Entity<Ticket>()
.HasOne(t => t.Screening)
.WithMany(s => s.Tickets)
.HasForeignKey(t => t.ScreeningId);

modelBuilder.Entity<Screening>()
.HasOne(s => s.Movie)
.WithMany(m => m.Screenings)
.HasForeignKey(s => s.MovieId);

// seeder
var seeder = new Seeder();
seeder.Seed();
modelBuilder.Entity<Customer>().HasData(seeder.Customers);
modelBuilder.Entity<Movie>().HasData(seeder.Movies);
modelBuilder.Entity<Screening>().HasData(seeder.Screenings);
modelBuilder.Entity<Ticket>().HasData(seeder.Tickets);


}

public DbSet<Customer> Customers { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<Screening> Screenings { get; set; }
public DbSet<Ticket> Tickets { get; set; }
}
}
Loading