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

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

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