ready for database

This commit is contained in:
Natlinux81
2022-08-20 01:41:56 +02:00
parent fcd1d90292
commit 68f0cec9d0
28 changed files with 356 additions and 36 deletions

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Apollon.Domain\Apollon.Domain.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,44 @@
using Apollon.Domain.Commands;
using Apollon.Domain.Models;
using Apollon.EntityFramework.DTOs;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework.Commands
{
public class CreateTournamentCommand : ICreateTournamentCommand
{
private readonly TournamentsDBContextFactory _contextFactory;
public CreateTournamentCommand(TournamentsDBContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task Execute(Tournament tournament)
{
using (TournamentsDBContext context = _contextFactory.Create())
{
TournamentDto tournamentDto = new TournamentDto()
{
Id = tournament.Id,
Organisation = tournament.Organisation,
TournamentName = tournament.TournamentName,
Competition = tournament.Competition,
StartDate = tournament.StartDate,
EndDate = tournament.EndDate,
Location = tournament.Location,
Rounds = tournament.Rounds,
};
context.Tournaments.Add(tournamentDto);
await context.SaveChangesAsync();
}
}
}
}

View File

@@ -0,0 +1,35 @@
using Apollon.Domain.Commands;
using Apollon.Domain.Models;
using Apollon.EntityFramework.DTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework.Commands
{
public class DeleteTournamentCommand : IDeleteTournamentCommand
{
private readonly TournamentsDBContextFactory _contextFactory;
public DeleteTournamentCommand(TournamentsDBContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task Execute(Guid id)
{
using (TournamentsDBContext context = _contextFactory.Create())
{
TournamentDto tournamentDto = new TournamentDto()
{
Id = id,
};
context.Tournaments.Remove(tournamentDto);
await context.SaveChangesAsync();
}
}
}
}

View File

@@ -0,0 +1,42 @@
using Apollon.Domain.Commands;
using Apollon.Domain.Models;
using Apollon.EntityFramework.DTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework.Commands
{
public class UpdateTournamentCommand : IUpdateTournamentCommand
{
private readonly TournamentsDBContextFactory _contextFactory;
public UpdateTournamentCommand(TournamentsDBContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task Execute(Tournament tournament)
{
using (TournamentsDBContext context = _contextFactory.Create())
{
TournamentDto tournamentDto = new TournamentDto()
{
Id = tournament.Id,
Organisation = tournament.Organisation,
TournamentName = tournament.TournamentName,
Competition = tournament.Competition,
StartDate = tournament.StartDate,
EndDate = tournament.EndDate,
Location = tournament.Location,
Rounds = tournament.Rounds,
};
context.Tournaments.Update(tournamentDto);
await context.SaveChangesAsync();
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework.DTOs
{
public class TournamentDto
{
public Guid Id { get; set; }
public string Organisation { get; set; }
public string TournamentName { get; set; }
public string Competition { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Location { get; set; }
public int Rounds { get; set; }
}
}

View File

@@ -0,0 +1,40 @@
using Apollon.Domain.Models;
using Apollon.Domain.Queries;
using Apollon.EntityFramework.DTOs;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework.Queries
{
public class GetAllTournamentsQuery : IGetAllTournamentsQuery
{
private readonly TournamentsDBContextFactory _contextFactory;
public GetAllTournamentsQuery(TournamentsDBContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task<IEnumerable<Tournament>> Execute()
{
using (TournamentsDBContext context = _contextFactory.Create())
{
IEnumerable<TournamentDto> tournamentsDtos = await context.Tournaments.ToListAsync();
return tournamentsDtos.Select(y => new Tournament(
y.Id,
y.Organisation,
y.TournamentName,
y.Competition,
y.StartDate,
y.EndDate,
y.Location,
y.Rounds));
}
}
}
}

View File

@@ -0,0 +1,19 @@
using Apollon.EntityFramework.DTOs;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework
{
public class TournamentsDBContext : DbContext
{
public TournamentsDBContext(DbContextOptions options) : base(options)
{
}
public DbSet<TournamentDto> Tournaments { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apollon.EntityFramework
{
public class TournamentsDBContextFactory
{
private readonly DbContextOptions _options;
public TournamentsDBContextFactory(DbContextOptions options)
{
_options = options;
}
public TournamentsDBContext Create()
{
return new TournamentsDBContext(_options);
}
}
}