Lagt in färdigfixade controllers

This commit is contained in:
2019-12-17 21:10:28 +01:00
parent 766869a6d6
commit 7df0f07d15
5 changed files with 382 additions and 98 deletions

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using H_PLUS_Sports.Models;
namespace HPlusSportsAPI.Controllers
{
@ -20,42 +17,105 @@ namespace HPlusSportsAPI.Controllers
_context = context;
}
private bool SalespersonExists(int id)
{
return _context.Salesperson.Any(e => e.SalespersonId == id);
}
[HttpGet]
[Produces(typeof(DbSet<Salesperson>))]
public IActionResult GetSalesperson()
{
return new ObjectResult(_context.Salesperson);
}
[HttpGet("{id}", Name = "GetSalesPerson")]
[HttpGet("{id}")]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> GetSalesperson([FromRoute] int id)
{
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId== id);
return Ok(salesPerson);
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
[HttpPost]
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesPerson)
{
_context.Salesperson.Add(salesPerson);
await _context.SaveChangesAsync();
return CreatedAtAction("getSalesPerson", new { id = salesPerson.SalespersonId}, salesPerson);
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
if (salesperson == null)
{
return NotFound();
}
return Ok(salesperson);
}
[HttpPut("{id}")]
public async Task<IActionResult> PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesPerson)
[Produces(typeof(Salesperson))]
public async Task<IActionResult> PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesperson)
{
_context.Entry(salesPerson).State = EntityState.Modified;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != salesperson.SalespersonId)
{
return BadRequest();
}
_context.Entry(salesperson).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
return Ok(salesperson);
}
catch (DbUpdateConcurrencyException)
{
if (!SalespersonExists(id))
{
return NotFound();
}
else
{
throw;
}
}
}
[HttpPost]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesperson)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Salesperson.Add(salesperson);
await _context.SaveChangesAsync();
return Ok(salesPerson);
return CreatedAtAction("GetSalesperson", new { id = salesperson.SalespersonId }, salesperson);
}
[HttpDelete("{id}")]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> DeleteSalesperson([FromRoute] int id)
{
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
_context.Salesperson.Remove(salesPerson);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
if (salesperson == null)
{
return NotFound();
}
_context.Salesperson.Remove(salesperson);
await _context.SaveChangesAsync();
return Ok(salesPerson);
return Ok(salesperson);
}
}
}