sale works

This commit is contained in:
Zakharov_Rostislav 2024-05-13 17:05:52 +04:00
parent fcafbcfdac
commit 9899ddddb6
3 changed files with 21 additions and 18 deletions

View File

@ -19,7 +19,9 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Sales return context.Sales
.Select(x => x.GetView()) .Include(s => s.Client)
.Include(s => s.Employee)
.Select(x => x.GetView())
.ToList(); .ToList();
} }
@ -31,6 +33,8 @@ namespace CarShowroomDatabaseStorage.Storages
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Sales return context.Sales
.Include(s => s.Client)
.Include(s => s.Employee)
.Where(x => !model.Id.HasValue || x.Id == model.Id) .Where(x => !model.Id.HasValue || x.Id == model.Id)
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
@ -58,8 +62,8 @@ namespace CarShowroomDatabaseStorage.Storages
if (newSale == null) if (newSale == null)
throw new InvalidOperationException("SaleId is incorrect"); throw new InvalidOperationException("SaleId is incorrect");
context.SaveChanges(); context.SaveChanges();
int saleId = context.Sales.Add(newSale).CurrentValues.GetValue<int>("Id"); context.Sales.Add(newSale);
SaleView result = UpdateCarsAndServices(context, model, saleId); SaleView result = UpdateCarsAndServices(context, model, newSale);
transaction.Commit(); transaction.Commit();
return result; return result;
} }
@ -82,7 +86,7 @@ namespace CarShowroomDatabaseStorage.Storages
throw new InvalidOperationException("SaleId is incorrect"); throw new InvalidOperationException("SaleId is incorrect");
sale.Update(model); sale.Update(model);
context.SaveChanges(); context.SaveChanges();
SaleView result = UpdateCarsAndServices(context, model, sale.Id); SaleView result = UpdateCarsAndServices(context, model, sale);
transaction.Commit(); transaction.Commit();
return result; return result;
} }
@ -108,9 +112,8 @@ namespace CarShowroomDatabaseStorage.Storages
} }
private SaleView UpdateCarsAndServices(CarShowroomDatabase context, private SaleView UpdateCarsAndServices(CarShowroomDatabase context,
SaleDto model, int saleId) SaleDto model, Sale sale)
{ {
var sale = context.Sales.First(x => x.Id == saleId);
var saleCars = context.SaleCars var saleCars = context.SaleCars
.Where(rec => rec.SaleId == model.Id) .Where(rec => rec.SaleId == model.Id)
.ToList(); .ToList();
@ -151,27 +154,20 @@ namespace CarShowroomDatabaseStorage.Storages
}); });
context.SaveChanges(); context.SaveChanges();
} }
return UpdatePrice(context, saleId); return UpdatePrice(context, sale);
} }
private SaleView UpdatePrice(CarShowroomDatabase context, int saleId) private SaleView UpdatePrice(CarShowroomDatabase context, Sale sale)
{ {
Sale? sale = context.Sales
.FirstOrDefault(rec => rec.Id == saleId);
if (sale == null) if (sale == null)
throw new InvalidOperationException("SaleId is incorrect"); throw new InvalidOperationException("SaleId is incorrect");
List<Car> Cars = context.Cars List<Car> Cars = context.Cars
.Include(c => c.Model) .Include(c => c.Model)
.Include(c => c.SaleCars) .Where(c => sale.CarIds.Contains(c.Id))
.Where(c => c.SaleCars
.Select(sc => sc.SaleId)
.Contains(saleId))
.ToList(); .ToList();
List<Service> Services = context.Services List<Service> Services = context.Services
.Include(s => s.SaleServices) .Include(s => s.SaleServices)
.Where(s => s.SaleServices .Where(s => sale.ServiceIds.Contains(s.Id))
.Select(ss => ss.SaleId)
.Contains(saleId))
.ToList(); .ToList();
int price = 0; int price = 0;
foreach(Car car in Cars) foreach(Car car in Cars)

View File

@ -86,10 +86,12 @@ namespace CarShowroomManagerApp.Controllers
if (ApiClient.Client == null) if (ApiClient.Client == null)
{ {
Redirect("~/Home/Enter"); Redirect("~/Home/Enter");
return;
} }
ApiClient.PostRequest("api/sale/createsale", new ApiClient.PostRequest("api/sale/createsale", new
SaleDto SaleDto
{ {
EmployeeId = ApiClient.Client.Id,
ClientId = client, ClientId = client,
CarIds = cars, CarIds = cars,
ServiceIds = services ServiceIds = services

View File

@ -22,8 +22,9 @@
<tr> <tr>
<th>Номер</th> <th>Номер</th>
<th>Цена</th> <th>Цена</th>
<th>Дата открытия</th> <th>Время продажи</th>
<th>Менеджер</th> <th>Менеджер</th>
<th>Клиент</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -38,6 +39,10 @@
@Html.DisplayFor(modelItem => @Html.DisplayFor(modelItem =>
item.Price) item.Price)
</td> </td>
<td>
@Html.DisplayFor(modelItem =>
item.SaleTime)
</td>
<td> <td>
@Html.DisplayFor(modelItem => @Html.DisplayFor(modelItem =>
item.EmployeeName) item.EmployeeName)