This repository has been archived on 2025-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
Files
KTUSA-PS/KTUSAPS/Services/DatabaseInitializationService.cs
Karolis Kundrotas aff6f8df82 Huge work
2021-10-25 22:00:01 +03:00

139 lines
5.6 KiB
C#

using KTUSAPS.Data;
using KTUSAPS.Data.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace KTUSAPS.Services
{
public class DatabaseInitializationService : IHostedService
{
private readonly IServiceProvider serviceProvider;
private readonly ILogger<DatabaseInitializationService> logger;
public DatabaseInitializationService(IServiceProvider serviceProvider, ILogger<DatabaseInitializationService> logger)
{
this.serviceProvider = serviceProvider;
this.logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = serviceProvider.CreateScope();
var dataContext = scope.ServiceProvider.GetRequiredService<SAPSDataContext>();
var migrations = (await dataContext.Database.GetPendingMigrationsAsync(cancellationToken: cancellationToken)).ToList();
if(migrations.Any())
{
logger.LogInformation($"There are {migrations.Count} pending migrations. Applying them");
try
{
await dataContext.Database.MigrateAsync(cancellationToken: cancellationToken);
await Seed(dataContext);
}
catch (Exception ex)
{
logger.LogError("Migration failed. Database may be corrupt!");
logger.LogError(ex, "Migration failed.");
}
}
}
public async Task Seed(SAPSDataContext dataContext)
{
var generalIssueType = await dataContext.IssueTypes.AddAsync(new IssueType()
{
Name = "Bendra",
NameEn = "General"
});
var otherIssueType = await dataContext.IssueTypes.AddAsync(new IssueType()
{
Name = "Kita",
NameEn = "Other"
});
var feedbackIssueType = await dataContext.IssueTypes.AddAsync(new IssueType()
{
Name = "Atsiliepimas",
NameEn = "Feedback"
});
await dataContext.SaveChangesAsync();
var issue1 = await dataContext.Issues.AddAsync(new Issue()
{
Created = DateTime.Now.AddDays(-5),
Description = "Man nepatinka dėstytojas.",
Email = "karolis.kundrotas@ktu.edu",
Publishable = true,
IssueType = generalIssueType.Entity
});
var issue2 = await dataContext.Issues.AddAsync(new Issue()
{
Created = DateTime.Now.AddDays(-12).AddHours(3),
Description = "Dėtytoja atsiskaitymo metu leido nusirašynėti kitiems, o man neleido.",
Email = "karolis.kundrotas@ktu.edu",
Publishable = true,
IssueType = otherIssueType.Entity
});
var issue3 = await dataContext.Issues.AddAsync(new Issue()
{
Created = DateTime.Now.AddDays(-18),
Description = "Tinklų destytoja per paskaitą neatsako į klausimus ir per paskaitą nieko neišmoko.",
Email = "karolis.kundrotas@ktu.edu",
Publishable = false,
IssueType = generalIssueType.Entity
});
var issue4 = await dataContext.Issues.AddAsync(new Issue()
{
Created = DateTime.Now.AddDays(-18),
Description = "Saitynų destytojas Tomas labai maloniai ir profesonaliai bendrauja.",
Email = "karolis.kundrotas@ktu.edu",
Publishable = true,
IssueType = feedbackIssueType.Entity,
Solved = true
});
await dataContext.SaveChangesAsync();
await dataContext.PublishedFeedbacks.AddAsync(new PublishedFeedback()
{
Issue = issue4.Entity,
FeedbackLt = "Studentas mano kad Saitynų dėstytojas Tomas yra profesonalus ir mandagiai bendraujantis.",
FeedbackEn = "Student thinks that Site creation module lecturer Tomas is profesonal ir pleasant at communications.",
});
var problem1 = await dataContext.PublishedProblems.AddAsync(new PublishedProblem()
{
Issue = issue2.Entity,
ProblemLt = "Atsikaitymo metu buvo leista nusirašynėti.",
ProblemEn = "During exam cheating was allowed.",
});
var problem2 = await dataContext.PublishedProblems.AddAsync(new PublishedProblem()
{
Issue = issue3.Entity,
ProblemLt = "Dėstytoja V. Pavardenė nemoko studentų per paskaitas, neraguoja į studentų klausimus, nesuteikia pagalbos.",
ProblemEn = "Lecturer V. Pavardenė does not lecture students, do not react to student questions and doesn't provide help."
});
await dataContext.SaveChangesAsync();
await dataContext.Solutions.AddAsync(new Solution()
{
Problem = problem2.Entity,
SolutionLt = "V. Parvedenei buvo priskirta tarnybinę nuobauda.",
SolutionEn = ""
});
await dataContext.SaveChangesAsync();
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}