@ -1,4 +1,6 @@
using BlacksmithWorkshopClientApp.Models ;
using BlacksmithWorkshopContracts.BindingModels ;
using BlacksmithWorkshopContracts.ViewModels ;
using Microsoft.AspNetCore.Mvc ;
using System.Diagnostics ;
@ -6,27 +8,171 @@ namespace BlacksmithWorkshopClientApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger < HomeController > _logger ;
private readonly ILogger < HomeController > _logger ;
public HomeController ( ILogger < HomeController > logger )
{
_logger = logger ;
}
public HomeController ( ILogger < HomeController > logger )
{
_logger = logger ;
}
public IActionResult Index ( )
{
return View ( ) ;
}
//вытаскивает через API клиента Get-запросом список е г о собственных заказов
public IActionResult Index ( )
{
if ( APIClient . Client = = null )
{
return Redirect ( "~/Home/Enter" ) ;
}
public IActionResult Privacy ( )
{
return View ( ) ;
}
return View ( APIClient . GetRequest < List < OrderViewModel > > ( $"api/main/getorders?clientId={APIClient.Client.Id}" ) ) ;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error ( )
{
return View ( new ErrorViewModel { RequestId = Activity . Current ? . Id ? ? HttpContext . TraceIdentifier } ) ;
}
}
//изменемение ланных Get-ом
[HttpGet]
public IActionResult Privacy ( )
{
if ( APIClient . Client = = null )
{
return Redirect ( "~/Home/Enter" ) ;
}
return View ( APIClient . Client ) ;
}
//изменение данных Post-ом
[HttpPost]
public void Privacy ( string login , string password , string fio )
{
if ( APIClient . Client = = null )
{
throw new Exception ( "Вы как сюда попали? Суда вход только авторизованным" ) ;
}
if ( string . IsNullOrEmpty ( login ) | | string . IsNullOrEmpty ( password ) | | string . IsNullOrEmpty ( fio ) )
{
throw new Exception ( "Введите логин, пароль и ФИО" ) ;
}
APIClient . PostRequest ( "api/client/updatedata" , new ClientBindingModel
{
Id = APIClient . Client . Id ,
ClientFIO = fio ,
Email = login ,
Password = password
} ) ;
APIClient . Client . ClientFIO = fio ;
APIClient . Client . Email = login ;
APIClient . Client . Password = password ;
Response . Redirect ( "Index" ) ;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error ( )
{
return View ( new ErrorViewModel
{
RequestId = Activity . Current ? . Id ? ? HttpContext . TraceIdentifier
} ) ;
}
//просто открытие вьюхи
[HttpGet]
public IActionResult Enter ( )
{
return View ( ) ;
}
//отсылаем указанные данные на проверку
[HttpPost]
public void Enter ( string login , string password )
{
if ( string . IsNullOrEmpty ( login ) | | string . IsNullOrEmpty ( password ) )
{
throw new Exception ( "Введите логин и пароль" ) ;
}
APIClient . Client = APIClient . GetRequest < ClientViewModel > ( $"api/client/login?login={login}&password={password}" ) ;
if ( APIClient . Client = = null )
{
throw new Exception ( "Неверный логин/пароль" ) ;
}
Response . Redirect ( "Index" ) ;
}
//просто открытие вьюхи
[HttpGet]
public IActionResult Register ( )
{
return View ( ) ;
}
//Post-запрос по созданию нового пользователя
[HttpPost]
public void Register ( string login , string password , string fio )
{
if ( string . IsNullOrEmpty ( login ) | | string . IsNullOrEmpty ( password ) | | string . IsNullOrEmpty ( fio ) )
{
throw new Exception ( "Введите логин, пароль и ФИО" ) ;
}
APIClient . PostRequest ( "api/client/register" , new ClientBindingModel
{
ClientFIO = fio ,
Email = login ,
Password = password
} ) ;
//переход на вкладку "Enter", чтобы пользователь сразу смог зайти
Response . Redirect ( "Enter" ) ;
return ;
}
//создание заказа. Получаем и передаём список изделий во вьюху?
[HttpGet]
public IActionResult Create ( )
{
ViewBag . Manufactures = APIClient . GetRequest < List < ManufactureViewModel > > ( "api/main/getmanufacturelist" ) ;
return View ( ) ;
}
//создание заказа Post-запросом
[HttpPost]
public void Create ( int manufacture , int count )
{
if ( APIClient . Client = = null )
{
throw new Exception ( "Вы как сюда попали? Суда вход только авторизованным" ) ;
}
if ( count < = 0 )
{
throw new Exception ( "Количество и сумма должны быть больше 0" ) ;
}
APIClient . PostRequest ( "api/main/createorder" , new OrderBindingModel
{
ClientId = APIClient . Client . Id ,
ManufactureId = manufacture ,
Count = count ,
Sum = Calc ( count , manufacture )
} ) ;
Response . Redirect ( "Index" ) ;
}
//подсчёт стоимости заказа
[HttpPost]
public double Calc ( int count , int manufacture )
{
var prod = APIClient . GetRequest < ManufactureViewModel > ( $"api/main/getmanufacture?manufactureId={manufacture}"
) ;
return count * ( prod ? . Price ? ? 1 ) ;
}
}
}