diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs new file mode 100644 index 0000000..06d06a1 --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Know_Your_Nation_Speedy.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace Know_Your_Nation_Speedy.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ProductController : ControllerBase + { + private readonly MyDbContext _db; + public ProductController(MyDbContext context) + { + _db = context; + } + + [HttpGet("products")] + public async Task>> Get() + { + + //var list = _db.ProductEntries.Select(o => o.Name).ToList(); + + //return Ok(list); + + return await _db.ProductEntries.ToListAsync(); + } + + + + + [HttpGet("{id}")] + public async Task GetEntry([FromRoute] int id) + { + var entry = _db.ProductEntries.Find(id); + if (entry == null) + { + return NotFound(); + } + await _db.SaveChangesAsync(); + return Ok(entry); + } + + + [HttpPost] + public ActionResult Post([FromBody] Product product) + { + if (product.Name == null && product.SizeOption == null && product.Type == null && product.ColourOption == null) + { + return BadRequest(new { Error = "product not fully loaded" }); + } + else + { + _db.ProductEntries.AddAsync(product); + _db.SaveChangesAsync(); + return Ok(product); + } + } + + + + + [HttpDelete("{id}")] + public async Task DeleteEntry([FromRoute]int id) + { + var entry = await _db.ProductEntries.SingleOrDefaultAsync(o => o.Id == id); + if (entry == null) + { + return NotFound(); + } + _db.ProductEntries.Remove(entry); + await _db.SaveChangesAsync(); + return Ok(entry); + } + + [HttpGet("getproduct")] + public async Task>> Get(int id) + { + var list = _db.ProductEntries.Where(o => o.Id == id); + + return Ok(list.ToList()); + } + + + [HttpGet("Type")] + public async Task>> Get(string type) + { + var list = _db.ProductEntries.Where(o => o.Type == type); + + return Ok(list.ToList()); + } + } +} \ No newline at end of file diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj index fe51a5a..a8e475a 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy.csproj @@ -10,6 +10,7 @@ + diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json index c800919..43ec188 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/appsettings.json @@ -5,6 +5,6 @@ } }, "ConnectionStrings": { - "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=3bHNuE8&rvG+99U2;" + "connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=P,A+m8JDHx{!L4_#;" } } \ No newline at end of file