ready for database
This commit is contained in:
18
Apollon.EntityFramework/Apollon.EntityFramework.csproj
Normal file
18
Apollon.EntityFramework/Apollon.EntityFramework.csproj
Normal 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>
|
||||
44
Apollon.EntityFramework/Commands/CreateTournamentCommand.cs
Normal file
44
Apollon.EntityFramework/Commands/CreateTournamentCommand.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Apollon.EntityFramework/Commands/DeleteTournamentCommand.cs
Normal file
35
Apollon.EntityFramework/Commands/DeleteTournamentCommand.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Apollon.EntityFramework/Commands/UpdateTournamentCommand.cs
Normal file
42
Apollon.EntityFramework/Commands/UpdateTournamentCommand.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Apollon.EntityFramework/DTOs/TournamentDto.cs
Normal file
20
Apollon.EntityFramework/DTOs/TournamentDto.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
40
Apollon.EntityFramework/Queries/GetAllTournamentsQuery.cs
Normal file
40
Apollon.EntityFramework/Queries/GetAllTournamentsQuery.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Apollon.EntityFramework/TournamentsDBContext.cs
Normal file
19
Apollon.EntityFramework/TournamentsDBContext.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
24
Apollon.EntityFramework/TournamentsDBContextFactory.cs
Normal file
24
Apollon.EntityFramework/TournamentsDBContextFactory.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user