refactor: project cleanup before further work

This commit is contained in:
cuqmbr 2022-05-11 15:12:21 +03:00
parent 625c6ced9b
commit acb4786f48
19 changed files with 286 additions and 203 deletions

View File

@ -1,10 +0,0 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace TicketOffice.Pages.Account;
public class Index : PageModel
{
public void OnGet()
{
}
}

View File

@ -1,9 +1,11 @@
@page
@model TicketOffice.Pages.Account.Index
@model TicketOffice.Pages.Account.IndexModel
@{
Layout = "~/Pages/Shared/_Layout.cshtml";
ViewData["Title"] = "Аккаунт";
}
}
<link rel="stylesheet" href="~/css/Account.css"/>

View File

@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using TicketOffice.Data;
using TicketOffice.Models;
namespace TicketOffice.Pages.Account;
public class IndexModel : PageModel
{
}

View File

@ -0,0 +1,6 @@
@page
@model TicketOffice.Pages.Auth.IndexModel
@{
Layout = null;
}

View File

@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace TicketOffice.Pages.Auth;
public class IndexModel : PageModel
{
public ActionResult OnGet() => HttpContext.Session.GetInt32("UserId") is not null ? RedirectToPage("/Auth/Account") : RedirectToPage("/Auth/Login");
}

View File

@ -16,13 +16,13 @@
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/>
<div class="validation-error">
<span>@Model.emailValidation</span>
<span>@Model.EmailValidation</span>
</div>
<br>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/>
<div class="validation-error">
<span>@Model.passwordValidation</span>
<span>@Model.PasswordValidation</span>
</div>
<br>

View File

