Commit 320b1ea3 authored by TTrueBenji's avatar TTrueBenji

Реализовать функционал заказов.

***
Реализовал возможность закза смартфона.
***
parent 0b081e06
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PhoneStore.Models;
......@@ -8,10 +10,14 @@ namespace PhoneStore.Controllers
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IHostEnvironment _environment;
public HomeController(ILogger<HomeController> logger)
public HomeController(
ILogger<HomeController> logger,
IHostEnvironment environment)
{
_logger = logger;
_environment = environment;
}
[HttpGet]
......@@ -20,6 +26,66 @@ namespace PhoneStore.Controllers
return View();
}
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return PhysicalFile(filePath, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// FileStream stream = new FileStream(filePath, FileMode.Open);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(stream, fileType, fileName);
// }
[HttpGet]
public IActionResult Test()
{
string filePath = Path.Combine("~/Files/", "test.txt");
string fileType = "application/txt";
return File(filePath, fileType);
}
[HttpGet]
[ActionName("Test2")]
public string Test(Phone phone)
{
return "tets";
}
[HttpGet]
[ActionName("Test3")]
public string Test(Phone[] phones)
{
return "tets";
}
public IActionResult Privacy()
{
return View();
......
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Models;
namespace PhoneStore.Controllers
{
public class OrdersController : Controller
{
private readonly MobileContext _db;
public OrdersController(MobileContext db)
{
_db = db;
}
// GET
public IActionResult Index()
{
var orders = _db.Orders
.Include(p => p.Phone)
.ToList();
return View(orders);
}
[HttpGet]
public IActionResult Create(int phoneId)
{
var phone = _db.Phones.FirstOrDefault(p => p.Id == phoneId);
if (phone is null)
return Content("Такого телефона нет.");
Order order = new Order
{
Phone = phone
};
return View(order);
}
[HttpPost]
public IActionResult Create(Order order)
{
_db.Orders.Add(order);
_db.SaveChanges();
return RedirectToAction("Index");
}
}
}
\ No newline at end of file
......@@ -12,4 +12,12 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.17" />
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages" />
</ItemGroup>
</Project>
......@@ -5,4 +5,10 @@
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
\ No newline at end of file
</div>
<form asp-action="Test">
<input type="text" name="id" placeholder="id">
<input type="text" name="quantity" placeholder="quantity">
<button type="submit">Отправить</button>
</form>
\ No newline at end of file
@model Order
@{
ViewBag.Title = "Заказать смартфон";
Layout = "_Layout";
}
<h2>Заполните форму для заказа</h2>
<div class="row">
<div class="col-md-6">
<form asp-action="Create" asp-controller="Orders" method="post">
<div class="form-row">
<label for="">
Имя покупателя
<input asp-for="UserName" type="text">
</label>
</div>
<div class="form-row">
<label for="">
Адрес доставки
<input asp-for="Address" type="text">
</label>
</div>
<div class="form-row">
<label for="">
Номер телефона
<input asp-for="ContactPhone" type="text">
</label>
</div>
<div class="form-row">
<label for="">
Смартфон :
<input disabled type="text" value="@Model.Phone.Name">
</label>
<input asp-for="PhoneId" type="hidden" value="@Model.Phone.Id">
</div>
<button type="submit">Заказать</button>
</form>
</div>
</div>
@model List<Order>
@{
ViewBag.Title = "Список заказов";
Layout = "_Layout";
}
@if (Model.Count == 0)
{
<h2>Список пуст</h2>
}
else
{
<table style="width: 100%">
<tr>
<th>Id</th>
<th>Адрес</th>
<th>Контактный телефон</th>
<th>Имя пользователя</th>
<th>Название телефона</th>
</tr>
@foreach (var order in Model)
{
<tr>
<td>@order.Id</td>
<td>@order.Address</td>
<td>@order.ContactPhone</td>
<td>@order.UserName</td>
<td>@order.Phone.Name</td>
</tr>
}
</table>
}
......@@ -21,6 +21,7 @@ else
<th>Компания</th>
<th>Стоимость</th>
<th></th>
<th></th>
</tr>
@foreach (var phone in Model)
{
......@@ -31,12 +32,20 @@ else
<td>@phone.Price</td>
<td>
<a asp-route-id="@phone.Id"
asp-action="Add"
asp-controller="Basket"
class="btn btn-outline-warning">
asp-action="Add"
asp-controller="Basket"
class="btn btn-outline-warning">
В корзину
</a>
</td>
<td>
<a asp-route-phoneId="@phone.Id"
asp-action="Create"
asp-controller="Orders"
class="btn btn-outline-warning">
Заказать
</a>
</td>
</tr>
}
</table>
......
......@@ -27,6 +27,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Phone" asp-action="Index">Смартфоны</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Orders" asp-action="Index">Заказы</a>
</li>
</ul>
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment