109 lines
2.8 KiB
C#
109 lines
2.8 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 ThuisApi.Data;
|
|
using ThuisApi.Models;
|
|
|
|
namespace ThuisApi.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class CardController : ControllerBase
|
|
{
|
|
private readonly ThuisDbContext _context;
|
|
|
|
public CardController(ThuisDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Card
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Card>>> GetCards()
|
|
{
|
|
return await _context.Cards.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Card/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Card>> GetCard(int id)
|
|
{
|
|
var card = await _context.Cards.FindAsync(id);
|
|
|
|
if (card == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return card;
|
|
}
|
|
|
|
// PUT: api/Card/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutCard(int id, Card card)
|
|
{
|
|
if (id != card.CardId)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(card).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!CardExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Card
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<Card>> PostCard(Card card)
|
|
{
|
|
_context.Cards.Add(card);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetCard", new { id = card.CardId }, card);
|
|
}
|
|
|
|
// DELETE: api/Card/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteCard(int id)
|
|
{
|
|
var card = await _context.Cards.FindAsync(id);
|
|
if (card == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Cards.Remove(card);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
private bool CardExists(int id)
|
|
{
|
|
return _context.Cards.Any(e => e.CardId == id);
|
|
}
|
|
}
|
|
}
|