One context again

This commit is contained in:
mborzyszkowski 2020-09-27 14:17:47 +02:00
parent a06811fc9f
commit 2576fda4c8
10 changed files with 30 additions and 55 deletions

View File

@ -18,14 +18,12 @@ namespace InternshipSystem.Api.Controllers
[Route("companies")]
public class CompaniesController : ControllerBase
{
public CompaniesController(InternshipDbContext context, InternshipPractiseSupervisorDbContext practiseSupervisorDbContext)
public CompaniesController(InternshipDbContext context)
{
Context = context;
PractiseSupervisorDbContext = practiseSupervisorDbContext;
}
private InternshipDbContext Context { get; }
private InternshipPractiseSupervisorDbContext PractiseSupervisorDbContext { get; }
/// <summary>
/// Get companies matching provided paginated query
@ -94,7 +92,7 @@ namespace InternshipSystem.Api.Controllers
if (companyForm.Id.HasValue)
{
var companyToUpdate = await PractiseSupervisorDbContext.Companies.FindAsync(companyForm.Id);
var companyToUpdate = await Context.Companies.FindAsync(companyForm.Id);
if (companyToUpdate != null)
{
@ -113,10 +111,10 @@ namespace InternshipSystem.Api.Controllers
Name = companyForm.Name,
Nip = companyForm.Nip,
};
await PractiseSupervisorDbContext.Companies.AddAsync(newCompany, cancellationToken);
await Context.Companies.AddAsync(newCompany, cancellationToken);
}
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return Ok($"Company updated successfully");
}
@ -135,7 +133,7 @@ namespace InternshipSystem.Api.Controllers
[Authorize]
public async Task<ActionResult> DeleteCompany(long companyId, CancellationToken cancellationToken)
{
var companyToDelete = await PractiseSupervisorDbContext.Companies
var companyToDelete = await Context.Companies
.Include(c => c.Branches)
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken);
@ -144,9 +142,9 @@ namespace InternshipSystem.Api.Controllers
return NotFound($"Company with id: {companyId} does not exist");
}
PractiseSupervisorDbContext.Companies.Attach(companyToDelete);
PractiseSupervisorDbContext.Companies.Remove(companyToDelete);
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
Context.Companies.Attach(companyToDelete);
Context.Companies.Remove(companyToDelete);
await Context.SaveChangesAsync(cancellationToken);
return Ok($"Company with id: {companyId} deleted successfully");
}
@ -174,7 +172,7 @@ namespace InternshipSystem.Api.Controllers
return BadRequest(validationResult.ToString());
}
var company = await PractiseSupervisorDbContext.Companies
var company = await Context.Companies
.Include(c => c.Branches)
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken);
@ -214,7 +212,7 @@ namespace InternshipSystem.Api.Controllers
company.Branches.Add(newBranchOffice);
}
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return Ok($"Branch office updated successfully");
}
@ -234,7 +232,7 @@ namespace InternshipSystem.Api.Controllers
public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken)
{
var company =
await PractiseSupervisorDbContext.Companies
await Context.Companies
.Include(c => c.Branches)
.Where(c => c.Branches.Any(b => b.Id.Equals(branchOfficeId)))
.FirstOrDefaultAsync(cancellationToken: cancellationToken);
@ -247,7 +245,7 @@ namespace InternshipSystem.Api.Controllers
var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeId));
company.Branches.Remove(branchOffice);
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
return Ok($"Branch office with id: {branchOfficeId} deleted successfully");
}
}

View File

@ -1,9 +1,7 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using InternshipSystem.Api.Security;
using InternshipSystem.Core;
using InternshipSystem.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@ -22,7 +20,7 @@ namespace InternshipSystem.Api.Controllers
{
_context = context;
}
/// <summary>
/// Register student for edition using provided registration code
/// </summary>
@ -51,9 +49,8 @@ namespace InternshipSystem.Api.Controllers
edition.RegisterInternship(student);
await _context.SaveChangesAsync(token);
return Ok();
}
}
}

View File

@ -17,7 +17,7 @@ namespace InternshipSystem.Api.Extensions
try
{
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetService<InternshipPractiseSupervisorDbContext>();
using var context = serviceScope.ServiceProvider.GetService<InternshipDbContext>();
context.Database.Migrate();
@ -35,7 +35,7 @@ namespace InternshipSystem.Api.Extensions
public static IApplicationBuilder UseDefaultData(this IApplicationBuilder app, bool useDefaultData)
{
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetService<InternshipPractiseSupervisorDbContext>();
using var context = serviceScope.ServiceProvider.GetService<InternshipDbContext>();
var filler = serviceScope.ServiceProvider.GetService<DatabaseFiller>();
context.Database.Migrate();

View File

@ -42,8 +42,6 @@ namespace InternshipSystem.Api
services
.AddDbContext<InternshipDbContext>(o =>
o.UseNpgsql(Configuration.GetConnectionString("InternshipDatabase")))
.AddDbContext<InternshipPractiseSupervisorDbContext>(o =>
o.UseNpgsql(Configuration.GetConnectionString("InternshipDatabase")))
.AddScoped<DatabaseFiller>()
.AddScoped<IInternshipService, InternshipService>()
.AddScoped<JwtTokenService>()

View File

@ -169,6 +169,16 @@ namespace InternshipSystem.Repository
public async Task FillEditions()
{
var student = new Student
{
FirstName = "Trzeci",
LastName = "Trzecikowski",
AlbumNumber = 142351,
Email = "s102137@student.pg.edu.pl",
};
await Context.Students.AddAsync(student);
var editions = new List<Edition>
{
new Edition

View File

@ -1,12 +0,0 @@
using InternshipSystem.Core;
using Microsoft.EntityFrameworkCore;
namespace InternshipSystem.Repository
{
public class InternshipPractiseSupervisorDbContext : InternshipDbContext
{
public InternshipPractiseSupervisorDbContext(DbContextOptions<InternshipDbContext> options) : base(options)
{
}
}
}

View File

@ -1,16 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace InternshipSystem.Repository
{
public class InternshipPractiseSupervisorDbContextFactory : IDesignTimeDbContextFactory<InternshipPractiseSupervisorDbContext>
{
public InternshipPractiseSupervisorDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<InternshipDbContext>();
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu");
return new InternshipPractiseSupervisorDbContext(optionsBuilder.Options);
}
}
}

View File

@ -9,8 +9,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace InternshipSystem.Repository.Migrations
{
[DbContext(typeof(InternshipPractiseSupervisorDbContext))]
[Migration("20200923103350_init")]
[DbContext(typeof(InternshipDbContext))]
[Migration("20200927114840_init")]
partial class init
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

@ -8,8 +8,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace InternshipSystem.Repository.Migrations
{
[DbContext(typeof(InternshipPractiseSupervisorDbContext))]
partial class InternshipPractiseSupervisorDbContextModelSnapshot : ModelSnapshot
[DbContext(typeof(InternshipDbContext))]
partial class InternshipDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{