mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 07:01:16 +03:00
Add project files.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Interfaces;
|
||||
using PracticeCalendar.Infrastructure.Notification;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
using System;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure
|
||||
{
|
||||
public static class InfrastructureDI
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfigurationRoot configuration)
|
||||
{
|
||||
string connectionString = configuration.GetConnectionString("SqliteConnection");
|
||||
|
||||
services.AddDbContext(connectionString);
|
||||
services.AddTransient<IEmailSender, FileEmailSender>();
|
||||
|
||||
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static void AddDbContext(this IServiceCollection services, string connectionString) =>
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PracticeCalendar.Domain.Interfaces;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Notification
|
||||
{
|
||||
public class FileEmailSender : IEmailSender
|
||||
{
|
||||
private readonly ILogger<FileEmailSender> logger;
|
||||
|
||||
public FileEmailSender(ILogger<FileEmailSender> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
public Task SendEmailAsync(string to, string from, string subject, string body)
|
||||
{
|
||||
logger.LogDebug($"Sending email from {from}, to {to}, subject {subject}, body {body}");
|
||||
//TODO save email content to a file
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Persistence
|
||||
{
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
|
||||
public DbSet<Attendee> Atendees => Set<Attendee>();
|
||||
public DbSet<PracticeEvent> PracticeEvents => Set<PracticeEvent>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Persistence
|
||||
{
|
||||
public class ApplicationDbContextInitialiser
|
||||
{
|
||||
private readonly ILogger<ApplicationDbContextInitialiser> logger;
|
||||
private readonly ApplicationDbContext context;
|
||||
|
||||
public ApplicationDbContextInitialiser(ILogger<ApplicationDbContextInitialiser> logger,
|
||||
ApplicationDbContext context)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public async Task InitialiseAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context.Database.IsSqlite())
|
||||
{
|
||||
await context.Database.MigrateAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while initialising the database.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SeedAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await TrySeedAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occurred while seeding the database.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task TrySeedAsync()
|
||||
{
|
||||
// Default data
|
||||
// Seed, if necessary
|
||||
if (!context.PracticeEvents.Any())
|
||||
{
|
||||
context.PracticeEvents.Add(new PracticeEvent("Event 1", "Event 1 desc"));
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Persistence.Configuration
|
||||
{
|
||||
public class AttendeeConfiguration : IEntityTypeConfiguration<Attendee>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Attendee> builder)
|
||||
{
|
||||
builder.Property(x => x.Name)
|
||||
.HasMaxLength(100)
|
||||
.IsRequired();
|
||||
builder.Property(x => x.EmailAddress)
|
||||
.HasMaxLength(100)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Persistence.Configuration
|
||||
{
|
||||
public class PracticeEventConfiguration : IEntityTypeConfiguration<PracticeEvent>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PracticeEvent> builder)
|
||||
{
|
||||
builder.Property(x => x.Title)
|
||||
.HasMaxLength(120)
|
||||
.IsRequired();
|
||||
builder.Property(x => x.Description)
|
||||
.HasMaxLength(1000)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Ardalis.Specification.EntityFrameworkCore;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Persistence
|
||||
{
|
||||
public class EfRepository<T> : RepositoryBase<T>, IRepository<T> where T : class, IAggregateRoot
|
||||
{
|
||||
public EfRepository(ApplicationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="6.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PracticeCalendar.Domain\PracticeCalendar.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user