Skip to content

Commit

Permalink
Merge pull request #456 from kzi-nastava/database-connection
Browse files Browse the repository at this point in the history
Fix: database migartions issues
  • Loading branch information
anasinik authored Jun 12, 2024
2 parents 92e5a33 + 950284d commit a95bb4c
Show file tree
Hide file tree
Showing 21 changed files with 751 additions and 227 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,5 @@ coverage*[.json, .xml, .info]
# Additional files built by Visual Studio

# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,csharp,visualstudio,visualstudiocode,vs,vim,rider,intellij+all,intellij+iml,intellij

*.env
69 changes: 60 additions & 9 deletions LangLang/App.xaml.cs
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);

Check warning on line 41 in LangLang/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'serviceProvider' in 'void Injector.SetServiceProvider(ServiceProvider 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);
}

}

}
7 changes: 4 additions & 3 deletions LangLang/BusinessLogic/UseCases/CourseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public List<string> GetLanguages()
return GetAll().Select(course => course.Language).Distinct().ToList();
}

public void Delete(int id)
public void Delete(Course course)
{
_courses.Delete(id);
_courses.Delete(course);
}

public void DeleteByTutor(Tutor tutor)
{
foreach (Course course in GetByTutor(tutor.Id))
Expand All @@ -60,7 +61,7 @@ public void DeleteByTutor(Tutor tutor)
{
EnrollmentRequestService enrollmentRequestService = new();
enrollmentRequestService.Delete(course);
Delete(course.Id);
Delete(course);
}
}
else
Expand Down
3 changes: 2 additions & 1 deletion LangLang/BusinessLogic/UseCases/ExamSlotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public bool Delete(int id)
if (exam == null) return false;

if (!((exam.TimeSlot.Time - DateTime.Now).TotalDays >= Constants.EXAM_MODIFY_PERIOD)) return false;
_exams.Delete(id);

_exams.Delete(exam);
return true;

}
Expand Down
47 changes: 47 additions & 0 deletions LangLang/BusinessLogic/UseCases/TimeSlotService.cs
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);
}

}
}
17 changes: 14 additions & 3 deletions LangLang/Configuration/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
using System;
using LangLang.Domain.RepositoryInterfaces;
using LangLang.Repositories;
using Microsoft.Extensions.DependencyInjection;

namespace LangLang.Composition
{
public class Injector
{

private static ServiceProvider _serviceProvider;

Check warning on line 12 in LangLang/Configuration/Injector.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_serviceProvider' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
public static void SetServiceProvider(ServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

private static Dictionary<Type, object> _implementations = new Dictionary<Type, object>
{
{ typeof(IStudentRepository), new StudentRepository() },
Expand All @@ -15,10 +23,10 @@ public class Injector
{ typeof(IEnrollmentRequestRepository), new EnrollmentRequestRepository() },
{ typeof(IWithdrawalRequestRepository), new WithdrawalRequestRepository() },
{ typeof(ITutorRatingRepository), new TutorRatingRepository() },
{ typeof(IExamSlotRepository), new ExamSlotRepository() },
//{ typeof(IExamSlotRepository), new ExamSlotRepository() },
{ typeof(IExamApplicationRepository), new ExamApplicationRepository() },
{ typeof(IPenaltyPointRepository), new PenaltyPointRepository() },
{ typeof(ICourseRepository), new CourseRepository() },
//{ typeof(ICourseRepository), new CourseRepository() },
{ typeof(IGradeRepository), new GradeRepository() },
{ typeof(IExamResultRepository), new ExamResultRepository() },
{ typeof(IEmailRepository), new EmailRepository() }
Expand All @@ -32,7 +40,10 @@ public static T CreateInstance<T>()
{
return (T)_implementations[type];
}

else
{
return _serviceProvider.GetRequiredService<T>();

Check warning on line 45 in LangLang/Configuration/Injector.cs

View workflow job for this annotation

GitHub Actions / build

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'ServiceProviderServiceExtensions.GetRequiredService<T>(IServiceProvider)'. Nullability of type argument 'T' doesn't match 'notnull' constraint.
}
throw new ArgumentException($"No implementation found for type {type}");
}
}
Expand Down
5 changes: 3 additions & 2 deletions LangLang/Domain/Models/TimeSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace LangLang.Domain.Models
{
public class TimeSlot
{
// NOTE: Adapt as needed during implementation
public int Id { get; set; }
public double Duration { get; set; }
public DateTime Time { get; set; }

public TimeSlot(double duration, DateTime time)
{
{

Duration = duration;
Time = time;
}
Expand Down
4 changes: 1 addition & 3 deletions LangLang/Domain/RepositoryInterfaces/ICourseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ internal interface ICourseRepository
public Course Get(int id);
public void Add(Course course);
public void Update(Course course);
public void Delete(int id);
public void Save();
public Dictionary<int, Course> Load();
public void Delete(Course course);
}
}
11 changes: 2 additions & 9 deletions LangLang/Domain/RepositoryInterfaces/IExamSlotRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ public interface IExamSlotRepository
public ExamSlot Get(int id);
public List<ExamSlot> GetExams(Tutor tutor);
public void Add(ExamSlot exam);
public void Delete(int id);
public void Delete(ExamSlot exam);
public void Update(ExamSlot exam);
public void Save();
public Dictionary<int, ExamSlot> Load();
/*
public List<ExamSlot> Search(List<ExamSlot> exams, DateTime examDate, string language, LanguageLevel? level);
public List<ExamSlot> SearchByTutor(Tutor tutor, DateTime examDate, string language, LanguageLevel? level);
public List<ExamSlot> SearchByStudent(AppController appController, Student student, DateTime examDate, string courseLanguage, LanguageLevel? languageLevel);
*/
}
}
}
12 changes: 12 additions & 0 deletions LangLang/Domain/RepositoryInterfaces/ITimeSLotRepository.cs
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);
}
}
25 changes: 17 additions & 8 deletions LangLang/LangLang.csproj
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>
Loading

0 comments on commit a95bb4c

Please sign in to comment.