Sample controller, change internship type to an entity, setup db properly
This commit is contained in:
parent
d2c4bca9d5
commit
63b2f8ceef
@ -3,6 +3,9 @@ services:
|
|||||||
|
|
||||||
internship.api:
|
internship.api:
|
||||||
image: internship.api:latest
|
image: internship.api:latest
|
||||||
|
environment:
|
||||||
|
CONNECTIONSTRINGS__INTERNSHIPDATABASE: "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu"
|
||||||
|
ASPNETCORE_ENVIRONMENT: Development
|
||||||
ports:
|
ports:
|
||||||
- 8080:80
|
- 8080:80
|
||||||
|
|
||||||
|
8
.vscode/tasks.json
vendored
8
.vscode/tasks.json
vendored
@ -10,10 +10,6 @@
|
|||||||
"context": "src"
|
"context": "src"
|
||||||
},
|
},
|
||||||
"problemMatcher": [],
|
"problemMatcher": [],
|
||||||
"group": {
|
|
||||||
"kind": "build",
|
|
||||||
"isDefault": true
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "down: api",
|
"label": "down: api",
|
||||||
@ -40,6 +36,10 @@
|
|||||||
"down: api",
|
"down: api",
|
||||||
"build: api"
|
"build: api"
|
||||||
],
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
"problemMatcher": []
|
"problemMatcher": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
40
src/InternshipSystem.Api/Controllers/CompaniesController.cs
Normal file
40
src/InternshipSystem.Api/Controllers/CompaniesController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<!-- <GenerateDocumentationFile>true</GenerateDocumentationFile>-->
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
20
src/InternshipSystem.Api/Queries/SearchQuery.cs
Normal file
20
src/InternshipSystem.Api/Queries/SearchQuery.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using InternshipSystem.Repository;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@ -16,8 +21,14 @@ namespace InternshipSystem.Api
|
|||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services) =>
|
public void ConfigureServices(IServiceCollection services) =>
|
||||||
services
|
services
|
||||||
|
.AddDbContext<InternshipDbContext>(o => o.UseNpgsql(Configuration.GetConnectionString("InternshipDatabase")))
|
||||||
.AddSwaggerGen(options =>
|
.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()
|
.AddControllers()
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,15 +1,9 @@
|
|||||||
namespace InternshipSystem.Core
|
namespace InternshipSystem.Core
|
||||||
{
|
{
|
||||||
public enum InternshipType
|
public class InternshipType
|
||||||
{
|
{
|
||||||
Other,
|
public int Id { get; set; }
|
||||||
FreeInternship,
|
public string Type { get; set; }
|
||||||
GraduateInternship,
|
public string Description { get; set; }
|
||||||
FreeApprenticeship,
|
|
||||||
PaidApprenticeship,
|
|
||||||
EmploymentContract,
|
|
||||||
MandateContract,
|
|
||||||
ContractWork,
|
|
||||||
ForeignInternship
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,9 +10,15 @@ namespace InternshipSystem.Repository
|
|||||||
public DbSet<Edition> Editions { get; set; }
|
public DbSet<Edition> Editions { get; set; }
|
||||||
public DbSet<Report> Reports { get; set; }
|
public DbSet<Report> Reports { get; set; }
|
||||||
public DbSet<Intern> Interns { 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) =>
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
|
||||||
optionsBuilder
|
optionsBuilder
|
||||||
.UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu")
|
|
||||||
.UseSnakeCaseNamingConvention();
|
.UseSnakeCaseNamingConvention();
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user