Sample controller, change internship type to an entity, setup db properly

This commit is contained in:
MaxchilKH 2020-07-11 12:31:14 +02:00
parent d2c4bca9d5
commit 63b2f8ceef
10 changed files with 94 additions and 17 deletions

View File

@ -3,6 +3,9 @@ services:
internship.api:
image: internship.api:latest
environment:
CONNECTIONSTRINGS__INTERNSHIPDATABASE: "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu"
ASPNETCORE_ENVIRONMENT: Development
ports:
- 8080:80

8
.vscode/tasks.json vendored
View File

@ -10,10 +10,6 @@
"context": "src"
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "down: api",
@ -40,6 +36,10 @@
"down: api",
"build: api"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using InternshipSystem.Api.Queries;
using InternshipSystem.Core;
using InternshipSystem.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace InternshipSystem.Api.Controllers
{
[ApiController]
[Route("companies")]
public class CompaniesController : ControllerBase
{
public CompaniesController(InternshipDbContext context)
{
Context = context;
}
private InternshipDbContext Context { get; }
/// <summary>
/// Get companies matching provided paginated query
/// </summary>
/// <param name="query">Paginated query description</param>
/// <returns>Part of companies collection</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IReadOnlyCollection<Company>> SearchByNameAsync([FromQuery]SearchQuery query, CancellationToken cancellationToken) =>
await Context.Companies
.Where(c => c.Name.Contains(query.Query ?? ""))
.OrderBy(o => o.Name)
.Skip(query.Page * query.PerPage)
.Take(query.PerPage)
.ToListAsync(cancellationToken);
}
}

View File

@ -2,7 +2,8 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<!-- <GenerateDocumentationFile>true</GenerateDocumentationFile>-->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -0,0 +1,20 @@
namespace InternshipSystem.Api.Queries
{
public class SearchQuery
{
/// <summary>
/// Value against which collection will be queried
/// </summary>
public string Query { get; set; }
/// <summary>
/// Which part of the collections to retrieve
/// </summary>
public int Page { get; set; }
/// <summary>
/// Size of the retrieved part
/// </summary>
public int PerPage { get; set; }
}
}

View File

@ -1,5 +1,10 @@
using System;
using System.IO;
using System.Reflection;
using InternshipSystem.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -16,8 +21,14 @@ namespace InternshipSystem.Api
public void ConfigureServices(IServiceCollection services) =>
services
.AddDbContext<InternshipDbContext>(o => o.UseNpgsql(Configuration.GetConnectionString("InternshipDatabase")))
.AddSwaggerGen(options =>
options.SwaggerDoc("v1", new OpenApiInfo {Title = "InternshipSystem Api - TEST", Version = "v1"}))
{
options.SwaggerDoc("v1", new OpenApiInfo {Title = "InternshipSystem Api - TEST", Version = "v1"});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
})
.AddControllers()
;

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>

View File

@ -1,15 +1,9 @@
namespace InternshipSystem.Core
{
public enum InternshipType
public class InternshipType
{
Other,
FreeInternship,
GraduateInternship,
FreeApprenticeship,
PaidApprenticeship,
EmploymentContract,
MandateContract,
ContractWork,
ForeignInternship
public int Id { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
}

View File

@ -10,9 +10,15 @@ namespace InternshipSystem.Repository
public DbSet<Edition> Editions { get; set; }
public DbSet<Report> Reports { get; set; }
public DbSet<Intern> Interns { get; set; }
public DbSet<InternshipType> InternshipTypes { get; set; }
public InternshipDbContext(DbContextOptions<InternshipDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu")
.UseSnakeCaseNamingConvention();
protected override void OnModelCreating(ModelBuilder modelBuilder) {

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>