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
Binary file added ERD_Cinema.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

using api_cinema_challenge.Data;
using api_cinema_challenge.Models;
using workshop.webapi.Services;
using workshop.webapi.DataTransfer.Response;
using workshop.webapi.DataTransfer.Requests;
using api_cinema_challenge.Enums;

namespace workshop.webapi.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,
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs
{
public class CustomerPost
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
9 changes: 9 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/CustomerPut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs
{
public class CustomerPut
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
10 changes: 10 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/MoviePost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace api_cinema_challenge.DTOs
{
public class MoviePost
{
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
}
}
10 changes: 10 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/MoviePut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace api_cinema_challenge.DTOs
{
public class MoviePut
{
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
}
}
11 changes: 11 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/ScreeningGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace api_cinema_challenge.DTOs
{
public class ScreeningGet
{
public int Id { get; set; }
public int MovieId { get; set; }
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime StartsAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs
{
public class ScreeningPost
{
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime StartsAt { get; set; }
}
}
96 changes: 85 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,100 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using api_cinema_challenge.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace api_cinema_challenge.Data
{
public class CinemaContext : DbContext
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)
{

modelBuilder.Entity<Customer>().HasData(
new Customer
{
Id = 1,
Name = "Isabell Tran",
Email = "isabell@experis.com",
Phone = "12345678",
CreatedAt = new DateTime(2025, 08, 22, 10, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 22, 10, 00, 00, DateTimeKind.Utc)
}
);
modelBuilder.Entity<Customer>().HasData(
new Customer
{
Id = 2,
Name = "Marie Hansen",
Email = "Marie@experis.com",
Phone = "98989898",
CreatedAt = new DateTime(2025, 08, 23, 10, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 23, 10, 00, 00, DateTimeKind.Utc)
}
);

modelBuilder.Entity<Movie>().HasData(
new Movie
{
Id = 1,
Title = "Inception",
Rating = "PG-13",
Description = "A mind-bending thriller",
RuntimeMins = 148,
CreatedAt = new DateTime(2025, 08, 22, 15, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 22, 15, 00, 00, DateTimeKind.Utc)
}
);

modelBuilder.Entity<Movie>().HasData(
new Movie
{
Id = 2,
Title = "F1",
Rating = "PG-13",
Description = "Action",
RuntimeMins = 155,
CreatedAt = new DateTime(2025, 08, 22, 15, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 22, 15, 00, 00, DateTimeKind.Utc)
}
);

modelBuilder.Entity<Screening>().HasData(
new Screening
{
Id = 1,
MovieId = 1,
ScreenNumber = 5,
StartsAt = new DateTime(2025, 08, 22, 14, 00, 00, DateTimeKind.Utc),
Capacity = 100,
CreatedAt = new DateTime(2025, 08, 22, 14, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 22, 14, 00, 00, DateTimeKind.Utc)
}
);

modelBuilder.Entity<Ticket>().HasData(
new Ticket
{
Id = 1,
CustomerId = 1,
ScreeningId = 1,
NumSeats = 2,
CreatedAt = new DateTime(2025, 08, 22, 13, 00, 00, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 22, 13, 00, 00, DateTimeKind.Utc)
}
);
base.OnModelCreating(modelBuilder);

}

public DbSet<Customer> Customers { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<Screening> Screenings { get; set; }
public DbSet<Ticket> Tickets { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace workshop.webapi.DataTransfer.Requests;

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,19 @@


using api_cinema_challenge.Enums;
using System.ComponentModel.DataAnnotations;


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,9 @@
namespace workshop.webapi.DataTransfer.Response;


public class AuthResponse
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Token { get; set; }
}
Loading