61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using H_PLUS_Sports.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace HPlusSportsAPI.Controllers
|
|
{
|
|
[Produces("application/json")]
|
|
[Route("api/Salespersons")]
|
|
public class SalespersonsController : Controller
|
|
{
|
|
private readonly H_Plus_SportsContext _context;
|
|
|
|
public SalespersonsController(H_Plus_SportsContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult GetSalesperson()
|
|
{
|
|
return new ObjectResult(_context.Salesperson);
|
|
}
|
|
|
|
[HttpGet("{id}", Name = "GetSalesPerson")]
|
|
public async Task<IActionResult> GetSalesperson([FromRoute] int id)
|
|
{
|
|
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId== id);
|
|
return Ok(salesPerson);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesPerson)
|
|
{
|
|
_context.Salesperson.Add(salesPerson);
|
|
await _context.SaveChangesAsync();
|
|
return CreatedAtAction("getSalesPerson", new { id = salesPerson.SalespersonId}, salesPerson);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesPerson)
|
|
{
|
|
_context.Entry(salesPerson).State = EntityState.Modified;
|
|
await _context.SaveChangesAsync();
|
|
return Ok(salesPerson);
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteSalesperson([FromRoute] int id)
|
|
{
|
|
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
|
|
_context.Salesperson.Remove(salesPerson);
|
|
await _context.SaveChangesAsync();
|
|
return Ok(salesPerson);
|
|
}
|
|
}
|
|
} |