Commit 698e45f3 authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#2 Добавил модель заведения, и пустой контроллер заведений. Изменил регистрацию…

#2 Добавил модель заведения, и пустой контроллер заведений. Изменил регистрацию в AccountController.cs
parent b67bfb69
...@@ -6,5 +6,6 @@ namespace CafeCritic.Context; ...@@ -6,5 +6,6 @@ namespace CafeCritic.Context;
public class CafeDbContext: IdentityDbContext<User> public class CafeDbContext: IdentityDbContext<User>
{ {
public required DbSet<Cafe> Cafes { get; set; }
public CafeDbContext(DbContextOptions<CafeDbContext> options) : base(options){} public CafeDbContext(DbContextOptions<CafeDbContext> options) : base(options){}
} }
\ No newline at end of file
using System.ComponentModel.DataAnnotations;
using CafeCritic.Models; using CafeCritic.Models;
using CafeCritic.ViewModels; using CafeCritic.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
...@@ -18,18 +20,29 @@ public class AccountController : Controller ...@@ -18,18 +20,29 @@ public class AccountController : Controller
// GET // GET
[HttpGet] [HttpGet]
[AllowAnonymous]
public IActionResult Register() public IActionResult Register()
{ {
return View(); var vm = new RegisterViewModel
{
Email = null,
Password = null,
ConfirmPassword = null
};
return View(vm);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model) [AllowAnonymous]
[ValidateAntiForgeryToken]
[Display(Name = "Register")]
public async Task<IActionResult> RegisterAsync(RegisterViewModel model)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
var user = new User var user = new User
{ {
UserName = model.Email,
Email = model.Email Email = model.Email
}; };
var result = await _userManager.CreateAsync(user, model.Password); var result = await _userManager.CreateAsync(user, model.Password);
......
using CafeCritic.Context;
using CafeCritic.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace CafeCritic.Controllers;
public class CafeController : Controller
{
private readonly CafeDbContext _db;
private readonly UserManager<User> _userManager;
public CafeController(CafeDbContext db, UserManager<User> userManager)
{
_db = db;
_userManager = userManager;
}
}
\ No newline at end of file
namespace CafeCritic.Models;
public class Cafe
{
public int Id { get; set; }
public required string Title { get; set; }
public string? Description { get; set; }
public required string Image { get; set; }
}
\ No newline at end of file
...@@ -8,8 +8,6 @@ var builder = WebApplication.CreateBuilder(args); ...@@ -8,8 +8,6 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
var app = builder.Build();
var connectionString = builder.Configuration var connectionString = builder.Configuration
.GetConnectionString("DefaultConnection"); .GetConnectionString("DefaultConnection");
...@@ -24,6 +22,8 @@ builder.Services.AddDbContext<CafeDbContext>(options => options.UseNpgsql(connec ...@@ -24,6 +22,8 @@ builder.Services.AddDbContext<CafeDbContext>(options => options.UseNpgsql(connec
o.Password.RequireDigit = false; o.Password.RequireDigit = false;
}).AddEntityFrameworkStores<CafeDbContext>(); }).AddEntityFrameworkStores<CafeDbContext>();
var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
......
...@@ -24,10 +24,7 @@ ...@@ -24,10 +24,7 @@
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1"> <ul class="navbar-nav flex-grow-1 justify-content-end">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Cafe Critic</a>
</li>
@if (User.Identity!.IsAuthenticated) @if (User.Identity!.IsAuthenticated)
{ {
<li class="nav-item"> <li class="nav-item">
...@@ -35,17 +32,17 @@ ...@@ -35,17 +32,17 @@
</li> </li>
<li class="nav-item"> <li class="nav-item">
<form asp-action="LogOut" asp-controller="Account" method="post"> <form asp-action="LogOut" asp-controller="Account" method="post">
<button type="submit" class="btn btn-dark">Выйти</button> <button type="submit" class="btn btn-primary">Выйти</button>
</form> </form>
</li> </li>
} }
else else
{ {
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-action="Login" asp-controller="Account">Войти</a> <a class="nav-link text-primary m-1" asp-action="Login" asp-controller="Account">Войти</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-action="Register" asp-controller="Account">Зарегистрироваться</a> <a class="nav-link text-primary m-1" asp-action="Register" asp-controller="Account">Зарегистрироваться</a>
</li> </li>
} }
</ul> </ul>
......
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