Projekt-Vorlage mit Tasks Controller

This commit is contained in:
jfhr
2020-05-26 14:50:26 +02:00
parent 39aa048e87
commit 640dda2aa3
10 changed files with 606 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ScrumTaskboard;
namespace ScrumTaskboard.Controllers
{
[Route("[controller]")]
[ApiController]
public class TasksController : ControllerBase
{
private readonly TaskContext _context;
public TasksController(TaskContext context)
{
_context = context;
}
// GET: api/Tasks
[HttpGet]
public async Task<ActionResult<IEnumerable<ScrumTask>>> GetTasks()
{
return await _context.Tasks.ToListAsync();
}
// GET: api/Tasks/5
[HttpGet("{id}")]
public async Task<ActionResult<ScrumTask>> GetTask(int id)
{
var task = await _context.Tasks.FindAsync(id);
if (task == null)
{
return NotFound();
}
return task;
}
// PUT: api/Tasks/5
[HttpPut("{id}")]
public async Task<IActionResult> PutTask(int id, ScrumTask task)
{
// Die ID des Tasks darf nicht geändert werden
if (id != task.Id)
{
return BadRequest();
}
// Speichere den geänderten Task im Context
_context.Entry(task).State = EntityState.Modified;
try
{
// Übernehme die Änderungen in die Datenbank
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
// Wenn der Task nicht existiert, gib Statuscode 404 zurück
if (!TaskExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Tasks
[HttpPost]
public async Task<ActionResult<ScrumTask>> PostTask(ScrumTask task)
{
_context.Tasks.Add(task);
await _context.SaveChangesAsync();
// Der neue Task wurde erstellt und kann über die GetTask Methode abgerufen werden.
return CreatedAtAction("GetTask", new { id = task.Id }, task);
}
// DELETE: api/Tasks/5
[HttpDelete("{id}")]
public async Task<ActionResult<ScrumTask>> DeleteTask(int id)
{
var scrumTask = await _context.Tasks.FindAsync(id);
if (scrumTask == null)
{
return NotFound();
}
// Entferne den Task aus dem Context
_context.Tasks.Remove(scrumTask);
// Speichere die Änderungen in der Datenbank
await _context.SaveChangesAsync();
return scrumTask;
}
/// <summary>
/// Prüft, ob ein Task mit der angegebenen ID bereits existiert.
/// </summary>
private bool TaskExists(int id)
{
return _context.Tasks.Any(e => e.Id == id);
}
}
}

26
ScrumTaskboard/Program.cs Normal file
View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ScrumTaskboard
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

View File

@@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51447",
"sslPort": 44334
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ScrumTaskboard": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "tasks",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
</ItemGroup>
</Project>

62
ScrumTaskboard/Startup.cs Normal file
View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ScrumTaskboard
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped(serviceProvider => new TaskContext(
new DbContextOptionsBuilder<TaskContext>()
.UseNpgsql("Host=nig.gl; Port=8543; Username=scrum; Database=taskboard; Password=c6gXud7YvBWp2sgxSgy4wRN")
.Options));
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<TaskContext>();
context.Database.EnsureCreated();
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

View File

@@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore;
using System;
namespace ScrumTaskboard
{
public class TaskContext : DbContext
{
public DbSet<ScrumTask> Tasks { get; set; }
public TaskContext() { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=nig.gl; Port=8543; Username=scrum; Database=taskboard; Password=c6gXud7YvBWp2sgxSgy4wRN");
}
public TaskContext(DbContextOptions<TaskContext> options) : base(options) { }
}
public class ScrumTask
{
public int Id { get; set; }
public string Titel { get; set; }
public string Inhalt { get; set; }
public int Status { get; set; }
public int Kategorie { get; set; }
public int Bearbeiter { get; set; }
public int ZugeordneterSprint { get; set; }
public int Projekt { get; set; }
public int Userstory { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}