109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
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 ProjectApi;
|
|
using ProjectApi.Models;
|
|
|
|
namespace ProjectApi.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class WorkItemController : ControllerBase
|
|
{
|
|
private readonly WorkItemContext _context;
|
|
|
|
public WorkItemController(WorkItemContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/WorkItem
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<WorkItem>>> GetWorkItems()
|
|
{
|
|
return await _context.WorkItems.ToListAsync();
|
|
}
|
|
|
|
// GET: api/WorkItem/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<WorkItem>> GetWorkItem(int id)
|
|
{
|
|
var workItem = await _context.WorkItems.FindAsync(id);
|
|
|
|
if (workItem == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return workItem;
|
|
}
|
|
|
|
// PUT: api/WorkItem/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutWorkItem(int id, WorkItem workItem)
|
|
{
|
|
if (id != workItem.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(workItem).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!WorkItemExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/WorkItem
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<WorkItem>> PostWorkItem(WorkItem workItem)
|
|
{
|
|
_context.WorkItems.Add(workItem);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetWorkItem", new { id = workItem.Id }, workItem);
|
|
}
|
|
|
|
// DELETE: api/WorkItem/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteWorkItem(int id)
|
|
{
|
|
var workItem = await _context.WorkItems.FindAsync(id);
|
|
if (workItem == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.WorkItems.Remove(workItem);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
private bool WorkItemExists(int id)
|
|
{
|
|
return _context.WorkItems.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|