Skip to content

Commit

Permalink
Implemented Product api and client side product integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olukeye committed Feb 13, 2024
1 parent 8fd04d2 commit 41c56aa
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 142 deletions.
24 changes: 20 additions & 4 deletions JendStore.Client/Controllers/CouponController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public async Task<IActionResult> CouponIndex()
{
list = JsonConvert.DeserializeObject<List<CouponDTO>>(Convert.ToString(response.Result));
}
else
{
TempData["success"] = response?.Message;
}
return View(list);
}

Expand All @@ -43,6 +47,10 @@ public async Task<IActionResult> CreateCoupon(CouponDTO create)
{
return RedirectToAction(nameof(CouponIndex));
}
else
{
TempData["success"] = response?.Message;
}
}
return View(create);
}
Expand All @@ -57,6 +65,10 @@ public async Task<IActionResult> DeleteCoupon(int couponId)
CouponDTO? model = JsonConvert.DeserializeObject<CouponDTO>(Convert.ToString(response.Result));
return View(model);
}
else
{
TempData["success"] = response?.Message;
}
return NotFound();
}

Expand All @@ -65,10 +77,14 @@ public async Task<IActionResult> DeleteCoupon(CouponDTO couponDTO)
{
ResponsDto? response = await _couponService.DeleteCouponAsync(couponDTO.CouponId);

if (response != null && response.IsSuccess)
{
return RedirectToAction(nameof(CouponIndex));
}
if (response != null && response.IsSuccess)
{
return RedirectToAction(nameof(CouponIndex));
}
else
{
TempData["success"] = response?.Message;
}

return View(couponDTO);
}
Expand Down
95 changes: 95 additions & 0 deletions JendStore.Client/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using JendStore.Client.Models;
using JendStore.Client.Service.IService;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace JendStore.Client.Controllers
{
public class ProductController : Controller
{
private readonly IProductService _productService;

public ProductController(IProductService productService)
{
_productService = productService;
}

public async Task<IActionResult> ProductIndex()
{
List<ProductDto>? list = new();

ResponsDto? response = await _productService.GetAllProductAsync();

if (response != null && response.IsSuccess)
{
list = JsonConvert.DeserializeObject<List<ProductDto>>(Convert.ToString(response.Result));
}
else
{
TempData["success"] = response?.Message;
}

return View(list);
}

public async Task<IActionResult> CreateProduct()
{
return View();
}

[HttpPost]
public async Task<IActionResult> CreateProduct(ProductDto create)
{
if (ModelState.IsValid)
{
ResponsDto? response = await _productService.CreateProductAsync(create);

if (response != null && response.IsSuccess)
{
return RedirectToAction(nameof(ProductIndex));
}
else
{
TempData["success"] = response?.Message;
}
}
return View(create);
}

public async Task<IActionResult> DeleteProduct(int productDto)
{

ResponsDto? response = await _productService.GetProductAsync(productDto);

if (response != null && response.IsSuccess)
{
ProductDto? model = JsonConvert.DeserializeObject<ProductDto>(Convert.ToString(response.Result));
return View(model);
}
else
{
TempData["success"] = response?.Message;
}

return NotFound();
}

[HttpPost]
public async Task<IActionResult> DeleteProduct(ProductDto productDto)
{
ResponsDto? response = await _productService.DeleteProductAsync(productDto.ProductId);

if (response != null && response.IsSuccess)
{
TempData["success"] = "Registration Successful";
return RedirectToAction(nameof(ProductIndex));
}
else
{
TempData["success"] = response?.Message;
}

return View(productDto);
}
}
}
17 changes: 17 additions & 0 deletions JendStore.Client/Models/ProductDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace JendStore.Client.Models
{
public class ProductDto
{
public int ProductId { get; set; }

public string Name { get; set; }

public double Price { get; set; }

public string Description { get; set; }

public string CategoryName { get; set; }

public string ImageUrl { get; set; }
}
}
2 changes: 2 additions & 0 deletions JendStore.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
builder.Services.AddHttpClient<ICouponService, CouponService>();
builder.Services.AddHttpClient<IProductService, ProductService>();
builder.Services.AddHttpClient<IAuthService, AuthService>();
HttpVerbs.CouponAPIBase = builder.Configuration["ServiceUrls:CouponAPI"];
HttpVerbs.AuthAPIBase = builder.Configuration["ServiceUrls:AuthAPI"];


builder.Services.AddScoped<IBaseService, BaseService>();
builder.Services.AddScoped<ICouponService, CouponService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<ITokenProvider, TokenProvider>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
Expand Down
13 changes: 13 additions & 0 deletions JendStore.Client/Service/IService/IProductService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using JendStore.Client.Models;

namespace JendStore.Client.Service.IService
{
public interface IProductService
{
Task<ResponsDto?> GetAllProductAsync();
Task<ResponsDto?> GetProductAsync(int id);
Task<ResponsDto?> UpdateProductAsync(int id);
Task<ResponsDto?> CreateProductAsync(ProductDto productDto);
Task<ResponsDto?> DeleteProductAsync(int id);
}
}
63 changes: 63 additions & 0 deletions JendStore.Client/Service/ProductService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using JendStore.Client.Models;
using JendStore.Client.Service.IService;
using JendStore.Client.Sevice.IService;
using JendStore.Client.Utilities;

namespace JendStore.Client.Service
{
public class ProductService: IProductService
{
private readonly IBaseService _baseService;
public ProductService(IBaseService baseService)
{
_baseService = baseService;
}


public async Task<ResponsDto?> GetAllProductAsync()
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = HttpVerbs.ApiType.GET,
Url = HttpVerbs.ProductAPIBase + "/api/product"
});
}

public async Task<ResponsDto?> CreateProductAsync(ProductDto productDto)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = HttpVerbs.ApiType.POST,
Data = productDto,
Url = HttpVerbs.ProductAPIBase + "/api/product"
});
}

public async Task<ResponsDto?> GetProductAsync(int id)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = HttpVerbs.ApiType.GET,
Url = HttpVerbs.ProductAPIBase + "/api/product/" + id
});
}

public async Task<ResponsDto?> UpdateProductAsync(int id)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = HttpVerbs.ApiType.PUT,
Url = HttpVerbs.ProductAPIBase + "/api/product/" + id
});
}

public async Task<ResponsDto?> DeleteProductAsync(int id)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = HttpVerbs.ApiType.DELETE,
Url = HttpVerbs.ProductAPIBase + "/api/product/" + id
});
}
}
}
1 change: 1 addition & 0 deletions JendStore.Client/Utilities/HttpVerbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class HttpVerbs
{
public static string CouponAPIBase{get; set;}
public static string ProductAPIBase { get; set; }
public static string AuthAPIBase{ get; set; }
public const string RoleAdmin = "ADMIN";
public const string RoleUser = "USER";
Expand Down
2 changes: 2 additions & 0 deletions JendStore.Client/Views/Product/ProductIndex.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@*
*@
1 change: 1 addition & 0 deletions JendStore.Client/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"AllowedHosts": "*",
"ServiceUrls": {
"CouponAPI": "https://localhost:2001",
"ProductAPI": "https://localhost:2004",
"AuthAPI": "https://localhost:2003"
}
}
Loading

0 comments on commit 41c56aa

Please sign in to comment.