-
Notifications
You must be signed in to change notification settings - Fork 44
/
ClientesController.cs
122 lines (105 loc) · 2.87 KB
/
ClientesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Domain.Interfaces;
using Infra.Data.Context;
using Domain.Models;
namespace DevXDemoASP2.Controllers
{
public class ClientesController : Controller
{
private readonly IClienteService _clienteService;
private readonly DemoContext _context;
public ClientesController(DemoContext context, IClienteService clienteService)
{
_context = context;
_clienteService = clienteService;
}
public IActionResult Index()
{
return View(_clienteService.ObterTodos());
}
public async Task<IActionResult> Details(Guid? id)
{
if (id == null)
return NotFound();
var cliente = await _context.Clientes.SingleOrDefaultAsync(m => m.Id == id);
if (cliente == null)
return NotFound();
return View(cliente);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([Bind("Id,Nome,Email")] Cliente cliente)
{
if (ModelState.IsValid)
{
_clienteService.Adicionar(cliente);
return RedirectToAction(nameof(Index));
}
return View(cliente);
}
public async Task<IActionResult> Edit(Guid? id)
{
if (id == null)
return NotFound();
var cliente = await _context.Clientes.SingleOrDefaultAsync(m => m.Id == id);
if (cliente == null)
return NotFound();
return View(cliente);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Id,Nome,Email")] Cliente cliente)
{
if (id != cliente.Id)
return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(cliente);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ClienteExists(cliente.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(cliente);
}
public async Task<IActionResult> Delete(Guid? id)
{
if (id == null)
return NotFound();
var cliente = await _context.Clientes
.SingleOrDefaultAsync(m => m.Id == id);
if (cliente == null)
return NotFound();
return View(cliente);
}
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id)
{
var cliente = await _context.Clientes.SingleOrDefaultAsync(m => m.Id == id);
_context.Clientes.Remove(cliente);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool ClienteExists(Guid id)
{
return _context.Clientes.Any(e => e.Id == id);
}
}
}