@ -9,11 +9,13 @@ namespace TicketOffice.Pages.Auth;
public class LoginModel : PageModel
{
public IList<User> User { get; set; }
[BindProperty] public string Email { get; set; }
[BindProperty] public string Password { get; set; }
public string emailValidation;
public string passwordValidation;
[BindProperty] public string Email { get; set; } = String.Empty;
[BindProperty] public string Password { get; set; } = String.Empty;
public string EmailValidation;
public string PasswordValidation;
private List<User> User { get; set; }
private readonly TicketOfficeContext _context;
@ -22,38 +24,30 @@ public class LoginModel : PageModel
_context = context;
}
public IActionResult OnGet()
public ActionResult OnGet() => ValidateSession() ? RedirectToPage("/Auth/Account") : Page();
public ActionResult OnPost()
{
if (HttpContext.Session.GetInt32("UserId") != null)
{
return RedirectToPage("/Account/Index");
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
emailValidation = String.Empty;
passwordValidation = String.Empty;
User = await _context.User
.Where(u => u.Email == Email)
.ToListAsync();
if (ValidateEmail(Email, out emailValidation) && ValidatePassword(Password, out passwordValidation))
if (ValidateForm())
{
HttpContext.Session.SetInt32("UserId", User.First().Id);
return RedirectToPage("/Account/Index");
return RedirectToPage("/Auth/Account");
}
return Page();
}
public bool ValidateEmail(string email, out string validationError)
private bool ValidateForm()
{
if (User.Any(u => u.Email == email))
User = _context.User
.Where(u => u.Email == Email)
.ToList();
return ValidateEmail(Email, out EmailValidation) && ValidatePassword(Password, out PasswordValidation);
bool ValidateEmail(string email, out string validationError)
{
if (User.Count == 1)
{
validationError = String.Empty;
return true;
@ -77,9 +71,9 @@ public class LoginModel : PageModel
return false;
}
public bool ValidatePassword(string password, out string validationError)
bool ValidatePassword(string password, out string validationError)
{
if (User.Where(u => u.Email == Email).Any(u => u.Password == password))
if (User.First().Password == password)
{
validationError = String.Empty;
return true;
@ -94,4 +88,7 @@ public class LoginModel : PageModel
validationError = "Неправильний пароль";
return false;
}
}
private bool ValidateSession() => HttpContext.Session.GetInt32("UserId") is not null;
}

View File

@ -16,13 +16,13 @@
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/>
<div class="validation-error">
<span>@Model.emailValidation</span>
<span>@Model.EmailValidation</span>
</div>
<br>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/>
<div class="validation-error">
<span>@Model.passwordValidation</span>
<span>@Model.PasswordValidation</span>
</div>
<br>

View File

@ -9,11 +9,13 @@ namespace TicketOffice.Pages.Auth;
public class RegistrationModel : PageModel
{
public IList<User> User { get; set; }
[BindProperty] public string Email { get; set; }
[BindProperty] public string Password { get; set; }
public string emailValidation;
public string passwordValidation;
[BindProperty] public string Email { get; set; } = String.Empty;
[BindProperty] public string Password { get; set; } = String.Empty;
public string EmailValidation;
public string PasswordValidation;
private List<User> User { get; set; }
private readonly TicketOfficeContext _context;
@ -22,48 +24,33 @@ public class RegistrationModel : PageModel
_context = context;
}
public IActionResult OnGet()
{
if (HttpContext.Session.GetInt32("UserId") != null)
{
return RedirectToPage("/Account/Index");
}
public ActionResult OnGet() => ValidateSession() ? RedirectToPage("/Auth/Account") : Page();
emailValidation = String.Empty;
passwordValidation = String.Empty;
return Page();
}
public async Task<IActionResult> OnPostAsync()
public ActionResult OnPostAsync()
{
User = await _context.User
if (ValidateForm())
{
_context.User.Add(new User {Email = Email, Password = Password});
_context.SaveChanges();
User = _context.User
.Where(u => u.Email == Email)
.ToListAsync();
if (ValidateEmail(Email, out emailValidation) && ValidatePassword(Password, out passwordValidation))
{
_context.User.Add(new User
{
Email = Email,
Password = Password
});
await _context.SaveChangesAsync();
User = await _context.User
.Where(u => u.Email == Email)
.ToListAsync();
.ToList();
HttpContext.Session.SetInt32("UserId", User.First().Id);
return RedirectToPage("/Account/Index");
return RedirectToPage("/Auth/Account");
}
return Page();
}
public bool ValidateEmail(string email, out string validationError)
private bool ValidateForm()
{
return ValidateEmail(Email, out EmailValidation) && ValidatePassword(Password, out PasswordValidation);
bool ValidateEmail(string email, out string validationError)
{
Regex emailRegex = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$");
@ -79,6 +66,10 @@ public class RegistrationModel : PageModel
return false;
}
User = _context.User
.Where(u => u.Email == Email)
.ToList();
if (User.Any())
{
validationError = "E-mail уже зареєстровано";
@ -89,7 +80,7 @@ public class RegistrationModel : PageModel
return true;
}
public bool ValidatePassword(string passowrd, out string validationError)
bool ValidatePassword(string passowrd, out string validationError)
{
if (String.IsNullOrWhiteSpace(passowrd))
{
@ -114,4 +105,7 @@ public class RegistrationModel : PageModel
validationError = String.Empty;
return true;
}
}
bool ValidateSession() => HttpContext.Session.GetInt32("UserId") is not null;
}

View File

@ -1,17 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TicketOffice.Data;
using TicketOffice.Models;
namespace TicketOffice.Pages;
public class IndexModel : PageModel
{
public IndexModel(TicketOfficeContext context)
{
}
public void OnGet()
{
}
}
public class IndexModel : PageModel {}

View File

@ -175,7 +175,7 @@
else if (Model.Routes == null)
{
<div class="search-error">
<p>Введіть дату й місто відправлення або прибуття</p>
<p>Уведіть дату й місто відправлення або прибуття</p>
</div>
}
else
@ -288,26 +288,43 @@
<div class="ticket-body-main">
<form class="ticket-form" method="post" id="ticket-form-@route.Id" novalidate>
<div class="ticket-input-item" style="margin-right: 0.5rem">
<input class="ticket-input-lastname" type="text" placeholder="Прізвище" @(HttpContext.Session.GetInt32("UserId").HasValue ? "" : "readonly") asp-for="PassengerLastName"/>
<div class="ticket-validation-error"><span asp-validation-for="PassengerLastName"></span></div>
@if (HttpContext.Session.GetInt32("UserId").HasValue)
{
<input class="ticket-input-lastname" type="text" placeholder="Прізвище" asp-for="Ticket.PassengerLastName"/>
}
else
{
<input class="ticket-input-lastname" type="text" placeholder="Прізвище" readonly/>
}
</div>
<div class="ticket-input-item" style="margin-left: 0.5rem; margin-right: 0.5rem">
<input class="ticket-input-firstname" type="text" placeholder="Ім'я" @(HttpContext.Session.GetInt32("UserId").HasValue ? "" : "readonly") asp-for="PassengerFirstName"/>
<div class="ticket-validation-error"><span asp-validation-for="PassengerFirstName"></span></div>
@if (HttpContext.Session.GetInt32("UserId").HasValue)
{
<input class="ticket-input-firstname" type="text" placeholder="Ім'я" asp-for="Ticket.PassengerFirstName"/>
}
else
{
<input class="ticket-input-firstname" type="text" placeholder="Ім'я" readonly/>
}
</div>
<div class="ticket-input-item" style="margin-left: 0.5rem">
<select class="ticket-place-select" required @(HttpContext.Session.GetInt32("UserId").HasValue ? "" : "disabled") asp-for="PassengerPlace">
<select class="ticket-place-select" required asp-for="Ticket.PassengerPlace">
<option selected disabled value="">Місце</option>
@for (int i = 1; i <= route.Capacity; i++)
{
if (!route.Tickets.Any(t => t.PassengerPlace == i))
if (route.Tickets.Any(t => t.PassengerPlace == i))
{
<option value="@i" disabled>@i - Місце зайняте</option>
}
else
{
<option value="@i">@i</option>
}
}
</select>
<div class="ticket-validation-error"><span asp-validation-for="PassengerPlace"></span></div>
</div>
<input type="number" value="@route.Id" style="display: none;" asp-for="Ticket.RouteId"/>
<input type="number" value="@HttpContext.Session.GetInt32("UserId")" style="display: none;" asp-for="Ticket.UserId"/>
</form>
<div class="ticket-info">
<div class="ticket-info-line">
@ -352,7 +369,14 @@
</div>
<div class="ticket-body-footer">
<a class="popup-footer-link-button" onclick="document.getElementById('popup-ticket-@route.Id').style.display = 'none'">Закрити</a>
@if (HttpContext.Session.GetString("UserId") != null)
{
<input class="popup-footer-button" type="submit" form="ticket-form-@route.Id" value="Купити">
}
else
{
<a class="popup-footer-link-button" href="/Auth/Login">Авторизуватись</a>
}
</div>
</div>
</div>

View File

@ -2,23 +2,21 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using TicketOffice.Data;
using TicketOffice.Models;
using Route = TicketOffice.Models.Route;
namespace TicketOffice.Pages.Routes;
public class IndexModel : PageModel
{
public List<Route> Routes { get; set; }
[BindProperty] public List<Route> Routes { get; set; }
[BindProperty] public Ticket Ticket { get; set; }
[BindProperty(SupportsGet = true)] public string From { get; set; }
[BindProperty(SupportsGet = true)] public string To { get; set; }
[BindProperty(SupportsGet = true)] public DateTime Date { get; set; } = new DateTime(2022, 03, 28, 0, 0, 0).Date;
[BindProperty(SupportsGet = true)] public string SortString { get; set; }
public string PassengerFirstName { get; set; }
public string PassengerLastName { get; set; }
public int PassengerPlace { get; set; }
private readonly TicketOfficeContext _context;
public IndexModel(TicketOfficeContext context)
@ -45,10 +43,21 @@ public class IndexModel : PageModel
if (!string.IsNullOrWhiteSpace(From) || !string.IsNullOrWhiteSpace(To))
{
//FilterRoutesByDate();
FilterRoutesByDate();
}
}
public ActionResult OnPost()
{
Ticket.User = _context.User.Where(u => u.Id == Ticket.UserId).ToList()[0];
Ticket.Route = _context.Route.Where(r => r.Id == Ticket.RouteId).ToList()[0];
_context.Ticket.Add(Ticket);
_context.SaveChangesAsync();
return RedirectToPage("/Account/Index");
}
public void OnGetSortByNumber()
{
OnGet();
@ -88,8 +97,6 @@ public class IndexModel : PageModel
{
OnGet();
Routes.Sort((x, y) =>
{
TimeSpan? totalDuration;

View File

@ -24,7 +24,7 @@
<div class="topnav-right">
@if (Context.Session.GetString("UserId") != null)
{
<a class="@(path.Contains("account") ? "active" : "")" href="/Account">Аккаунт</a>
<a class="@(path.Contains("account") ? "active" : "")" href="/Auth/Account">Аккаунт</a>
}
else
{

View File

@ -15,7 +15,7 @@ builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.Cookie.Name = ".AutoBus.Session";
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.IdleTimeout = TimeSpan.FromSeconds(3600);
options.Cookie.IsEssential = true;
});

View File

@ -0,0 +1,57 @@
html {
font-size: 16px;
min-height: 100%;
font-family: 'Roboto', sans-serif;
background-color: #eaeef1;
}
body {
margin: 0;
}
.section {
width: 70rem;
margin: 1.5rem auto;
}
.section-header {
text-align: center;
font-size: 1.25rem;
font-weight: 700;
}
.section-text {
text-align: center;
font-size: 1rem;
font-weight: 700;
margin: 2rem 0;
}
.link-btn {
line-height: 2.5rem;
padding: 0 1rem;
display: inline-block;
color: #1d4965;
font-weight: 700;
background: linear-gradient(0deg,#79b6db,#b3dbf2);
border: none;
border-radius: .3rem;
cursor: pointer;
text-decoration: none;
}
.link {
color: #245c78;
text-decoration: none;
cursor: pointer;
}
.link:hover {
text-decoration: underline;
}
.divider {
background-color: #9ccdf0;
height: 0.2rem;
width: 100%;
}

View File

@ -39,7 +39,7 @@ div.title-block {
div.title-header {
color: white;
font-size: 2rem;
font-size: 1.8rem;
font-weight: 700;
height: fit-content;
margin-bottom: 1rem;

View File

@ -14,7 +14,7 @@ body {
.topnav {
width: 100%;
background-color: white;
border-bottom: .2rem solid #78b5da;
border-bottom: .2rem solid #9ccdf0;
overflow: auto;
}
@ -32,7 +32,7 @@ body {
}
.topnav a.active {
background-color: #79b6db;
background-color: #9ccdf0;
}
.topnav-right {

View File

View File

@ -16,7 +16,7 @@ body {
}*/
.wrapper {
width: 70rem;
width: 78rem;
margin: 2.5rem auto;
}
@ -26,13 +26,13 @@ body {
background: #eaeef1;
border-radius: 0.5rem;
box-shadow: 0 1px 2.4rem 0 #c3c9d0;
padding: 1.5rem 1.5rem 1.5rem 1.5rem;
padding: 1.5rem;
margin: 1.5rem 0;
}
.station, .date, div.search-btn {
display: inline-block;
margin: 0 1.5rem;
margin: 0 2.2rem;
}
.title {
@ -68,7 +68,7 @@ input.search-btn {
font-size: 1.6rem;
font-weight: 700;
line-height: 3rem;
padding: 0 1.5rem;
padding: 0 3rem;
display: inline-block;
background: linear-gradient(0deg,#79b6db,#b3dbf2);
border: none;
@ -93,7 +93,8 @@ table {
}
th {
line-height: 4rem;
height: 6rem;
line-height: 1.6rem;
background: #e6e9ed;
border: 1px solid #d7dce1;
padding: 0 1rem;
@ -103,10 +104,6 @@ th {
color: #777a7e;
}
.departure, .arrival {
line-height: 1.6rem;
}
.link-btn-sort {
color: #777a7e;
text-decoration: none;