2023-09-15 09:52:44 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-09-18 13:23:20 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using CsvHelper;
|
|
|
|
using backend;
|
2023-09-15 09:52:44 +00:00
|
|
|
|
2023-09-13 08:37:53 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
2023-10-02 09:45:08 +00:00
|
|
|
|
|
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy(name: MyAllowSpecificOrigins,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.WithOrigins("http://localhost:5173");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-09-13 08:37:53 +00:00
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
2023-09-15 09:52:44 +00:00
|
|
|
var configuration = builder.Configuration;
|
|
|
|
|
|
|
|
builder.Services.AddDbContext<MovieDbContext>(options =>
|
|
|
|
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
|
2023-09-18 13:23:20 +00:00
|
|
|
var services = builder.Services.BuildServiceProvider();
|
|
|
|
using (var scope = services.CreateScope())
|
|
|
|
{
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<MovieDbContext>();
|
2023-10-03 08:29:41 +00:00
|
|
|
|
|
|
|
if (context.Database.EnsureCreated())
|
|
|
|
{
|
|
|
|
context.Database.Migrate();
|
|
|
|
}
|
2023-09-18 13:23:20 +00:00
|
|
|
|
|
|
|
// Check if movies are already inserted to avoid duplicate insertion
|
|
|
|
if (!context.Movies.Any())
|
|
|
|
{
|
|
|
|
using (var reader = new StreamReader("public/DbMockData.csv"))
|
|
|
|
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
|
|
|
|
{
|
|
|
|
var records = csv.GetRecords<MovieDB>().ToList();
|
|
|
|
context.Movies.AddRange(records);
|
|
|
|
context.SaveChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 08:37:53 +00:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
2023-10-02 09:45:08 +00:00
|
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
|
|
|
2023-09-13 08:37:53 +00:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
app.Run();
|