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 Screenshot 2025-01-22 100528.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTO/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTO
{
public class CustomerDTO
{
[Column("customerId")]
public int customerId { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Email")]
public string Email { get; set; }
[Column("Phone")]
public string Phone { get; set; }
[Column("CreatedAt")]
public string CreatedAt { get; set; }
[Column("UpdatedAt")]
public string UpdatedAt { get; set; }
[Column("tickets")]
public virtual List<string> tickets { get; set; } = new List<string>();

public CustomerDTO(Customer customer)
{
customerId = customer.customerId;
Name = customer.Name;
Email = customer.Email;
Phone = customer.Phone;
CreatedAt = customer.CreatedAt.ToString();
UpdatedAt = customer.UpdatedAt.ToString();
//making ticket dtos
customer.tickets.ForEach(x => tickets.Add($" screenNumber: {x.screen.screenNumber} "));
}
}
}
29 changes: 29 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTO/MovieDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTO
{
public class MovieDTO
{
public int movieId { get; set; }
public string Title { get; set; }
public string Rating { get; set; }
public string Description { get; set; }
public string RuntimeMins { get; set; }
public string CreatedAt { get; set; }
public string UpdatedAt { get; set; }
public virtual List<string> screens { get; set; } = new List<string>();

public MovieDTO(Movie movie)
{
movieId = movie.movieId;
Title = movie.Title;
Rating = movie.Rating;
Description = movie.Description;
RuntimeMins = movie.RuntimeMins;
CreatedAt = movie.CreatedAt.ToString();
UpdatedAt = movie.UpdatedAt.ToString();
movie.screens.ForEach(x => screens.Add(x.screenNumber.ToString()));
}
}
}
33 changes: 33 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTO/ScreenDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace api_cinema_challenge.DTO
{
public class ScreenDTO
{
public int screenId { get; set; }
public int screenNumber { get; set; }
public int capacity { get; set; }
public string startsAt { get; set; }
public string createdAt { get; set; }
public string updatedAt { get; set; }
public virtual List<string> tickets { get; set; }
public virtual string movie { get; set; }

public ScreenDTO(Screen screen)
{
screenId = screen.screenId;
screenNumber = screen.screenNumber;
capacity = screen.capacity;
startsAt = screen.startsAt;
createdAt = screen.createdAt;
updatedAt = screen.updatedAt;
tickets = new List<string>();

//convert tickets and movies to strings
screen.tickets.ForEach(x => tickets.Add($" ticket id : {x.ticketId}, customer: {x.customer.Name}"));
movie = $" movie id {screen.movieId}, movie title {screen.movie.Title}";

}
}
}
31 changes: 31 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTO/TicketDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTO
{
public class TicketDTO
{

public int ticketId { get; set; }

public int screenNumber { get; set; }

public int customerID { get; set; }
[JsonIgnore]
public string customer { get; set; }




public TicketDTO(Ticket ticket)
{
ticketId = ticket.ticketId;
screenNumber = ticket.screen.screenNumber;
customerID = ticket.customerID;
//fill in customer
customer = ticket.customer.Name.ToString();
}
}

}
85 changes: 84 additions & 1 deletion api-cinema-challenge/api-cinema-challenge/Data/CinemaContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Microsoft.EntityFrameworkCore;
using api_cinema_challenge.DTO;
using api_cinema_challenge.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.JSInterop.Infrastructure;
using Newtonsoft.Json.Linq;


namespace api_cinema_challenge.Data
{
public class CinemaContext : DbContext
Expand All @@ -10,17 +14,96 @@ public CinemaContext(DbContextOptions<CinemaContext> options) : base(options)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
_connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString")!;
this.Database.SetConnectionString(_connectionString);
this.Database.EnsureCreated();
}

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

//keys
modelBuilder.Entity<Customer>()
.HasKey(a => a.customerId);

modelBuilder.Entity<Movie>()
.HasKey(a => a.movieId);

modelBuilder.Entity<Screen>()
.HasKey(a => a.screenId);

modelBuilder.Entity<Ticket>()
.HasKey(a => a.ticketId);


//defining relations
modelBuilder.Entity<Customer>()
.HasMany(a => a.tickets)
.WithOne(a => a.customer);

modelBuilder.Entity<Ticket>()
.HasOne(a => a.customer)
.WithMany(a => a.tickets)
.HasForeignKey(a => a.customerID);

modelBuilder.Entity<Ticket>()
.HasOne(a => a.screen)
.WithMany(a => a.tickets)
.HasForeignKey(a => a.screenId);

modelBuilder.Entity<Screen>()
.HasOne(a => a.movie)
.WithMany(a => a.screens);

modelBuilder.Entity<Screen>()
.HasMany(a => a.tickets)
.WithOne(a => a.screen);


modelBuilder.Entity<Movie>()
.HasMany(a => a.screens)
.WithOne(a => a.movie);

//seeding
modelBuilder.Entity<Movie>()
.HasData(
new List<Movie>
{
new Movie { movieId=1 ,Title = "the hobbit",UpdatedAt=DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString(), CreatedAt =DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString() ,Rating = "10",Description= "a lonely hobbit travels the world together with his trusty friends gollum and sauron", RuntimeMins="120"}
}
);
modelBuilder.Entity<Customer>()
.HasData(
new List<Customer>
{
new Customer {customerId =1, Name="bob" , CreatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString(), Email="bob@gmail.com", Phone="12345678" , UpdatedAt=DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString()}
}
);
modelBuilder.Entity<Screen>()
.HasData(
new List<Screen>
{
new Screen {screenId=1, capacity=2, movieId=1,createdAt= DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString(), screenNumber=1, startsAt=DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString(), updatedAt= DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToString()}
}
);
modelBuilder.Entity<Ticket>()
.HasData(
new List<Ticket>
{
new Ticket {ticketId=1, customerID=1, screenId=1}
}
);

}
public DbSet<Movie> Movies { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Screen > Screens { get; set; }
public DbSet<Ticket> Tickets { get; set; }

}
}
Loading