This commit is contained in:
Илья Федотов 2024-07-24 17:40:56 +04:00
parent ae632c5cb4
commit 5c574a0dec
9 changed files with 82 additions and 41 deletions

View File

@ -89,7 +89,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public bool SetStatus(PaymeantBindingModel model, PaymeantOption paymeantOption) {
CheckModel(model, false);
model.PayOption = paymeantOption;
if (_storage.UpdatePay == null) {
if (_storage.UpdatePay(model) == null) {
_logger.LogWarning("update operation failed");
return false;
}

View File

@ -10,7 +10,6 @@ namespace ElectronicsShopContracts.SearchModels
{
public int? ID { get; set; }
public int? OrderID { get; set; }
public double? SumPay { get; set; }
public int? ClientID { get; set; }
public DateTime? DateFrom{ get; set; }
public DateTime? DateTo { get; set; }

View File

@ -32,6 +32,7 @@ namespace ElectronicsShopDataBaseImplement.Implements
if (order == null) {
return null;
}
order.Update(model);
order.UpdateProducts(context, model);
transcation.Commit();
return order.GetViewModel;

View File

@ -25,7 +25,6 @@ namespace ElectronicsShopDataBaseImplement.Implements {
return newPayment.GetViewModel;
}
// todo тут должен меняться статус оплаты
public PaymeantViewModel? UpdatePay(PaymeantBindingModel model) {
using var context = new Database();
var paymeant = context.Paymeants.FirstOrDefault(x => x.ID == model.ID);
@ -39,7 +38,11 @@ namespace ElectronicsShopDataBaseImplement.Implements {
public PaymeantViewModel? GetElement(PaymeantSearchModel model) {
using var context = new Database();
return context.Paymeants.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
if (model.OrderID.HasValue) {
return context.Paymeants.FirstOrDefault(x => x.OrderID == model.OrderID)?.GetViewModel;
}
return context.Paymeants
.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<PaymeantViewModel> GetFillteredList(PaymeantSearchModel model) {
@ -50,9 +53,16 @@ namespace ElectronicsShopDataBaseImplement.Implements {
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientID.HasValue) {
return context.Paymeants
.Where(x => x.ClientID == model.ClientID).Select(x => x.GetViewModel).ToList();
}
return context.Paymeants
.Where(x => x.OrderID == model.OrderID)
.Select(x => x.GetViewModel)
.ToList();
}
public List<PaymeantViewModel>? GetFullList() {
using var context = new Database();

View File

@ -8,6 +8,7 @@ using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Identity.Client;
using MigraDoc.Rendering;
@ -74,19 +75,64 @@ namespace ElectronicsShopRestAPI.Controllers {
[HttpPost]
public void CreatePaymeant (PaymeantBindingModel model) {
try {
if (_payLogic.ReadList(new PaymeantSearchModel { OrderID = model.OrderID })?.Count > 0) {
var order = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID });
if (order == null) {
throw new Exception("Ошибка получения данных");
}
_orderLogic.Update(new OrderBindingModel {
ID = order.ID,
ClientID = order.ClientID,
DateCreate = order.DateCreate,
Sum = order.Sum - model.SumPayment,
ProductList = order.ProductList,
});
var payemeant = _payLogic.ReadElement(new PaymeantSearchModel { OrderID = model.OrderID })
?? throw new Exception("Ошибка получения оплаты");
payemeant.SumPayment += model.SumPayment;
_payLogic.SetStatus(new PaymeantBindingModel {
ID = payemeant.ID,
OrderID = payemeant.OrderID,
SumPayment = payemeant.SumPayment,
PayOption = payemeant.PayOption,
ClientID = payemeant.ClientID,
DatePaymeant = payemeant.DatePaymeant,
PayProductList = payemeant.PayProductList,
}, model.PayOption);
}
else {
var products = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID })?.ProductList;
if (products == null) {
throw new Exception("Ошибка получения товаров");
throw new Exception("Ошибка получения данных");
}
model.PayProductList = products;
_payLogic.CreatePay(model);
}
if (model.PayOption == 0) {
_orderLogic.Delete(new OrderBindingModel { ID = model.OrderID });
}
else {
var order = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID });
if (order == null) {
throw new Exception("Ошибка получения данных");
}
_orderLogic.Update(new OrderBindingModel {
ID = order.ID,
ClientID = order.ClientID,
DateCreate = order.DateCreate,
Sum = order.Sum - model.SumPayment,
ProductList = order.ProductList,
});
}
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания оплаты");

View File

@ -82,8 +82,11 @@ namespace ElectronicsShopRestAPI.Controllers {
}
[HttpGet]
public OrderViewModel? GetOrder(int _clientID) {
public OrderViewModel? GetOrder(int? _clientID, int? _orderID) {
try {
if (_orderID.HasValue) {
return _order.ReadElement(new OrderSearchModel { ID = _orderID });
}
return _order.ReadElement(new OrderSearchModel { ClientID = _clientID });
}
catch (Exception ex) {

View File

@ -235,7 +235,10 @@ namespace ElectronicsShopUserApp.Controllers {
_productList.Add(product.ID, (product, count));
}
(int, Dictionary<int, (IProductModel, int)>) tuple = (id, _productList);
double sum = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_orderid={id}").Sum;
(double ,int, Dictionary<int, (IProductModel, int)>) tuple = (sum, id, _productList);
return View(tuple);
}

View File

@ -1,7 +1,7 @@
@using ElectronicsShopContracts.ViewModels
@using ElectronicsShopDataModels.Models
@model (int, Dictionary<int, (IProductModel, int)>)
@model (double, int, Dictionary<int, (IProductModel, int)>)
@{
ViewData["Title"] = "Payment";
@ -15,7 +15,7 @@
<div class="row">
<div class="col-4"></div>
<div class="col-8">
<input id="id" type="hidden" name="id" readonly value="@Model.Item1" />
<input id="id" type="hidden" name="id" readonly value="@Model.Item2" />
</div>
</div>
<div class=" text-center">
@ -29,7 +29,7 @@
<div class="row">
<div class="col-4">Сумма к оплате:</div>
<div class="col-8">
<input type="text" name="sum" id="sum" readonly />
<input type="text" name="sum" id="sum" readonly value="@Model.Item1"/>
</div>
<input type="submit" value="Оплатить!" class="btn btn-primary" />
</div>
@ -51,7 +51,7 @@
</tr>
</thead>
<tbody>
@foreach (var item in Model.Item2)
@foreach (var item in Model.Item3)
{
<tr class="element">
<th>
@ -80,25 +80,4 @@
</div>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
</form>
<script>
$('#btn').on('click', function () {
calc();
});
let sum = 0;
const elementRows = document.querySelectorAll('.element');
calc();
function calc() {
elementRows.forEach(row => {
const count = parseInt(row.querySelector('.count').innerHTML, 10);
const countsum = parseInt(row.querySelector('.countsum').innerHTML, 10);
const rowTotal = count * countsum;
sum += rowTotal;
});
$('#sum').val(sum);
}
</script>