-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bryce.coon
authored and
bryce.coon
committed
Feb 29, 2024
1 parent
64c7f27
commit 8b67752
Showing
4 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
using KakeysBakery.Tests; | ||
using KakeysBakeryClassLib.Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http.Json; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace KakeysBakery.Tests; | ||
|
||
public class ProductTests : IClassFixture<BakeryFactory> | ||
{ | ||
public HttpClient client { get; set; } | ||
public ProductTests(BakeryFactory Factory) | ||
{ | ||
client = Factory.CreateDefaultClient(); | ||
} | ||
|
||
[Fact] | ||
public void CanPassATest() | ||
{ | ||
Assert.Equal(1, 1); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_ProductList() | ||
{ | ||
// ARRANGE | ||
Basegood basegood = new Basegood(); | ||
basegood.Id = 1; | ||
|
||
Product testProduct = new() | ||
{ | ||
Id = 245, | ||
Basegoodid = 1, | ||
Description = "Test", | ||
Productname = "TestName", | ||
}; | ||
|
||
await client.PostAsJsonAsync("api/basegood/add", basegood); | ||
await client.PostAsJsonAsync("api/Product/add", testProduct); | ||
|
||
// ACT | ||
List<Product>? result = await client.GetFromJsonAsync<List<Product>>("api/Product/getall"); | ||
|
||
// ASSERT | ||
Assert.NotNull(result); | ||
Assert.NotEmpty(result); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_Product_ById() | ||
{ | ||
// ARRANGE | ||
Basegood basegood = new Basegood(); | ||
basegood.Id = 1; | ||
Product testProduct = new() | ||
{ | ||
Id = 245, | ||
Basegoodid = 1, | ||
Description = "Test", | ||
Productname = "TestName", | ||
}; | ||
await client.PostAsJsonAsync("api/basegood/add", basegood); | ||
await client.PostAsJsonAsync("api/Product/add", testProduct); | ||
|
||
// ACT | ||
Product? result = await client.GetFromJsonAsync<Product>($"api/Product/get/{testProduct.Id}"); | ||
|
||
// ASSERT | ||
Assert.NotNull(result); | ||
|
||
Assert.Equal(testProduct.Description, result.Description); | ||
Assert.Equal(testProduct.Id, result.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task Create_Product() | ||
{ | ||
// ARRANGE | ||
Basegood basegood = new Basegood(); | ||
basegood.Id = 1; | ||
Product testProduct = new() | ||
{ | ||
Id = 245, | ||
Basegoodid = 1, | ||
Description = "Test", | ||
Productname = "TestName", | ||
}; | ||
|
||
// ACT | ||
await client.PostAsJsonAsync("api/basegood/add", basegood); | ||
await client.PostAsJsonAsync("api/Product/add", testProduct); | ||
Product? result = await client.GetFromJsonAsync<Product>($"api/Product/get/{testProduct.Id}"); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
|
||
Assert.Equal(testProduct.Description, result.Description); | ||
Assert.Equal(testProduct.Id, result.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task Edit_Product() | ||
{ | ||
// ARRANGE | ||
Basegood basegood = new Basegood(); | ||
basegood.Id = 1; | ||
Product testProduct = new() | ||
{ | ||
Id = 245, | ||
Basegoodid = 1, | ||
Description = "Test", | ||
Productname = "TestName", | ||
}; | ||
await client.PostAsJsonAsync("api/basegood/add", basegood); | ||
await client.PostAsJsonAsync("api/Product/add", testProduct); | ||
|
||
// ACT | ||
testProduct.Description = "some new description"; | ||
await client.PatchAsJsonAsync("api/Product/update", testProduct); | ||
|
||
Product? result = await client.GetFromJsonAsync<Product>($"api/Product/get/{testProduct.Id}"); | ||
|
||
|
||
// ASSERT | ||
Assert.NotNull(result); | ||
|
||
Assert.Equal(testProduct.Description, result.Description); | ||
Assert.Equal(testProduct.Id, result.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task Delete_Product() | ||
{ | ||
// ARRANGE | ||
Basegood basegood = new Basegood(); | ||
basegood.Id = 1; | ||
Product testProduct = new() | ||
{ | ||
Id = 245, | ||
Basegoodid = 1, | ||
Description = "Test", | ||
Productname = "TestName", | ||
}; | ||
await client.PostAsJsonAsync("api/basegood/add", basegood); | ||
await client.PostAsJsonAsync("api/Product/add", testProduct); | ||
Product temp = await client.GetFromJsonAsync<Product>($"api/Product/get/{testProduct.Id}"); | ||
// Assert | ||
Assert.NotNull(temp); | ||
// ACT | ||
await client.DeleteAsync($"api/Product/delete/{testProduct.Id}"); | ||
|
||
// ASSERT | ||
await Assert.ThrowsAsync<HttpRequestException>(async () => | ||
{ | ||
await client.GetFromJsonAsync<Addon>($"api/Product/get/{testProduct.Id}"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using KakeysBakery.Services; | ||
using KakeysBakeryClassLib.Services.Interfaces; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace KakeysBakery.Controllers; | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class ProductController : ControllerBase | ||
{ | ||
private readonly IProductService ProductService; | ||
public ProductController(IProductService service) | ||
{ | ||
ProductService = service; | ||
} | ||
|
||
[HttpGet("getall")] | ||
public async Task<List<Product>> GetProductsAsync() | ||
{ | ||
return await ProductService.GetProductListAsync(); | ||
} | ||
|
||
[HttpGet("get/{id}")] | ||
public async Task<IActionResult> GetProductAsync(int id) | ||
{ | ||
var prod = await ProductService.GetProductAsync(id); | ||
if (prod == null) | ||
{ | ||
return NotFound(); // Return 404 Not Found status | ||
} | ||
return Ok(prod); // Return the addon if found | ||
} | ||
|
||
[HttpGet("get_by_name/{name}")] | ||
public async Task<IActionResult> GetProductAsync(string name) | ||
{ | ||
var prod = await ProductService.GetProductAsync(name); | ||
if (prod == null) | ||
{ | ||
return NotFound(); // Return 404 Not Found status | ||
} | ||
return Ok(prod); // Return the addon if found | ||
} | ||
|
||
[HttpPost("add")] | ||
public async Task CreateProductAsync(Product product) | ||
{ | ||
await ProductService.CreateProductAsync(product); | ||
} | ||
|
||
[HttpPatch("update")] | ||
public async Task UpdateProductAsync(Product product) | ||
{ | ||
await ProductService.UpdateProductAsync(product); | ||
} | ||
|
||
[HttpDelete("delete/{id}")] | ||
public async Task DeleteProductAsync(int id) | ||
{ | ||
await ProductService.DeleteProductAsync(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
| ||
using KakeysBakery.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace KakeysBakery.Services; | ||
|
||
public class ProductService : IProductService | ||
{ | ||
private PostgresContext _context; | ||
public ProductService(PostgresContext pc) | ||
{ | ||
_context = pc; | ||
} | ||
public Task CreateProductAsync(Product product) | ||
{ | ||
try | ||
{ | ||
_context.Products.Add(product); | ||
_context.SaveChanges(); | ||
} | ||
catch { } | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task DeleteProductAsync(int baseGoodId) | ||
{ | ||
try | ||
{ | ||
Product? product = _context.Products.FirstOrDefault(b => b.Id == baseGoodId); | ||
if (product != null) | ||
{ | ||
_context.Products.Remove(product); | ||
_context.SaveChanges(); | ||
} | ||
} | ||
catch { } | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task<List<Product>> GetProductListAsync() | ||
{ | ||
try | ||
{ | ||
return await _context.Products.ToListAsync(); | ||
} | ||
catch { return new List<Product>(); } | ||
} | ||
|
||
public async Task<Product?> GetProductAsync(int id) | ||
{ | ||
return await _context.Products | ||
.Where(b => b.Id == id) | ||
.FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<Product?> GetProductAsync(string name) | ||
{ | ||
return await _context.Products | ||
.Where(b => b.Productname == name) | ||
.FirstOrDefaultAsync(); | ||
} | ||
|
||
public Task UpdateProductAsync(Product product) | ||
{ | ||
try | ||
{ | ||
_context.Products.Update(product); | ||
_context.SaveChanges(); | ||
} | ||
catch { } | ||
return Task.CompletedTask; | ||
} | ||
} |