Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Crud_Carros/Controllers/CarController.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion Crud_Carros/Controllers/ClientController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public async Task<IActionResult> AddClient()
return View();
}


[HttpPost]
public async Task<IActionResult> AddClient(AddClientViewModel viewModel)
{

var client = new Client
{
Name_Client = viewModel.Name_Client,
Expand All @@ -45,6 +45,7 @@ public async Task<IActionResult> AddClient(AddClientViewModel viewModel)
return RedirectToAction ("ListClient", "Client");
}


[HttpGet]

public async Task<IActionResult> ListClient()
Expand Down
4 changes: 2 additions & 2 deletions Crud_Carros/Controllers/ClientOfStaffController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public async Task<IActionResult> AddClientOfStaff()
public async Task<IActionResult> 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
{
Expand Down
42 changes: 42 additions & 0 deletions Crud_Carros/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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);
}
}
}
4 changes: 2 additions & 2 deletions Crud_Carros/Controllers/ModelOfCarController.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
87 changes: 87 additions & 0 deletions Crud_Carros/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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<IActionResult> ListUser()
{
var users = await dbContext.Users.ToListAsync();

return View(users);
}

[HttpGet]
public async Task<IActionResult> EditUser (Guid id)
{
var user = await dbContext.Users.FindAsync(id);

return View(user);
}

[HttpPost]
public async Task<IActionResult> 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<IActionResult> 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");
}
}
}