Merge pull request 'New DbContext and crud for companies' (#40) from AnotherDbSet into master
This commit is contained in:
commit
0c58248d2a
@ -4,7 +4,9 @@ using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentValidation;
|
||||
using InternshipSystem.Api.Options;
|
||||
using InternshipSystem.Api.Queries;
|
||||
using InternshipSystem.Api.Security;
|
||||
using InternshipSystem.Core;
|
||||
using InternshipSystem.Repository;
|
||||
@ -50,7 +52,7 @@ namespace InternshipSystem.Api.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == id);
|
||||
var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == id, cancellationToken: cancellationToken);
|
||||
|
||||
if (student == null)
|
||||
{
|
||||
@ -104,7 +106,7 @@ namespace InternshipSystem.Api.Controllers
|
||||
|
||||
return Ok(_tokenService.generateToken(newIdentity));
|
||||
}
|
||||
|
||||
|
||||
private Student CreateStudentWithCasData(CasUserData casData)
|
||||
{
|
||||
var id = long.Parse(casData.PersonNumber);
|
||||
|
@ -8,6 +8,7 @@ using InternshipSystem.Repository;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using static System.String;
|
||||
|
||||
namespace InternshipSystem.Api.Controllers
|
||||
{
|
||||
@ -15,16 +16,21 @@ namespace InternshipSystem.Api.Controllers
|
||||
[Route("companies")]
|
||||
public class CompaniesController : ControllerBase
|
||||
{
|
||||
public CompaniesController(InternshipDbContext context)
|
||||
public CompaniesController(InternshipDbContext context, InternshipPractiseSupervisorDbContext practiseSupervisorDbContext)
|
||||
{
|
||||
Context = context;
|
||||
PractiseSupervisorDbContext = practiseSupervisorDbContext;
|
||||
}
|
||||
|
||||
private InternshipDbContext Context { get; }
|
||||
private InternshipPractiseSupervisorDbContext PractiseSupervisorDbContext { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get companies matching provided paginated query
|
||||
/// </summary>
|
||||
/// <param name="searchQuery">Paginated query description</param>
|
||||
/// <response code="200">Successfully retrieved Companies</response>
|
||||
/// <response code="400">Search query was malformed</response>
|
||||
/// <returns>Part of companies collection</returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
@ -60,6 +66,183 @@ namespace InternshipSystem.Api.Controllers
|
||||
.Take(searchQuery.PerPage)
|
||||
.ToListAsync(token);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates or add new company (if not new than contains id)
|
||||
/// </summary>
|
||||
/// <param name="companyForm"></param>
|
||||
/// <response code="200">Successfully updated company</response>
|
||||
/// <response code="400">Company form was malformed</response>
|
||||
/// <response code="404">Company not found</response>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> UpdateCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken)
|
||||
{
|
||||
var validator = new CompanyForm.Validator();
|
||||
var validationResult = await validator.ValidateAsync(companyForm, cancellationToken);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.ToString());
|
||||
}
|
||||
|
||||
if (companyForm.Id.HasValue)
|
||||
{
|
||||
var companyToUpdate = await PractiseSupervisorDbContext.Companies.FindAsync(companyForm.Id);
|
||||
|
||||
if (companyToUpdate != null)
|
||||
{
|
||||
companyToUpdate.Name = IsNullOrEmpty(companyForm.Name) ? companyToUpdate.Name : companyForm.Name;
|
||||
companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound($"Company with id: {companyForm.Id} does not exist");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var newCompany = new Company
|
||||
{
|
||||
Name = companyForm.Name,
|
||||
Nip = companyForm.Nip,
|
||||
};
|
||||
await PractiseSupervisorDbContext.Companies.AddAsync(newCompany, cancellationToken);
|
||||
}
|
||||
|
||||
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
|
||||
return Ok($"Company updated successfully");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes existing company by id
|
||||
/// </summary>
|
||||
/// <param name="companyId"></param>
|
||||
/// <response code="200">Successfully deleted company</response>
|
||||
/// <response code="400">Company id is empty</response>
|
||||
/// <response code="404">Company not found</response>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{companyId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteCompany(long companyId, CancellationToken cancellationToken)
|
||||
{
|
||||
var companyToDelete = await PractiseSupervisorDbContext.Companies
|
||||
.Include(c => c.Branches)
|
||||
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken);
|
||||
|
||||
if (companyToDelete == null)
|
||||
{
|
||||
return NotFound($"Company with id: {companyId} does not exist");
|
||||
}
|
||||
|
||||
PractiseSupervisorDbContext.Companies.Attach(companyToDelete);
|
||||
PractiseSupervisorDbContext.Companies.Remove(companyToDelete);
|
||||
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
|
||||
return Ok($"Company with id: {companyId} deleted successfully");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates or add new branchOffice (if not new than contains id)
|
||||
/// </summary>
|
||||
/// <param name="branchOfficeForm"></param>
|
||||
/// <param name="companyId"></param>
|
||||
/// <response code="200">Successfully updated company branch office</response>
|
||||
/// <response code="400">Branch office was malformed/response>
|
||||
/// <response code="404">Company or branch office not found</response>
|
||||
/// <returns></returns>
|
||||
[HttpPut("branchOffice/{companyId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> UpdateBranch([FromBody] BranchOfficeForm branchOfficeForm, long companyId, CancellationToken cancellationToken)
|
||||
{
|
||||
var validator = new BranchOfficeForm.Validator();
|
||||
var validationResult = await validator.ValidateAsync(branchOfficeForm, cancellationToken);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.ToString());
|
||||
}
|
||||
|
||||
var company = await PractiseSupervisorDbContext.Companies
|
||||
.Include(c => c.Branches)
|
||||
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken);
|
||||
|
||||
if (company == null)
|
||||
{
|
||||
return NotFound($"Company with id: {companyId} does not exist");
|
||||
}
|
||||
|
||||
if (branchOfficeForm.Id.HasValue)
|
||||
{
|
||||
var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeForm.Id.Value));
|
||||
|
||||
if (branchOffice == null)
|
||||
{
|
||||
return NotFound($"Branch office with id: {branchOfficeForm.Id} does not exist");
|
||||
}
|
||||
|
||||
branchOffice.Address.Country = IsNullOrEmpty(branchOfficeForm.Country) ? branchOffice.Address.Country : branchOfficeForm.Country;
|
||||
branchOffice.Address.City = IsNullOrEmpty(branchOfficeForm.City) ? branchOffice.Address.City : branchOfficeForm.City;
|
||||
branchOffice.Address.PostalCode = IsNullOrEmpty(branchOfficeForm.PostalCode) ? branchOffice.Address.PostalCode : branchOfficeForm.PostalCode;
|
||||
branchOffice.Address.Street = IsNullOrEmpty(branchOfficeForm.Street) ? branchOffice.Address.Street : branchOfficeForm.Street;
|
||||
branchOffice.Address.Building = IsNullOrEmpty(branchOfficeForm.Building) ? branchOffice.Address.Building : branchOfficeForm.Building;
|
||||
}
|
||||
else
|
||||
{
|
||||
var newBranchOffice = new BranchOffice
|
||||
{
|
||||
Address = new BranchAddress
|
||||
{
|
||||
Country = branchOfficeForm.Country,
|
||||
City = branchOfficeForm.City,
|
||||
PostalCode = branchOfficeForm.PostalCode,
|
||||
Street = branchOfficeForm.Street,
|
||||
Building = branchOfficeForm.Building,
|
||||
}
|
||||
};
|
||||
company.Branches.Add(newBranchOffice);
|
||||
}
|
||||
|
||||
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
|
||||
return Ok($"Branch office updated successfully");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes existing branchOffice
|
||||
/// </summary>
|
||||
/// <param name="branchOfficeId"></param>
|
||||
/// <response code="200">Successfully deleted company branch office</response>
|
||||
/// <response code="400">Branch office id is empty</response>
|
||||
/// <response code="404">Company or branch office not found</response>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("branchOffice/{branchOfficeId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken)
|
||||
{
|
||||
var company =
|
||||
await PractiseSupervisorDbContext.Companies
|
||||
.Include(c => c.Branches)
|
||||
.Where(c => c.Branches.Any(b => b.Id.Equals(branchOfficeId)))
|
||||
.FirstOrDefaultAsync(cancellationToken: cancellationToken);
|
||||
|
||||
if (company == null)
|
||||
{
|
||||
return NotFound($"Branch office with id: {branchOfficeId} does not exist");
|
||||
}
|
||||
|
||||
var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeId));
|
||||
company.Branches.Remove(branchOffice);
|
||||
|
||||
await PractiseSupervisorDbContext.SaveChangesAsync(cancellationToken);
|
||||
return Ok($"Branch office with id: {branchOfficeId} deleted successfully");
|
||||
}
|
||||
}
|
||||
}
|
56
src/InternshipSystem.Api/Controllers/StudentController.cs
Normal file
56
src/InternshipSystem.Api/Controllers/StudentController.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using InternshipSystem.Api.Queries;
|
||||
using InternshipSystem.Api.Security;
|
||||
using InternshipSystem.Core;
|
||||
using InternshipSystem.Repository;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace InternshipSystem.Api.Controllers
|
||||
{
|
||||
[Route("student")]
|
||||
[ApiController]
|
||||
public class StudentController : ControllerBase
|
||||
{
|
||||
private readonly InternshipDbContext _context;
|
||||
|
||||
public StudentController(InternshipDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet("current")]
|
||||
[Authorize]
|
||||
public async Task<Student> GetCurrentStudentData([FromServices] User user, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Students.FindAsync(user.PersonNumber);
|
||||
}
|
||||
|
||||
[HttpPut("updateCurrent")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, [FromServices] User user, CancellationToken cancellationToken)
|
||||
{
|
||||
var validator = new StudentForm.Validator();
|
||||
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
|
||||
|
||||
if (!validationResult.IsValid || !user.PersonNumber.Equals(studentNewData.Id))
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var currentStudent = await _context.Students.FindAsync(user.PersonNumber);
|
||||
|
||||
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
|
||||
currentStudent.FirstName = string.IsNullOrEmpty(studentNewData.FirstName) ? currentStudent.FirstName : studentNewData.FirstName;
|
||||
currentStudent.LastName = string.IsNullOrEmpty(studentNewData.LastName) ? currentStudent.LastName : studentNewData.LastName;
|
||||
currentStudent.Email = string.IsNullOrEmpty(studentNewData.Email) ? currentStudent.Email : studentNewData.Email;
|
||||
currentStudent.Course = string.IsNullOrEmpty(studentNewData.Course) ? currentStudent.Course : studentNewData.Course;
|
||||
currentStudent.Semester = studentNewData.Semester ?? currentStudent.Semester;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
@ -17,9 +17,9 @@ namespace InternshipSystem.Api.Queries
|
||||
{
|
||||
RuleFor(b => b.Id).NotNull()
|
||||
.When(c =>
|
||||
!string.IsNullOrEmpty(c.Country) || !string.IsNullOrEmpty(c.City) ||
|
||||
!string.IsNullOrEmpty(c.PostalCode) || !string.IsNullOrEmpty(c.Street) ||
|
||||
!string.IsNullOrEmpty(c.Building));
|
||||
string.IsNullOrEmpty(c.Country) || string.IsNullOrEmpty(c.City) ||
|
||||
string.IsNullOrEmpty(c.PostalCode) || string.IsNullOrEmpty(c.Street) ||
|
||||
string.IsNullOrEmpty(c.Building));
|
||||
RuleFor(b => b.Country).NotNull()
|
||||
.When(b => !b.Id.HasValue);
|
||||
RuleFor(b => b.City).NotNull()
|
||||
|
@ -13,7 +13,7 @@ namespace InternshipSystem.Api.Queries
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(c => c.Id).NotNull()
|
||||
.When(c => !string.IsNullOrEmpty(c.Nip) || !string.IsNullOrEmpty(c.Name));
|
||||
.When(c => string.IsNullOrEmpty(c.Nip) || string.IsNullOrEmpty(c.Name));
|
||||
RuleFor(c => c.Nip).NotEmpty()
|
||||
.When(c => !c.Id.HasValue);
|
||||
RuleFor(c => c.Name).NotEmpty()
|
||||
|
24
src/InternshipSystem.Api/Queries/StudentForm.cs
Normal file
24
src/InternshipSystem.Api/Queries/StudentForm.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using InternshipSystem.Core;
|
||||
|
||||
namespace InternshipSystem.Api.Queries
|
||||
{
|
||||
public class StudentForm
|
||||
{
|
||||
public long? Id { get; set; }
|
||||
public int? AlbumNumber { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Course { get; set; }
|
||||
public int? Semester { get; set; }
|
||||
|
||||
public class Validator : AbstractValidator<StudentForm>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(c => c.Id).NotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -43,11 +43,13 @@ 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>()
|
||||
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>());
|
||||
|
||||
|
||||
services
|
||||
.AddSwaggerGen(options =>
|
||||
{
|
||||
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using InternshipSystem.Core.Entity.Internship;
|
||||
using InternshipSystem.Core.UglyOrmArtifacts;
|
||||
|
||||
@ -48,10 +47,5 @@ namespace InternshipSystem.Core
|
||||
{
|
||||
return start >= EditionStart && end <= EditionFinish;
|
||||
}
|
||||
|
||||
public bool IsInternshiptypeAllowed(InternshipType internshipType)
|
||||
{
|
||||
return AllowedInternshipTypes.HasFlag(internshipType);
|
||||
}
|
||||
}
|
||||
}
|
@ -64,7 +64,7 @@ namespace InternshipSystem.Core
|
||||
|
||||
var internshipType = updateRegistration.Type ?? InternshipRegistration.Type;
|
||||
|
||||
if (!Edition.IsInternshiptypeAllowed(internshipType))
|
||||
if (!Edition.IsInternshipTypeAllowed(internshipType))
|
||||
{
|
||||
throw new ArgumentException("Internship type not allowed for this edition", nameof(updateRegistration.Type));
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ namespace InternshipSystem.Core
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Course { get; set; }
|
||||
public int? Semester { get; set; }
|
||||
|
||||
public static Student CreateStudent(long id, string firstName, string lastName, string email, in int albumNumber)
|
||||
{
|
||||
|
@ -9,10 +9,10 @@ namespace InternshipSystem.Repository {
|
||||
|
||||
public InternshipDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBulider = new DbContextOptionsBuilder<InternshipDbContext>();
|
||||
optionsBulider.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu");
|
||||
var optionsBuilder = new DbContextOptionsBuilder<InternshipDbContext>();
|
||||
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu");
|
||||
|
||||
return new InternshipDbContext(optionsBulider.Options);
|
||||
return new InternshipDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using InternshipSystem.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace InternshipSystem.Repository
|
||||
{
|
||||
public class InternshipPractiseSupervisorDbContext : InternshipDbContext
|
||||
{
|
||||
public InternshipPractiseSupervisorDbContext(DbContextOptions<InternshipDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user