One context again #51

Manually merged
Zonar merged 1 commits from MoreEndpoints into master 2020-09-27 14:18:38 +02:00
10 changed files with 30 additions and 55 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -169,6 +169,16 @@ namespace InternshipSystem.Repository
public async Task FillEditions() 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> var editions = new List<Edition>
{ {
new 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 namespace InternshipSystem.Repository.Migrations
{ {
[DbContext(typeof(InternshipPractiseSupervisorDbContext))] [DbContext(typeof(InternshipDbContext))]
[Migration("20200923103350_init")] [Migration("20200927114840_init")]
partial class init partial class init
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

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