From 66870cf7cd71d374be2a4ba42c140cb35cb3ce0d Mon Sep 17 00:00:00 2001 From: mborzyszkowski Date: Wed, 8 Jul 2020 21:25:52 +0200 Subject: [PATCH] IntershipController changes --- .docker/docker-compose.yaml | 4 +- .vscode/launch.json | 4 +- .vscode/tasks.json | 6 +- .../Controllers/InternshipController.cs | 87 ++++++++++++++++--- src/InternshipSystem.Api/Dockerfile | 9 +- .../InternshipSystem.Api.csproj | 1 + src/InternshipSystem.Api/Startup.cs | 4 + .../InternshipSystem.Repository.csproj | 2 +- 8 files changed, 96 insertions(+), 21 deletions(-) diff --git a/.docker/docker-compose.yaml b/.docker/docker-compose.yaml index 6d84647..d26c136 100644 --- a/.docker/docker-compose.yaml +++ b/.docker/docker-compose.yaml @@ -3,7 +3,9 @@ services: internship.api: image: internship.api:latest - build: ../src/Internship.Api + build: + context: ../src + dockerfile: InternshipSystem.Api/Dockerfile ports: - 8080:80 diff --git a/.vscode/launch.json b/.vscode/launch.json index 15cdbfd..4895807 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,9 +9,9 @@ "type": "coreclr", "request": "launch", "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": [], - "cwd": "${workspaceFolder}/src/Internship.Api", + "cwd": "${workspaceFolder}/src/InternshipSystem.Api", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 92fcc64..350922e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", + "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -19,7 +19,7 @@ "type": "process", "args": [ "publish", - "${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", + "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -32,7 +32,7 @@ "args": [ "watch", "run", - "${workspaceFolder}/src/Internship.Api/Internship.Api.csproj", + "${workspaceFolder}/src/InternshipSystem.Api/InternshipSystem.Api.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], diff --git a/src/InternshipSystem.Api/Controllers/InternshipController.cs b/src/InternshipSystem.Api/Controllers/InternshipController.cs index 78bf017..5b8a982 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipController.cs @@ -1,26 +1,91 @@ -using System; using Microsoft.AspNetCore.Mvc; 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] - //[Route("[internship]")] + [Route("Internship")] public class InternshipController : ControllerBase { + private readonly InternshipDbContext _context; - [HttpGet] - public IActionResult GetInternships() { - //TODO: parse params - //return JsonResult(""); - return Ok(); + public InternshipController(InternshipDbContext context) + { + this._context = context; } [HttpPost] - public IActionResult CreateInternship(Internship internship) { - //TODO: add internship - return Ok(); + public async Task> CreateInternship(Internship internship) + { + this._context.Internships.Add(internship); + await this._context.SaveChangesAsync(); + + return CreatedAtAction(nameof(GetInternship), new { id = internship.Id }, internship); } + [HttpGet] + public async Task>> GetAllInternships() => + await this._context.Internships.ToListAsync(); + [HttpGet("{id}")] + public async Task> 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 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 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(); + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Dockerfile b/src/InternshipSystem.Api/Dockerfile index 3361645..044160f 100644 --- a/src/InternshipSystem.Api/Dockerfile +++ b/src/InternshipSystem.Api/Dockerfile @@ -2,15 +2,18 @@ FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build-env WORKDIR /app # Copy csproj and restore as distinct layers -COPY *.csproj ./ -RUN dotnet restore +# TODO: TODO +# COPY */*.csproj ./ +# RUN ls +# RUN dotnet restore InternshipSystem.Api.csproj # Copy everything else and build COPY . ./ +WORKDIR /app/InternshipSystem.Api RUN dotnet publish -c Release -o out # Build runtime image FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine WORKDIR /app COPY --from=build-env /app/out . -ENTRYPOINT ["dotnet", "./Internship.Api.dll"] +ENTRYPOINT ["dotnet", "./InternshipSystem.Api.dll"] diff --git a/src/InternshipSystem.Api/InternshipSystem.Api.csproj b/src/InternshipSystem.Api/InternshipSystem.Api.csproj index f309fb8..51d591f 100644 --- a/src/InternshipSystem.Api/InternshipSystem.Api.csproj +++ b/src/InternshipSystem.Api/InternshipSystem.Api.csproj @@ -19,6 +19,7 @@ + diff --git a/src/InternshipSystem.Api/Startup.cs b/src/InternshipSystem.Api/Startup.cs index 59c50c1..4fe22ef 100644 --- a/src/InternshipSystem.Api/Startup.cs +++ b/src/InternshipSystem.Api/Startup.cs @@ -1,5 +1,7 @@ +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; @@ -18,6 +20,8 @@ namespace InternshipSystem.Api services .AddSwaggerGen(options => options.SwaggerDoc("v1", new OpenApiInfo {Title = "InternshipSystem Api - TEST", Version = "v1"})) + .AddDbContext(options => + options.UseNpgsql(Configuration.GetConnectionString("InternshipDbContext"))) .AddControllers() ; diff --git a/src/InternshipSystem.Repository/InternshipSystem.Repository.csproj b/src/InternshipSystem.Repository/InternshipSystem.Repository.csproj index 4b21438..7be0990 100644 --- a/src/InternshipSystem.Repository/InternshipSystem.Repository.csproj +++ b/src/InternshipSystem.Repository/InternshipSystem.Repository.csproj @@ -6,7 +6,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all