2023-03-04 17:55:19 +04:00
using ConfectioneryClientApp.Models ;
2023-03-04 20:46:18 +04:00
using ConfectioneryClientApp ;
using ConfectioneryContracts.BindingModels ;
using ConfectioneryContracts.ViewModels ;
2023-03-04 17:55:19 +04:00
using Microsoft.AspNetCore.Mvc ;
using System.Diagnostics ;
2023-03-18 00:02:03 +04:00
using System.Text ;
2023-03-17 21:43:59 +04:00
using ConfectioneryContracts.SearchModels ;
2023-03-18 00:02:03 +04:00
using Microsoft.AspNetCore.Mvc.Rendering ;
2023-03-04 17:55:19 +04:00
namespace ConfectioneryClientApp.Controllers
{
2023-03-04 20:46:18 +04:00
public class HomeController : Controller
{
private readonly ILogger < HomeController > _logger ;
public HomeController ( ILogger < HomeController > logger )
{
_logger = logger ;
}
public IActionResult Index ( )
{
if ( APIClient . Client = = null )
{
return Redirect ( "~/Home/Enter" ) ;
}
return View ( APIClient . GetRequest < List < OrderViewModel > > ( $"api/main/getorders?clientId={APIClient.Client.Id}" ) ) ;
}
[HttpGet]
public IActionResult Privacy ( )
{
if ( APIClient . Client = = null )
{
return Redirect ( "~/Home/Enter" ) ;
}
return View ( APIClient . Client ) ;
}
[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 ( ) ;
}
[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
} ) ;
Response . Redirect ( "Enter" ) ;
return ;
}
[HttpGet]
public IActionResult Create ( )
{
ViewBag . Pastries = APIClient . GetRequest < List < PastryViewModel > > ( "api/main/getpastrylist" ) ;
return View ( ) ;
}
[HttpPost]
public void Create ( int pastry , 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 ,
PastryId = pastry ,
Count = count ,
Sum = Calc ( count , pastry )
} ) ;
Response . Redirect ( "Index" ) ;
}
[HttpPost]
public double Calc ( int count , int pastry )
{
var prod = APIClient . GetRequest < PastryViewModel > ( $"api/main/getpastry?pastryId={pastry}" ) ;
return count * ( prod ? . Price ? ? 1 ) ;
}
2023-03-13 22:59:37 +04:00
[HttpGet]
public IActionResult Mails ( )
{
if ( APIClient . Client = = null )
{
return Redirect ( "~/Home/Enter" ) ;
}
2023-03-18 00:02:03 +04:00
return View ( ) ;
2023-03-13 22:59:37 +04:00
}
2023-03-17 21:43:59 +04:00
2023-03-18 00:02:03 +04:00
/// <summary>
/// Switches the page.
/// </summary>
/// <returns>Возвращает кортеж с таблицой в html, текущей страницей писем, выключать ли кнопку пред. страницы, выключать ли кнопку след. страницы</returns>
2023-03-17 21:43:59 +04:00
[HttpGet]
2023-03-18 00:02:03 +04:00
public Tuple < string? , string? , bool , bool > ? SwitchPage ( bool isNext )
2023-03-17 21:43:59 +04:00
{
if ( isNext )
{
APIClient . CurrentPage + + ;
}
else
{
if ( APIClient . CurrentPage = = 1 )
{
2023-03-18 00:02:03 +04:00
return null ;
2023-03-17 21:43:59 +04:00
}
APIClient . CurrentPage - - ;
}
2023-03-18 00:02:03 +04:00
var res = APIClient . GetRequest < List < MessageInfoViewModel > > ( $"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}" ) ;
if ( isNext & & ( res = = null | | res . Count = = 0 ) )
{
APIClient . CurrentPage - - ;
return Tuple . Create < string? , string? , bool , bool > ( null , null , APIClient . CurrentPage ! = 1 , false ) ;
}
StringBuilder htmlTable = new ( ) ;
foreach ( var mail in res )
{
htmlTable . Append ( "<tr>" +
$"<td>{mail.DateDelivery}</td>" +
$"<td>{mail.Subject}</td>" +
$"<td>{mail.Body}</td>" +
"</tr>" ) ;
}
return Tuple . Create < string? , string? , bool , bool > ( htmlTable . ToString ( ) , APIClient . CurrentPage . ToString ( ) , APIClient . CurrentPage ! = 1 , true ) ;
2023-03-17 21:43:59 +04:00
}
2023-03-04 20:46:18 +04:00
}
2023-03-04 17:55:19 +04:00
}