-
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.
Implemented Product api and client side product integration.
- Loading branch information
Olukeye
committed
Feb 13, 2024
1 parent
8fd04d2
commit 41c56aa
Showing
12 changed files
with
359 additions
and
142 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
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,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); | ||
} | ||
} | ||
} |
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,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; } | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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 | ||
}); | ||
} | ||
} | ||
} |
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,2 @@ | ||
@* | ||
*@ |
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
Oops, something went wrong.