PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
110
DataAccess/EF_Demo/Demo.cs
Normal file
110
DataAccess/EF_Demo/Demo.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
#if POSTGRES
|
||||
using EF_Demo.Data.Postgres;
|
||||
#else
|
||||
using EF_Demo.Data.MSSQL;
|
||||
#endif
|
||||
|
||||
using EF_Demo.Models;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EF_Demo;
|
||||
|
||||
internal class Demo
|
||||
{
|
||||
internal static int? AddEvent(IServiceProvider serviceProvider, string eventName, DateOnly eventDate)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
Event @event = new()
|
||||
{
|
||||
Name = eventName,
|
||||
EventDate = eventDate
|
||||
};
|
||||
ctx.Add(@event); //or ctx.Events.Add(@event) or ctx.Set<Event>().Add(@event);
|
||||
ctx.SaveChanges();
|
||||
logger.LogInformation($"Event {eventName} successfully added with id {@event.Id}");
|
||||
return @event.Id;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
logger.LogError($"Error adding event #{eventName}: {exc.CompleteExceptionMessage()}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void DeleteEvent(IServiceProvider serviceProvider, int eventId)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
Event? @event = ctx.Events.Find(eventId);
|
||||
if (@event != null)
|
||||
{
|
||||
ctx.Remove(@event); //or ctx.Entry(product).State = EntityState.Deleted;
|
||||
ctx.SaveChanges();
|
||||
logger.LogInformation($"Event {@event.Name} deleted");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogWarning($"Event #{eventId} does not exists");
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
logger.LogError($"Error deleting event #{eventId}: {exc.CompleteExceptionMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
internal static void PostponeEvent(IServiceProvider serviceProvider, int eventId, int days)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
Event? @event = ctx.Events.Find(eventId);
|
||||
if (@event != null)
|
||||
{
|
||||
@event.EventDate = @event.EventDate.AddDays(days);
|
||||
ctx.SaveChanges();
|
||||
logger.LogInformation($"Event {@event.Name} postoponed to {@event.EventDate:dd.MM.yyyy.}");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogWarning($"Event #{eventId} does not exists");
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
logger?.LogError($"Error trying to change evenbt #{eventId}: {exc.CompleteExceptionMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Print all people born in the provided year, ordered by country, and then by transliterated last name, and first name
|
||||
/// query is projected to an anonymous class
|
||||
/// </summary>
|
||||
internal static void PrintPeople(IServiceProvider serviceProvider, int year)
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
var query = ctx.People
|
||||
.Where(p => p.BirthDate.Year == year)
|
||||
.OrderBy(p => p.CountryCodeNavigation.Name)
|
||||
.ThenBy(p => p.LastNameTranscription)
|
||||
.ThenBy(p => p.FirstNameTranscription)
|
||||
.Select(p => new
|
||||
{
|
||||
p.FirstName, p.LastName,
|
||||
p.FirstNameTranscription, p.LastNameTranscription,
|
||||
p.BirthDate,
|
||||
Country = p.CountryCodeNavigation.Name
|
||||
});
|
||||
foreach (var p in query)
|
||||
{
|
||||
Console.WriteLine($"{p.FirstName} {p.LastName} ({p.FirstNameTranscription} {p.LastNameTranscription}), {p.Country} {p.BirthDate:dd.MM.yyyy.}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user