Initial commit
This commit is contained in:
91
Pages/Index.cshtml
Normal file
91
Pages/Index.cshtml
Normal file
@@ -0,0 +1,91 @@
|
||||
@page
|
||||
@model Workshop8.Pages.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "Clientes";
|
||||
}
|
||||
<div class="d-flex justify-content-between align-items-center my-3">
|
||||
<h2>Clientes</h2>
|
||||
<button class="btn btn-primary" type="button" id="createClientBtn">Adicionar Cliente</button>
|
||||
</div>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th><th>Nome</th><th>Email</th><th>Criado Em</th><th>Ações</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach(var client in Model.Clientes)
|
||||
{
|
||||
<tr>
|
||||
<td>@client.Id</td>
|
||||
<td>@client.Nome</td>
|
||||
<td>@client.Email</td>
|
||||
<td>@client.CriadoEm</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-primary"
|
||||
onclick="openEditModal(@client.Id,'@client.Nome','@client.Email')">
|
||||
Editar
|
||||
</button>
|
||||
<form method="post" asp-page-handler="Delete" asp-route-id="@client.Id"
|
||||
style="display:inline;">
|
||||
<button type="submit" class="btn btn-sm btn-danger">Excluir</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="clientModal" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<form id="clientForm" method="post" action="?handler=Create">
|
||||
@Html.AntiForgeryToken()
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="modalTitle">Adicionar Cliente</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<input type="hidden" asp-for="Input.Id" />
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Nome" class="form-label"></label>
|
||||
<input asp-for="Input.Nome" class="form-control" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Email" class="form-label"></label>
|
||||
<input asp-for="Input.Email" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" class="btn btn-success">Salvar</button>
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
var clientModal = new bootstrap.Modal(document.getElementById('clientModal'));
|
||||
document.getElementById('createClientBtn').addEventListener('click', function () {
|
||||
document.getElementById('modalTitle').innerText = 'Adicionar Cliente';
|
||||
var form = document.getElementById('clientForm');
|
||||
form.action = '?handler=Create';
|
||||
form.querySelector('input[name="Input.Id"]').value = 0;
|
||||
form.querySelector('input[name="Input.Nome"]').value = '';
|
||||
form.querySelector('input[name="Input.Email"]').value = '';
|
||||
clientModal.show();
|
||||
});
|
||||
function openEditModal(id, nome, email) {
|
||||
document.getElementById('modalTitle').innerText = 'Editar Cliente';
|
||||
var form = document.getElementById('clientForm');
|
||||
form.action = '?handler=Edit';
|
||||
form.querySelector('input[name="Input.Id"]').value = id;
|
||||
form.querySelector('input[name="Input.Nome"]').value = nome;
|
||||
form.querySelector('input[name="Input.Email"]').value = email;
|
||||
clientModal.show();
|
||||
}
|
||||
</script>
|
||||
}
|
56
Pages/Index.cshtml.cs
Normal file
56
Pages/Index.cshtml.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Workshop8.Data;
|
||||
using Workshop8.Infrastructure;
|
||||
using Workshop8.Models;
|
||||
|
||||
namespace Workshop8.Pages
|
||||
{
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
private readonly IClientRepository _repository;
|
||||
private readonly LogService _logService;
|
||||
|
||||
public IndexModel(IClientRepository repository, LogService logService)
|
||||
{
|
||||
_repository = repository;
|
||||
_logService = logService;
|
||||
}
|
||||
|
||||
public IEnumerable<Cliente> Clientes { get; set; }
|
||||
|
||||
[BindProperty]
|
||||
public Cliente Input { get; set; }
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
Clientes = await _repository.GetAllClientsAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostCreateAsync()
|
||||
{
|
||||
if (!ModelState.IsValid) return Page();
|
||||
Input.CriadoEm = System.DateTime.Now;
|
||||
await _repository.AddClientAsync(Input);
|
||||
await _logService.WriteProviderLogAsync($"Cliente criado: {Input.Nome}");
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostEditAsync()
|
||||
{
|
||||
if (!ModelState.IsValid) return Page();
|
||||
await _repository.UpdateClientAsync(Input);
|
||||
await _logService.WriteProviderLogAsync($"Cliente editado: {Input.Id}");
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
||||
{
|
||||
await _repository.DeleteClientAsync(id);
|
||||
await _logService.WriteProviderLogAsync($"Cliente excluído: {id}");
|
||||
return RedirectToPage();
|
||||
}
|
||||
}
|
||||
}
|
48
Pages/Shared/_Layout.cshtml
Normal file
48
Pages/Shared/_Layout.cshtml
Normal file
@@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="pt-BR">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>@ViewData["Title"] - Workshop8</title>
|
||||
<!-- Bootstrap 5 CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
|
||||
<!-- Nosso CSS -->
|
||||
<link rel="stylesheet" href="~/css/site.css" />
|
||||
</head>
|
||||
<body>
|
||||
<!-- Navbar Bootstrap -->
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" asp-page="/Index">Workshop8</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
|
||||
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
|
||||
aria-expanded="false" aria-label="Alternar navegação">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a
|
||||
class="nav-link @(ViewData["Title"]?.ToString() == "Clientes" ? "active" : "")"
|
||||
asp-page="/Index">
|
||||
Clientes
|
||||
</a>
|
||||
</li>
|
||||
<!-- adicione mais itens de menu aqui -->
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Conteúdo principal -->
|
||||
<div class="container mt-4">
|
||||
@RenderBody()
|
||||
</div>
|
||||
|
||||
<!-- Bootstrap 5 Bundle (JS + Popper) -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
|
||||
|
||||
@RenderSection("Scripts", required: false)
|
||||
</body>
|
||||
</html>
|
3
Pages/_ViewImports.cshtml
Normal file
3
Pages/_ViewImports.cshtml
Normal file
@@ -0,0 +1,3 @@
|
||||
@using Workshop8
|
||||
@using Workshop8.Models
|
||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
3
Pages/_ViewStart.cshtml
Normal file
3
Pages/_ViewStart.cshtml
Normal file
@@ -0,0 +1,3 @@
|
||||
@{
|
||||
Layout = "_Layout";
|
||||
}
|
Reference in New Issue
Block a user