Skip to content

Commit

Permalink
Added Product and it's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce.coon authored and bryce.coon committed Feb 29, 2024
1 parent 64c7f27 commit 8b67752
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 0 deletions.
161 changes: 161 additions & 0 deletions KakeysBakery.Tests/ProductTests.cs
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}");
});
}
}
61 changes: 61 additions & 0 deletions KakeysBakery/Controllers/ProductController.cs
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);
}
}
1 change: 1 addition & 0 deletions KakeysBakery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
builder.Services.AddScoped<IAddonService, AddOnService>();
builder.Services.AddScoped<IBaseGoodService, BaseGoodService>();
builder.Services.AddScoped<IPurchaseService, PurchaseService>();
builder.Services.AddScoped<IProductService, ProductService>();

builder.Services.AddControllers();
builder.Services.AddDbContext<PostgresContext>(options => options.UseNpgsql(builder.Configuration["db"]));
Expand Down
73 changes: 73 additions & 0 deletions KakeysBakery/Services/ProductService.cs
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;
}
}

0 comments on commit 8b67752

Please sign in to comment.