diff --git a/KakeysBakery.Tests/ProductTests.cs b/KakeysBakery.Tests/ProductTests.cs new file mode 100644 index 0000000..b276c6f --- /dev/null +++ b/KakeysBakery.Tests/ProductTests.cs @@ -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 +{ + 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? result = await client.GetFromJsonAsync>("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($"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($"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($"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($"api/Product/get/{testProduct.Id}"); + // Assert + Assert.NotNull(temp); + // ACT + await client.DeleteAsync($"api/Product/delete/{testProduct.Id}"); + + // ASSERT + await Assert.ThrowsAsync(async () => + { + await client.GetFromJsonAsync($"api/Product/get/{testProduct.Id}"); + }); + } +} diff --git a/KakeysBakery/Controllers/ProductController.cs b/KakeysBakery/Controllers/ProductController.cs new file mode 100644 index 0000000..c40e0ad --- /dev/null +++ b/KakeysBakery/Controllers/ProductController.cs @@ -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> GetProductsAsync() + { + return await ProductService.GetProductListAsync(); + } + + [HttpGet("get/{id}")] + public async Task 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 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); + } +} diff --git a/KakeysBakery/Program.cs b/KakeysBakery/Program.cs index 15fa638..a247e90 100644 --- a/KakeysBakery/Program.cs +++ b/KakeysBakery/Program.cs @@ -13,6 +13,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddControllers(); builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration["db"])); diff --git a/KakeysBakery/Services/ProductService.cs b/KakeysBakery/Services/ProductService.cs new file mode 100644 index 0000000..3302cbc --- /dev/null +++ b/KakeysBakery/Services/ProductService.cs @@ -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> GetProductListAsync() + { + try + { + return await _context.Products.ToListAsync(); + } + catch { return new List(); } + } + + public async Task GetProductAsync(int id) + { + return await _context.Products + .Where(b => b.Id == id) + .FirstOrDefaultAsync(); + } + + public async Task 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; + } +}