IntershipController changes #15

Closed
Zonar wants to merge 2 commits from AddMentor into master
8 changed files with 95 additions and 22 deletions

View File

@ -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

4
.vscode/launch.json vendored
View File

@ -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",

6
.vscode/tasks.json vendored
View File

@ -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"
],

View File

@ -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<ActionResult<Internship>> 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<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,16 @@ 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
COPY */*.csproj ./
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"]
COPY --from=build-env /app/InternshipSystem.Api/out .
ENTRYPOINT ["dotnet", "./InternshipSystem.Api.dll"]

View File

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

View File

@ -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<InternshipDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("InternshipDbContext")))
.AddControllers()
;

View File

@ -6,7 +6,7 @@
<ItemGroup>
<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>
<PrivateAssets>all</PrivateAssets>
</PackageReference>