Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions WMS.WebUI/WMS.WebUI/Controllers/PartnersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Reflection.Metadata.Ecma335;
using WMS.WebUI.Helpers;
using WMS.WebUI.Stores;
using WMS.WebUI.Stores.Interfaces;
using WMS.WebUI.ViewModels;
using WMS.WebUI.ViewModels.PartnerViewModels;

namespace WMS.WebUI.Controllers;

public class PartnersController : Controller
{
private readonly IPartnerStore _partnerStore;

public PartnersController(IPartnerStore partnersStore,
IProductsStore productsStore,
IPartnerStore partnerStore)
{
_partnerStore=partnerStore;
}

public async Task<IActionResult> Index(string? searchString = null, string? partnerType = null)
{
var partners = await _partnerStore.GetPartnersAsync(searchString, partnerType);

ViewBag.SelectedType = partnerType ?? "All";

return View(partners);
}

public async Task<IActionResult> Create()
{
ViewBag.Types = new string[] { "Customer", "Supplier" };
ViewBag.SelectedType = "Customer";

return View();
}

[HttpPost]
public async Task<IActionResult> Create([FromBody] CreatePartnerViewModel data)
{
if (!ModelState.IsValid)
{
return BadRequest();
}

var createdPartner = await _partnerStore.CreateAsync(data);
var result = new
{
redirectToUrl = Url.Action(
"Details", "Partners",
new
{
id = createdPartner.Id,
type = (int)createdPartner.Type
})
};

return Json(result);
}


public async Task<IActionResult> Details(int id, int type)
{
var partner = await _partnerStore.GetByIdAndTypeAsync(id, (PartnerType)type);

return View(partner);
}
public async Task<IActionResult> Edit(int id, int type)
{
var partner = await _partnerStore.GetByIdAndTypeAsync(id, (PartnerType)type);

var editPartner = ParseEditPartner(partner);

return View(editPartner);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(int id, EditPartnerViewModel partner)
{
try
{
await _partnerStore.UpdateAsync(partner);

return RedirectToAction(nameof(Details), new { id });
}
catch
{
return View(partner);
}
}
public async Task<IActionResult> Delete(int id, int type)
{
var partner = await _partnerStore.GetByIdAndTypeAsync(id, (PartnerType)type);

return View(partner);
}

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ConfirmDelete(int id, int type)
{
try
{
await _partnerStore.DeleteAsync(id, (PartnerType)type);

return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
private EditPartnerViewModel ParseEditPartner(PartnerViewModel partner)
{
string[] nameParts = partner.FullName.Split(new char[] { ' ' }, 2);

string firstName = nameParts[0]; // First part is the first name

// If there's more than one part, the rest is considered the last name
string lastName = (nameParts.Length > 1) ? nameParts[1] : "";

return new EditPartnerViewModel()
{
Id = partner.Id,
FirstName = firstName,
LastName = lastName,
Balance = partner.Balance,
PhoneNumber = partner.PhoneNumber ?? "",
};
}
}
67 changes: 63 additions & 4 deletions WMS.WebUI/WMS.WebUI/Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ public class TransactionsController : Controller
{
private readonly ITransactionsStore _transactionsStore;
private readonly IProductsStore _productsStore;
private readonly IPartnerStore _partnerStore;

public TransactionsController(ITransactionsStore transactionsStore, IProductsStore productsStore)
public TransactionsController(ITransactionsStore transactionsStore,
IProductsStore productsStore,
IPartnerStore partnerStore)
{
_transactionsStore = Validator.NotNull(transactionsStore);
_productsStore = Validator.NotNull(productsStore);
_partnerStore=partnerStore;
}

public async Task<IActionResult> Index(string? searchString = null, string? transactionType = null)
Expand All @@ -24,10 +28,16 @@ public async Task<IActionResult> Index(string? searchString = null, string? tran

return View(transactions);
}
public async Task<IActionResult> Details(int id, TransactionType type)
{
var transaction = await _transactionsStore.GetByIdAndTypeAsync(id, type);

return View(transaction);
}

public async Task<IActionResult> Create()
{
var partnersTask = _transactionsStore.GetPartnersAsync();
var partnersTask = _partnerStore.GetPartnersAsync();
var productsTask = _productsStore.GetProductsAsync();

await Task.WhenAll(partnersTask, productsTask);
Expand All @@ -49,7 +59,7 @@ public async Task<IActionResult> Create(
return BadRequest();
}

var createdTransaction = await _transactionsStore.Create(data);
var createdTransaction = await _transactionsStore.CreateAsync(data);
var result = new
{
redirectToUrl = Url.Action(
Expand All @@ -63,11 +73,60 @@ public async Task<IActionResult> Create(

return Json(result);
}
public async Task<IActionResult> Edit(int id,TransactionType type)
{
var partnersTask = _partnerStore.GetPartnersAsync();
var productsTask = _productsStore.GetProductsAsync();
var transaction = _transactionsStore.GetByIdAndTypeAsync(id, type);

public async Task<IActionResult> Details(int id, TransactionType type)
await Task.WhenAll(partnersTask, productsTask,transaction);

ViewBag.Types = new string[] { "Sale", "Supply" };
ViewBag.SelectedType = "Sale";
ViewBag.Partners = partnersTask.Result;
ViewBag.Products= productsTask.Result.Data;

return View(transaction.Result);
}

[HttpPost]
public async Task<IActionResult> Edit(
[FromBody] TransactionView data)
{
if (!ModelState.IsValid)
{
return View(data);
}
await _transactionsStore.UpdateAsync(data);

var result = new
{
redirectToUrl = Url.Action(
"Details",
new
{
id = data.Id,
type = (int)data.Type
})
};

return Json(result);
}
public async Task<IActionResult> Delete(int id, TransactionType type)
{
var transaction = await _transactionsStore.GetByIdAndTypeAsync(id, type);

return View(transaction);
}

[ValidateAntiForgeryToken]
[HttpPost]
[ActionName("Delete")]
public async Task<IActionResult> ConfirmDelete(int id, TransactionType type)
{
await _transactionsStore.DeleteAsync(id, type);

return RedirectToAction(nameof(Index));
}

}
19 changes: 19 additions & 0 deletions WMS.WebUI/WMS.WebUI/Mappings/CustomerMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using WMS.WebUI.ViewModels.PartnerViewModels;

namespace WMS.WebUI.Mappings;

public static class CustomerMappings
{
public static PartnerViewModel ToPartner(this CustomerViewModel viewModel)
{
return new PartnerViewModel()
{
Id = viewModel.Id,
FullName = viewModel.FullName,
PhoneNumber = viewModel.PhoneNumber,
Balance = viewModel.Balance,
Type = PartnerType.Customer,
};
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WMS.WebUI.Mappings;

public static class SalesMappings
public static class SaleMappings
{
public static TransactionView ToTransaction(this SaleViewModel sale)
{
Expand Down
18 changes: 18 additions & 0 deletions WMS.WebUI/WMS.WebUI/Mappings/SupplierMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using WMS.WebUI.ViewModels.PartnerViewModels;

namespace WMS.WebUI.Mappings;

public static class SupplierMappings
{
public static PartnerViewModel ToPartner(this SupplierViewModel viewModel)
{
return new PartnerViewModel()
{
Id = viewModel.Id,
FullName = viewModel.FullName,
PhoneNumber = viewModel.PhoneNumber,
Balance = viewModel.Balance,
Type = PartnerType.Supplier,
};
}
}
1 change: 1 addition & 0 deletions WMS.WebUI/WMS.WebUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
builder.Services.AddScoped<IDashboardStore, DashboardStore>();
builder.Services.AddScoped<ICategoryStore, CategoryStore>();
builder.Services.AddScoped<IProductsStore, ProductStore>();
builder.Services.AddScoped<IPartnerStore,PartnerStore>();
builder.Services.AddScoped<ITransactionsStore, TransactionsStore>();
builder.Services.AddSyncfusion(builder.Configuration);

Expand Down
4 changes: 2 additions & 2 deletions WMS.WebUI/WMS.WebUI/Stores/CategoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class CategoryStore : ICategoryStore
{
private readonly HttpClient _client;

public CategoryStore()
public CategoryStore(IConfiguration configuration)
{
_client = new HttpClient();
_client.BaseAddress = new Uri("https://localhost:7097/api/");
_client.BaseAddress = new Uri(configuration["WMSApiUrl"]);
}

public async Task<CategoryViewModel> CreateCategoryAsync(CategoryViewModel category)
Expand Down
4 changes: 2 additions & 2 deletions WMS.WebUI/WMS.WebUI/Stores/DashboardStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class DashboardStore : IDashboardStore
{
private readonly HttpClient _client;

public DashboardStore()
public DashboardStore(IConfiguration configuration)
{
_client = new HttpClient();
_client.BaseAddress = new Uri("https://localhost:7097/api/");
_client.BaseAddress = new Uri(configuration["WMSApiUrl"]);
}

public async Task<DashboardViewModel> Get()
Expand Down
13 changes: 13 additions & 0 deletions WMS.WebUI/WMS.WebUI/Stores/Interfaces/IPartnerStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using WMS.WebUI.ViewModels;
using WMS.WebUI.ViewModels.PartnerViewModels;

namespace WMS.WebUI.Stores.Interfaces;

public interface IPartnerStore
{
Task<List<PartnerViewModel>> GetPartnersAsync(string? search="", string? type = "");
Task<PartnerViewModel> GetByIdAndTypeAsync(int id, PartnerType type);
Task<PartnerViewModel> CreateAsync(CreatePartnerViewModel partner);
Task UpdateAsync(EditPartnerViewModel partner);
Task DeleteAsync(int id, PartnerType type);
}
6 changes: 4 additions & 2 deletions WMS.WebUI/WMS.WebUI/Stores/Interfaces/ITransactionsStore.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using WMS.WebUI.ViewModels;
using WMS.WebUI.ViewModels.PartnerViewModels;

namespace WMS.WebUI.Stores.Interfaces;

public interface ITransactionsStore
{
Task<List<TransactionView>> GetTransactionsAsync(string? search, string? type);
Task<List<PartnerViewModel>> GetPartnersAsync();
Task<TransactionView> GetByIdAndTypeAsync(int id, TransactionType type);
Task<TransactionView> Create(CreateTransactionViewModel transaction);
Task<TransactionView> CreateAsync(CreateTransactionViewModel transaction);
Task UpdateAsync(TransactionView transaction);
Task DeleteAsync(int id,TransactionType type);
}
Loading