Skip to content

Instantly share code, notes, and snippets.

@alanwillms
Last active September 30, 2017 13:31
Show Gist options
  • Save alanwillms/bb47fa99381747aac88385023f8f8925 to your computer and use it in GitHub Desktop.
Save alanwillms/bb47fa99381747aac88385023f8f8925 to your computer and use it in GitHub Desktop.
Exercicio Aula C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using PosGraduacao.Models;
namespace PosGraduacao.Controllers
{
public class ClientController : Controller
{
public ActionResult Index()
{
var clients = Client.GetClientes();
return View (clients);
}
public ActionResult Create()
{
ViewBag.Submitted = false;
var created = false;
if (HttpContext.Request.RequestType == "POST") {
ViewBag.Submitted = true;
var id = Request.Form["id"];
var nome = Request.Form["nome"];
var endereco = Request.Form["endereco"];
Client client = new Client()
{
ID = Convert.ToInt16(id),
Nome = nome,
Endereco = endereco
};
var ClientFile = Client.ClientFile;
var ClientData = System.IO.File.ReadAllText(ClientFile);
List<Client> ClientList = new List<Client>();
ClientList = JsonConvert.DeserializeObject<List<Client>>(ClientData);
if (ClientList == null) {
ClientList = new List<Client>();
}
ClientList.Add(client);
System.IO.File.WriteAllText(ClientFile, JsonConvert.SerializeObject(ClientList));
created = true;
}
if (created) {
ViewBag.Message = "Cliente criado com sucesso.";
} else {
ViewBag.Message = "Erro ao cadastrar o cliente.";
}
return View();
}
public ActionResult Update(int id)
{
if (HttpContext.Request.RequestType == "POST") {
var nome = Request.Form["nome"];
var endereco = Request.Form["endereco"];
var clients = Client.GetClientes();
foreach (Client client in clients) {
if (client.ID == id) {
client.Nome = nome;
client.Endereco = endereco;
break;
}
}
System.IO.File.WriteAllText(
Client.ClientFile,
JsonConvert.SerializeObject(clients)
);
Response.Redirect("~/Client/Index?Message=Client_Updated");
}
var clnt = new Client();
var clints = Client.GetClientes();
foreach (Client client in clints) {
if (client.ID == id) {
clnt = client;
break;
}
}
if (clnt == null) {
ViewBag.Message = "Cliente não encontrado";
}
return View(clnt);
}
public ActionResult Delete(int id)
{
var clients = Client.GetClientes();
var deleted = false;
foreach (Client client in clients) {
if (client.ID == id) {
var index = clients.IndexOf(client);
clients.RemoveAt(index);
System.IO.File.WriteAllText(
Client.ClientFile,
JsonConvert.SerializeObject(clients)
);
deleted = true;
break;
}
}
if (deleted) {
ViewBag.Message = "Cliente deletado com sucesso.";
} else {
ViewBag.Message = "Erro ao deletar cliente.";
}
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Newtonsoft.Json;
namespace PosGraduacao.Models
{
public class Client
{
public static string ClientFile = HttpContext.Current.Server.MapPath(
"~/App_Data/Clients.json"
);
public int ID { get; set; }
public string Nome { get; set; }
public string Endereco { get; set; }
public static List<Client> GetClientes()
{
List<Client> clients = new List<Client>();
if (File.Exists(ClientFile)) {
string content = File.ReadAllText(ClientFile);
clients = JsonConvert.DeserializeObject<List<Client>>(content);
} else {
File.Create(ClientFile).Close();
File.WriteAllText(ClientFile, "[]");
}
return clients;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment