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 Freiberufler.Data;
using Freiberufler.Models;
namespace Freiberufler.Controllers.Api
{
[Produces("application/json")]
[Route("api/Projects")]
public class ProjectsController : Controller
{
private readonly ApplicationDbContext _context;
public
ProjectsController(ApplicationDbContext context)
{
_context = context;
}
// GET:
api/Projects
[HttpGet]
public IEnumerable<Project>
Gettbl_Projects()
{
return _context.tbl_Projects;
}
// GET:
api/Projects/1
[HttpGet("{id}")]
public async Task<IActionResult>
GetProject([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var project = await
_context.tbl_Projects.SingleOrDefaultAsync(m => m.ID_Project == id);
if (project == null)
{
return NotFound();
}
return Ok(project);
}
// PUT:
api/Projects/5
[HttpPut("{id}")]
public async Task<IActionResult>
PutProject([FromRoute] int id,
[FromBody] Project project)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != project.ID_Project)
{
return BadRequest();
}
_context.Entry(project).State =
EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST:
api/Projects
[HttpPost]
public async Task<IActionResult>
PostProject([FromBody] Project project)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.tbl_Projects.Add(project);
await _context.SaveChangesAsync();
return CreatedAtAction("GetProject", new { id = project.ID_Project }, project);
}
// DELETE:
api/Projects/5
[HttpDelete("{id}")]
public async Task<IActionResult>
DeleteProject([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var project = await
_context.tbl_Projects.SingleOrDefaultAsync(m => m.ID_Project == id);
if (project == null)
{
return NotFound();
}
_context.tbl_Projects.Remove(project);
await _context.SaveChangesAsync();
return Ok(project);
}
private bool ProjectExists(int id)
{
return _context.tbl_Projects.Any(e =>
e.ID_Project == id);
}
}
}
|