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
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using api_cinema_challenge.Data;
using api_cinema_challenge.DataTransfer.Requests;
using api_cinema_challenge.DataTransfer.Response;
using api_cinema_challenge.Enums;
using api_cinema_challenge.Models;
using api_cinema_challenge.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

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,
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs.Customers
{
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.Customers
{
public class CustomerPost
{
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.Customers
{
public class CustomerPut
{
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/Movies/MovieDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs.Movies
{
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; }
}
}
13 changes: 13 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Movies/MoviePost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using api_cinema_challenge.DTOs.Screenings;

namespace api_cinema_challenge.DTOs.Movies
{
public class MoviePost
{
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public int RuntimeMins { get; set; }
public List<ScreeningPost>? screenings { get; set; }
}
}
10 changes: 10 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Movies/MoviePut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace api_cinema_challenge.DTOs.Movies
{
public class MoviePut
{
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,10 @@
using api_cinema_challenge.DTOs.Screenings;

namespace api_cinema_challenge.DTOs.Movies
{
public class MovieWithScreenings
{
public int Id { get; set; }
public List<ScreeningsForMovie> Screenings { get; set; } = new List<ScreeningsForMovie>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs.Screenings
{
public class ScreeningPost
{
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,15 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs.Screenings
{
public class ScreeningsForMovie
{
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; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs.Tickets
{
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.Tickets
{
public class TicketPost
{
public int numSeats { get; set; }
}
}
40 changes: 37 additions & 3 deletions api-cinema-challenge/api-cinema-challenge/Data/CinemaContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Microsoft.EntityFrameworkCore;
using api_cinema_challenge.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

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();
//this.Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Expand All @@ -20,7 +22,39 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

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

modelBuilder.Entity<Customer>().HasData(
new Customer { Id = 1, Name = "John Doe", Email = "john.doe@email.com", Phone = "+4792763498", CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc), UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc) },
new Customer { Id = 2, Name = "Jane Doe", Email = "jane.doe@email.com", Phone = "+4743761209", CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc), UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc) }
);

modelBuilder.Entity<Movie>().HasData(
new Movie { Id = 1, Title = "Inception", Rating = "PG-13", Description = "The film stars Leonardo DiCaprio as a professional " +
"thief who steals information by infiltrating the subconscious of his targets. He is offered a chance to have his criminal " +
"history erased as payment for the implantation of another person's idea into a target's subconscious.", RuntimeMins = 148,
CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc)},
new Movie { Id = 2, Title = "The Godfather", Rating = "R", Description = "\"The Godfather\" is based on Mario Puzo's novel of the " +
"same name. The film chronicles the life of the Corleone family, a powerful Italian-American mafia clan in New York City, focusing " +
"on the patriarch, Don Vito Corleone, and his youngest son, Michael Corleone.", RuntimeMins = 175,
CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc)}
);

modelBuilder.Entity<Screening>().HasData(
new Screening { Id = 1, MovieId = 1, ScreenNumber = 5, Capacity = 100, StartsAt = new DateTime(2025, 10, 01, 11, 3, 0, DateTimeKind.Utc),
CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc)},
new Screening { Id = 2, MovieId = 2, ScreenNumber = 3, Capacity = 150, StartsAt = new DateTime(2025, 10, 01, 12, 3, 0, DateTimeKind.Utc),
CreatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc),
UpdatedAt = new DateTime(2025, 08, 15, 10, 0, 0, DateTimeKind.Utc)}
);
}

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,13 @@
namespace api_cinema_challenge.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;

namespace api_cinema_challenge.DataTransfer.Requests
{
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 api_cinema_challenge.DataTransfer.Response
{
public class AuthResponse
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Token { get; set; }
}
}
Loading