From e324c9f0041ab0a7922d5b1bea2325ca11c11eb0 Mon Sep 17 00:00:00 2001 From: Mpilo Mshengu Date: Fri, 15 Feb 2019 08:46:21 +0200 Subject: [PATCH 1/5] ProductsController --- .../Controllers/ProductsController.cs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs diff --git a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs new file mode 100644 index 0000000..32d1224 --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs @@ -0,0 +1,62 @@ +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 ProductsController : ControllerBase + { + private readonly MyDbContext _db; + public ProductsController(MyDbContext context) + { + _db = context; + } + + // GET api/values + [HttpGet] + public async Task>> Get() + { + return await _db.ProductEntries.ToListAsync(); + } + + [HttpGet("{id}")] + public async Task GetEntry([FromRoute] int id) + { + var entry = await _db.ProductEntries.SingleOrDefaultAsync(o => o.Id == id); + if (entry == null) + { + return NotFound(); + } + await _db.SaveChangesAsync(); + return Ok(entry); + } + + + [HttpPost] + public async Task Post([FromBody] Product product) + { + await _db.ProductEntries.AddAsync(product); + await _db.SaveChangesAsync(); + } + + + [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); + } + } +} \ No newline at end of file From d84b3e57157eae0d0d7fa8d98b94e75326f79497 Mon Sep 17 00:00:00 2001 From: Mpilo Mshengu Date: Fri, 15 Feb 2019 09:50:21 +0200 Subject: [PATCH 2/5] changed the name of ProductsController to ProductController --- .../Controllers/ProductController.cs | 61 +++++++++++++++++++ .../Know-Your-Nation-Speedy/appsettings.json | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs 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..eb282a8 --- /dev/null +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs @@ -0,0 +1,61 @@ +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] + public async Task>> Get() + { + return await _db.ProductEntries.ToListAsync(); + } + + [HttpGet("{id}")] + public async Task GetEntry([FromRoute] int id) + { + var entry = await _db.ProductEntries.SingleOrDefaultAsync(o => o.Id == id); + if (entry == null) + { + return NotFound(); + } + await _db.SaveChangesAsync(); + return Ok(entry); + } + + + [HttpPost] + public async Task Post([FromBody] Product product) + { + await _db.ProductEntries.AddAsync(product); + await _db.SaveChangesAsync(); + } + + + [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); + } + } +} \ No newline at end of file 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 From 5af94a195be9737f17550b86f901b50b2d4c8185 Mon Sep 17 00:00:00 2001 From: Mpilo Mshengu Date: Fri, 15 Feb 2019 10:51:27 +0200 Subject: [PATCH 3/5] under GetEntry I changed SingleOrDefaultAsync() to Find() --- .../Know-Your-Nation-Speedy/Controllers/ProductController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index eb282a8..aff0a5d 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs @@ -27,7 +27,7 @@ public async Task>> Get() [HttpGet("{id}")] public async Task GetEntry([FromRoute] int id) { - var entry = await _db.ProductEntries.SingleOrDefaultAsync(o => o.Id == id); + var entry = _db.ProductEntries.Find(id); if (entry == null) { return NotFound(); From d149992f08dceb23e7b713de73203b3b75baaead Mon Sep 17 00:00:00 2001 From: Mpilo Mshengu Date: Fri, 15 Feb 2019 11:14:40 +0200 Subject: [PATCH 4/5] error handling on Post function and it return BadRequest if there is null value. --- .../Controllers/ProductController.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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 index aff0a5d..3751583 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs @@ -38,10 +38,18 @@ public async Task GetEntry([FromRoute] int id) [HttpPost] - public async Task Post([FromBody] Product product) + public ActionResult Post([FromBody] Product product) { - await _db.ProductEntries.AddAsync(product); - await _db.SaveChangesAsync(); + 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); + } } From 31fe73c0fe8eca9ec032a568ce1661fbbec2fdef Mon Sep 17 00:00:00 2001 From: Mpilo Mshengu Date: Tue, 19 Feb 2019 10:54:15 +0200 Subject: [PATCH 5/5] ProductController --- .../Controllers/ProductController.cs | 31 +++++++++- .../Controllers/ProductsController.cs | 62 ------------------- .../Know-Your-Nation-Speedy.csproj | 1 + 3 files changed, 30 insertions(+), 64 deletions(-) delete mode 100644 Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs 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 index 3751583..06d06a1 100644 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs +++ b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductController.cs @@ -18,12 +18,20 @@ public ProductController(MyDbContext context) _db = context; } - [HttpGet] + [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) { @@ -53,6 +61,8 @@ public ActionResult Post([FromBody] Product product) } + + [HttpDelete("{id}")] public async Task DeleteEntry([FromRoute]int id) { @@ -65,5 +75,22 @@ public async Task DeleteEntry([FromRoute]int id) 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/Controllers/ProductsController.cs b/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs deleted file mode 100644 index 32d1224..0000000 --- a/Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/Controllers/ProductsController.cs +++ /dev/null @@ -1,62 +0,0 @@ -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 ProductsController : ControllerBase - { - private readonly MyDbContext _db; - public ProductsController(MyDbContext context) - { - _db = context; - } - - // GET api/values - [HttpGet] - public async Task>> Get() - { - return await _db.ProductEntries.ToListAsync(); - } - - [HttpGet("{id}")] - public async Task GetEntry([FromRoute] int id) - { - var entry = await _db.ProductEntries.SingleOrDefaultAsync(o => o.Id == id); - if (entry == null) - { - return NotFound(); - } - await _db.SaveChangesAsync(); - return Ok(entry); - } - - - [HttpPost] - public async Task Post([FromBody] Product product) - { - await _db.ProductEntries.AddAsync(product); - await _db.SaveChangesAsync(); - } - - - [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); - } - } -} \ 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 @@ +