From b57574642563be30655f04d591e71adfa1be1094 Mon Sep 17 00:00:00 2001 From: RobsonLMDS <98341530+Robsonlmds@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:03:58 -0300 Subject: [PATCH] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20de=20seguran=C3=A7a=20e?= =?UTF-8?q?=20atualizando=20so=20many=20to=20many=20e=20outras=20rela?= =?UTF-8?q?=C3=A7=C3=B5es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Crud_Carros/Controllers/CarController.cs | 5 +- Crud_Carros/Controllers/ClientController.cs | 3 +- .../Controllers/ClientOfStaffController.cs | 4 +- Crud_Carros/Controllers/LoginController.cs | 42 +++++++++ .../Controllers/ModelOfCarController.cs | 4 +- Crud_Carros/Controllers/UserController.cs | 87 +++++++++++++++++++ 6 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 Crud_Carros/Controllers/LoginController.cs create mode 100644 Crud_Carros/Controllers/UserController.cs diff --git a/Crud_Carros/Controllers/CarController.cs b/Crud_Carros/Controllers/CarController.cs index efce5c1..1fff2ac 100644 --- a/Crud_Carros/Controllers/CarController.cs +++ b/Crud_Carros/Controllers/CarController.cs @@ -1,9 +1,10 @@ -using Crud_Carros.Data; -using Crud_Carros.Models.Entities; +using Crud_Carros.Models.Entities; using Crud_Carros.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.AspNetCore.Authorization; +using Crud_Carros.Data; namespace Crud_Carros.Controllers diff --git a/Crud_Carros/Controllers/ClientController.cs b/Crud_Carros/Controllers/ClientController.cs index 08c177b..1da941c 100644 --- a/Crud_Carros/Controllers/ClientController.cs +++ b/Crud_Carros/Controllers/ClientController.cs @@ -26,10 +26,10 @@ public async Task AddClient() return View(); } + [HttpPost] public async Task AddClient(AddClientViewModel viewModel) { - var client = new Client { Name_Client = viewModel.Name_Client, @@ -45,6 +45,7 @@ public async Task AddClient(AddClientViewModel viewModel) return RedirectToAction ("ListClient", "Client"); } + [HttpGet] public async Task ListClient() diff --git a/Crud_Carros/Controllers/ClientOfStaffController.cs b/Crud_Carros/Controllers/ClientOfStaffController.cs index 0d867e2..da67764 100644 --- a/Crud_Carros/Controllers/ClientOfStaffController.cs +++ b/Crud_Carros/Controllers/ClientOfStaffController.cs @@ -30,13 +30,13 @@ public async Task AddClientOfStaff() public async Task AddClientOfStaff(AddClientOfStaffViewModel viewModel) { var client = await dbContext.Clients.FindAsync(viewModel.ClientId); - if (client == null) return NotFound(); // Verificar + if (client == null) return NotFound(); foreach (var staffId in viewModel.SelectedStaffIds) { var existeRegistro = await dbContext.ClientOfStaffs .FirstOrDefaultAsync(cos => cos.ClientId == viewModel.ClientId && cos.StaffId == staffId); - if (existeRegistro != null) continue; // Se o registro já existir - Continuar + if (existeRegistro != null) continue; var clientOfStaff = new ClientOfStaff { diff --git a/Crud_Carros/Controllers/LoginController.cs b/Crud_Carros/Controllers/LoginController.cs new file mode 100644 index 0000000..05f0609 --- /dev/null +++ b/Crud_Carros/Controllers/LoginController.cs @@ -0,0 +1,42 @@ +using Crud_Carros.Data; +using Crud_Carros.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; + +namespace Crud_Carros.Controllers +{ + public class LoginController : Controller + { + private readonly ApplicationDbContext dbContext; + + public LoginController(ApplicationDbContext dbContext) + { + this.dbContext = dbContext; + } + + [HttpGet] + public IActionResult Login () + { + return View(); + } + + [HttpPost] + public async Task Login (LoginViewModel loginView) + { + if (ModelState.IsValid) + { + var user = await dbContext.Users + .FirstOrDefaultAsync(user => user.Username == loginView.Input_Username && user.Password == loginView.Input_Password); + + if (user != null) + { + return RedirectToAction("Index", "Home"); + } + ModelState.AddModelError("", "Usuário ou senha inválidos."); + } + return View(loginView); + } + } +} diff --git a/Crud_Carros/Controllers/ModelOfCarController.cs b/Crud_Carros/Controllers/ModelOfCarController.cs index 5bbca44..66b6af1 100644 --- a/Crud_Carros/Controllers/ModelOfCarController.cs +++ b/Crud_Carros/Controllers/ModelOfCarController.cs @@ -1,8 +1,8 @@ -using Crud_Carros.Data; -using Crud_Carros.Models.Entities; +using Crud_Carros.Models.Entities; using Crud_Carros.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Crud_Carros.Data; namespace Crud_Carros.Controllers { diff --git a/Crud_Carros/Controllers/UserController.cs b/Crud_Carros/Controllers/UserController.cs new file mode 100644 index 0000000..e153a2d --- /dev/null +++ b/Crud_Carros/Controllers/UserController.cs @@ -0,0 +1,87 @@ +using Crud_Carros.Data; +using Crud_Carros.Models; +using Crud_Carros.Models.Entities; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; + +namespace Crud_Carros.Controllers +{ + public class UserController : Controller + { + private readonly ApplicationDbContext dbContext; + + public UserController(ApplicationDbContext dbContext) + { + this.dbContext = dbContext; + } + + [HttpGet] + public IActionResult AddUser () + { + return View(); + } + + [HttpPost] + public async Task AddUser (AddUserViewModel viewModel) + { + var user = new User + { + Username = viewModel.Username, + Password = viewModel.Password, + }; + + await dbContext.Users.AddAsync(user); + await dbContext.SaveChangesAsync(); + + return RedirectToAction("ListUser", "User"); + } + public async Task ListUser() + { + var users = await dbContext.Users.ToListAsync(); + + return View(users); + } + + [HttpGet] + public async Task EditUser (Guid id) + { + var user = await dbContext.Users.FindAsync(id); + + return View(user); + } + + [HttpPost] + public async Task EditUser (User viewModel) + { + var user = await dbContext.Users.FindAsync(viewModel.UserId); + + if (user is not null) + { + user.UserId = viewModel.UserId; + user.Username = viewModel.Username; + user.Password = viewModel.Password; + + await dbContext.SaveChangesAsync(); + } + await dbContext.SaveChangesAsync(); + return RedirectToAction("ListUser", "User"); + } + + [HttpPost] + public async Task Delete(User ViewModel) + { + var User = await dbContext.Users + .AsNoTracking() + .FirstOrDefaultAsync(x => x.UserId == ViewModel.UserId); + + if (User is not null) + { + dbContext.Users.Remove(ViewModel); + await dbContext.SaveChangesAsync(); + } + + return RedirectToAction("ListUser", "User"); + } + } +}