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,101 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using api_cinema_challenge.Data;
using api_cinema_challenge.Models;
using api_cinema_challenge.DataTransfer.Requests;
using api_cinema_challenge.DataTransfer.Response;
using api_cinema_challenge.Enums;
using api_cinema_challenge.Services;

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,
});
}
}
}
9 changes: 9 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace api_cinema_challenge.DTOs
{
public class CustomerDTO
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
}
14 changes: 14 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/MovieDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs
{
public class MovieDTO
{
public string Title { get; set; }
public string Description { get; set; }
public string Rating { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public int RuntimeMins { get; set; }
}
}
18 changes: 18 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/ScreeningDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs
{
public class ScreeningDTO
{
public int MovieId { get; set; }
public MovieDTO Movie { get; set; }
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public DateTime StartsAt { get; set; }

}
}
12 changes: 12 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/ScreeningPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace api_cinema_challenge.DTOs
{
public class ScreeningPost
{
public int MovieId { get; set; }
public int ScreenNumber { get; set; }
public int Capacity { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public DateTime StartsAt { get; set; }
}
}
17 changes: 17 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/TicketDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs
{
public class TicketDTO
{
public int NumSeats { get; set; }
public ScreeningDTO Screening { get; set; }
public CustomerDTO Customer { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
}
}
12 changes: 12 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/TicketPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTOs
{
public class TicketPost
{
public int NumSeats { get; set; }
public int ScreeningId { get; set; }
public int CustomerId { get; set; }
}
}
90 changes: 88 additions & 2 deletions api-cinema-challenge/api-cinema-challenge/Data/CinemaContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using api_cinema_challenge.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

namespace api_cinema_challenge.Data
Expand All @@ -10,17 +11,102 @@ 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)
{
optionsBuilder.UseNpgsql(_connectionString);


}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().HasData(
new Customer() {
Id = 1,
Name = "Trym Berger",
Email = "Trym@Berger.no",
Phone = "12459732"
},
new Customer()
{
Id = 2,
Name = "Monika Synnove",
Email = "mogak@haugan.no",
Phone = "56753212"
}
);
modelBuilder.Entity<Movie>().HasData(
new Movie() {
Id = 1,
Title = "Howl's Moving Castle",
Description = "Howl's Moving Castle is a 2004 Japanese animated fantasy film written and directed by Hayao Miyazaki",
Rating = "G",
RuntimeMins = 119,
CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
},
new Movie()
{
Id = 2,
Title = "Kiki's Delivery Service",
Description = "A 1989 Japanese animated fantasy film written, produced, and directed by Hayao Miyazaki, based on Eiko Kadono's 1985 novel Kiki's Delivery Service.",
Rating = "G",
RuntimeMins = 102,
CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
}
);
modelBuilder.Entity<Screening>().HasData(
new Screening()
{
Id = 1,
ScreenNumber = 1,
Capacity = 200,
StartsAt = DateTime.Parse("2025-09-12 14:30").ToUniversalTime(),
MovieId = 1,
CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
},
new Screening()
{
Id = 2,
ScreenNumber = 2,
Capacity = 130,
StartsAt = DateTime.Parse("2025-09-13 20:30").ToUniversalTime(),
MovieId = 2,
CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
}
);

modelBuilder.Entity<Ticket>().HasData(
new Ticket()
{
Id = 1,
NumSeats = 1,
ScreeningId = 2,
CustomerId = 1,
CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
},
new Ticket()
{
Id = 2,
NumSeats = 3,
ScreeningId = 1,
CustomerId = 2,
CreatedAt = DateTime.MinValue,
UpdatedAt= DateTime.MinValue
}
);
}

public DbSet<Movie> Movies { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Screening> Screenings { get; set; }
public DbSet<ApplicationUser> Users { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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,18 @@

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


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