Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ActionResult<IEnumerable<Product>>> Get()
{

//var list = _db.ProductEntries.Select(o => o.Name).ToList();

//return Ok(list);

return await _db.ProductEntries.ToListAsync();
}




[HttpGet("{id}")]
public async Task<IActionResult> GetEntry([FromRoute] int id)
{
var entry = _db.ProductEntries.Find(id);
if (entry == null)
{
return NotFound();
}
await _db.SaveChangesAsync();
return Ok(entry);
}


[HttpPost]
public ActionResult<Product> 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<IActionResult> 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<ActionResult<IEnumerable<Product>>> Get(int id)
{
var list = _db.ProductEntries.Where(o => o.Id == id);

return Ok(list.ToList());
}


[HttpGet("Type")]
public async Task<ActionResult<IEnumerable<Product>>> Get(string type)
{
var list = _db.ProductEntries.Where(o => o.Type == type);

return Ok(list.ToList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_#;"
}
}