generated from kzi-nastava/dotnet-wpf-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from kzi-nastava/database-connection
Fix: database migartions issues
- Loading branch information
Showing
21 changed files
with
751 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using LangLang.Composition; | ||
using LangLang.Domain.RepositoryInterfaces; | ||
using LangLang.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Npgsql; | ||
using System; | ||
using System.IO; | ||
using System.Windows; | ||
|
||
namespace LangLang | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
private readonly IHost _host; | ||
|
||
public App() | ||
{ | ||
|
||
DotNetEnv.Env.Load(); | ||
|
||
_host = Host.CreateDefaultBuilder().ConfigureServices((context, services) => | ||
{ | ||
var host = Environment.GetEnvironmentVariable("HOST"); | ||
var database = Environment.GetEnvironmentVariable("DATABASE"); | ||
var username = Environment.GetEnvironmentVariable("USERNAME"); | ||
var password = Environment.GetEnvironmentVariable("PASSWORD"); | ||
|
||
var connectionString = $"Host={host};Database={database};Username={username};Password={password}"; | ||
|
||
services.AddDbContext<DatabaseContext>(options => | ||
options.UseNpgsql(connectionString)); | ||
services.AddTransient<ITimeSlotRepository, TimeSlotRepository>(); | ||
services.AddTransient<ICourseRepository, CourseRepository>(); | ||
services.AddTransient<IExamSlotRepository, ExamSlotRepository>(); | ||
|
||
}).Build(); | ||
|
||
Injector.SetServiceProvider(_host.Services as ServiceProvider); | ||
ApplyMigrations(); | ||
} | ||
|
||
private void ApplyMigrations() | ||
{ | ||
using (var scope = _host.Services.CreateScope()) | ||
{ | ||
var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); | ||
db.Database.Migrate(); | ||
} | ||
} | ||
|
||
protected override void OnStartup(StartupEventArgs e) | ||
{ | ||
_host.Start(); | ||
base.OnStartup(e); | ||
} | ||
|
||
protected override void OnExit(ExitEventArgs e) | ||
{ | ||
_host.StopAsync().Wait(); | ||
base.OnExit(e); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using LangLang.Composition; | ||
using LangLang.Domain.Models; | ||
using LangLang.Domain.RepositoryInterfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LangLang.BusinessLogic.UseCases | ||
{ | ||
public class TimeSlotService | ||
{ | ||
|
||
|
||
private ITimeSlotRepository _timeSlot; | ||
|
||
|
||
public TimeSlotService() | ||
{ | ||
_timeSlot = Injector.CreateInstance<ITimeSlotRepository>(); | ||
} | ||
|
||
private int GenerateId() | ||
{ | ||
var last = GetAll().LastOrDefault(); | ||
return last?.Id + 1 ?? 0; | ||
} | ||
|
||
public List<TimeSlot> GetAll() | ||
{ | ||
return _timeSlot.GetAll(); | ||
} | ||
|
||
public TimeSlot Get(int id) | ||
{ | ||
return _timeSlot.Get(id); | ||
} | ||
|
||
public void Add(TimeSlot timeslot) | ||
{ | ||
timeslot.Id = GenerateId(); | ||
_timeSlot.Add(timeslot); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
LangLang/Domain/RepositoryInterfaces/ITimeSLotRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using LangLang.Domain.Models; | ||
using System.Collections.Generic; | ||
|
||
namespace LangLang.Domain.RepositoryInterfaces | ||
{ | ||
public interface ITimeSlotRepository | ||
{ | ||
public List<TimeSlot> GetAll(); | ||
public TimeSlot Get(int id); | ||
public void Add(TimeSlot timeSlot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net6.0-windows</TargetFramework> | ||
<TargetFramework>net7.0-windows</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="View\**" /> | ||
<EmbeddedResource Remove="View\**" /> | ||
<None Remove="View\**" /> | ||
<Page Remove="View\**" /> | ||
<Compile Remove="View*" /> | ||
<EmbeddedResource Remove="View*" /> | ||
<None Remove="View*" /> | ||
<Page Remove="View*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="DotNetEnv" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.20"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" /> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.18" /> | ||
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="25.2.5" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
Oops, something went wrong.