IntershipController changes

This commit is contained in:
mborzyszkowski 2020-07-08 21:25:52 +02:00
parent fd5d6b0f48
commit 66870cf7cd
8 changed files with 96 additions and 21 deletions

View File

@ -3,7 +3,9 @@ services:
internship.api: internship.api:
image: internship.api:latest image: internship.api:latest
build: ../src/Internship.Api build:
context: ../src
dockerfile: InternshipSystem.Api/Dockerfile
ports: ports:
- 8080:80 - 8080:80

4
.vscode/launch.json vendored
View File

@ -9,9 +9,9 @@
"type": "coreclr", "type": "coreclr",
"request": "launch", "request": "launch",
"preLaunchTask": "build", "preLaunchTask": "build",
"program": "${workspaceFolder}/src/Internship.Api/bin/Debug/netcoreapp3.1/Internship.Api.dll", "program": "${workspaceFolder}/src/InternshipSystem.Api/bin/Debug/netcoreapp3.1/InternshipSystem.Api.dll",
"args": [], "args": [],
"cwd": "${workspaceFolder}/src/Internship.Api", "cwd": "${workspaceFolder}/src/InternshipSystem.Api",
"stopAtEntry": false, "stopAtEntry": false,
"serverReadyAction": { "serverReadyAction": {
"action": "openExternally", "action": "openExternally",

6
.vscode/tasks.json vendored
View File

@ -7,7 +7,7 @@
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
@ -19,7 +19,7 @@
"type": "process", "type": "process",
"args": [ "args": [
"publish", "publish",
"${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
@ -32,7 +32,7 @@
"args": [ "args": [
"watch", "watch",
"run", "run",
"${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],

View File

@ -1,26 +1,91 @@
using System;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using InternshipSystem.Core; using InternshipSystem.Core;
using System.Threading.Tasks;
using System.Collections.Generic;
using InternshipSystem.Repository;
using Microsoft.EntityFrameworkCore;
namespace InternshipSystem.Api.Controllers { namespace InternshipSystem.Api.Controllers
{
[ApiController] [ApiController]
//[Route("[internship]")] [Route("Internship")]
public class InternshipController : ControllerBase { public class InternshipController : ControllerBase {
private readonly InternshipDbContext _context;
[HttpGet] public InternshipController(InternshipDbContext context)
public IActionResult GetInternships() { {
//TODO: parse params this._context = context;
//return JsonResult("");
return Ok();
} }
[HttpPost] [HttpPost]
public IActionResult CreateInternship(Internship internship) { public async Task<ActionResult<Internship>> CreateInternship(Internship internship)
//TODO: add internship {
return Ok(); this._context.Internships.Add(internship);
await this._context.SaveChangesAsync();
return CreatedAtAction(nameof(GetInternship), new { id = internship.Id }, internship);
} }
[HttpGet]
public async Task<ActionResult<IEnumerable<Internship>>> GetAllInternships() =>
await this._context.Internships.ToListAsync();
[HttpGet("{id}")]
public async Task<ActionResult<Internship>> GetInternship(int id)
{
var internship = await this._context.Internships.FindAsync(id);
if(internship == null) {
return NotFound();
}
return internship;
}
//TODO: Refactor
[HttpPut("{id}")]
public async Task<IActionResult> UpdateInternship(int id, Internship internship)
{
if (id != internship.Id)
{
return BadRequest();
}
var internshipItem = await this._context.Internships.FindAsync(id);
if (internshipItem == null)
{
return NotFound();
}
try
{
await this._context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) when (!InternshipsExists(id))
{
return NotFound();
}
return NoContent();
}
private bool InternshipsExists(int id) =>
this._context.Internships.Find(id) != null;
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteInternship(int id) {
var internshipItem = await this._context.Internships.FindAsync(id);
if (internshipItem == null)
{
return NotFound();
}
this._context.Internships.Remove(internshipItem);
await _context.SaveChangesAsync();
return NoContent();
}
} }
} }

View File

@ -2,15 +2,18 @@ FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build-env
WORKDIR /app WORKDIR /app
# Copy csproj and restore as distinct layers # Copy csproj and restore as distinct layers
COPY *.csproj ./ # TODO: TODO
RUN dotnet restore # COPY */*.csproj ./
# RUN ls
# RUN dotnet restore InternshipSystem.Api.csproj
# Copy everything else and build # Copy everything else and build
COPY . ./ COPY . ./
WORKDIR /app/InternshipSystem.Api
RUN dotnet publish -c Release -o out RUN dotnet publish -c Release -o out
# Build runtime image # Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
WORKDIR /app WORKDIR /app
COPY --from=build-env /app/out . COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "./Internship.Api.dll"] ENTRYPOINT ["dotnet", "./InternshipSystem.Api.dll"]

View File

@ -19,6 +19,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\InternshipSystem.Core\InternshipSystem.Core.csproj" /> <ProjectReference Include="..\InternshipSystem.Core\InternshipSystem.Core.csproj" />
<ProjectReference Include="..\InternshipSystem.Repository\InternshipSystem.Repository.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,7 @@
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;
@ -18,6 +20,8 @@ namespace InternshipSystem.Api
services services
.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"}))
.AddDbContext<InternshipDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("InternshipDbContext")))
.AddControllers() .AddControllers()
; ;

View File

@ -6,7 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="1.1.0" /> <PackageReference Include="EFCore.NamingConventions" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>