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 GetSalesperson([FromRoute] int id) { var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId== id); return Ok(salesPerson); } [HttpPost] public async Task PostSalesperson([FromBody] Salesperson salesPerson) { _context.Salesperson.Add(salesPerson); await _context.SaveChangesAsync(); return CreatedAtAction("getSalesPerson", new { id = salesPerson.SalespersonId}, salesPerson); } [HttpPut("{id}")] public async Task PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesPerson) { _context.Entry(salesPerson).State = EntityState.Modified; await _context.SaveChangesAsync(); return Ok(salesPerson); } [HttpDelete("{id}")] public async Task DeleteSalesperson([FromRoute] int id) { var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id); _context.Salesperson.Remove(salesPerson); await _context.SaveChangesAsync(); return Ok(salesPerson); } } }