public CustomerJson Get(HttpRequestMessage request, string name) { X509Certificate2 certificate = request.GetClientCertificate(); if (certificate.Issuer.Equals("CN=IIS Lab CARoot") && certificate.Subject.Equals("CN=AnyClientInIISLab")) { CustomerJson customer = GetCustomer(name); if (customer != null) { return(GetCustomer(name)); } var customerNotFoundEx = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No customer with name = {0}", name)), ReasonPhrase = "Customer name Not Found" }; throw new HttpResponseException(customerNotFoundEx); } var invalidCertEx = new HttpResponseMessage(HttpStatusCode.Unauthorized) { Content = new StringContent(string.Format("Unauthorized Cert")), ReasonPhrase = "Invalid Cert" }; throw new HttpResponseException(invalidCertEx); }
private void removeCustomer(CustomerJson customer) { List <CustomerJson> allCustomers = GetCustomers(); allCustomers.Remove(customer); File.WriteAllText(dbFile, JsonConvert.SerializeObject(allCustomers)); }
private void saveCustomer(CustomerJson customer) { List <CustomerJson> allCustomers = GetCustomers(); CustomerJson oldCostumer = allCustomers.Where(x => x.Customer.firstName.Equals(customer.Customer.firstName)).FirstOrDefault(); allCustomers.Remove(oldCostumer); allCustomers.Add(customer); File.WriteAllText(dbFile, JsonConvert.SerializeObject(allCustomers)); }
public void Post(Transaction transaction) { CustomerJson sender = GetCustomers().Where(x => x.Customer.firstName.Equals(transaction.sender.firstName) && x.Customer.lastName.Equals(transaction.sender.lastName)).FirstOrDefault(); CustomerJson recipient = GetCustomers().Where(x => x.Customer.firstName.Equals(transaction.recipient.firstName) && x.Customer.lastName.Equals(transaction.recipient.lastName)).FirstOrDefault(); if (sender != null && recipient != null) { sender.Customer.balance -= transaction.amount; recipient.Customer.balance += transaction.amount; } saveCustomer(sender); saveCustomer(recipient); }
// DELETE api/<controller>/5 public void Delete(string name) { try { CustomerJson rmCustomer = GetCustomers().Where(x => x.Customer.firstName.Equals(name)).FirstOrDefault(); removeCustomer(rmCustomer); } catch { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No customer with name = {0}", name)), ReasonPhrase = "Customer name Not Found" }; throw new HttpResponseException(resp); } }
public IHttpActionResult GetCustomers() { List <CustomerJson> listData = new List <CustomerJson>(); foreach (Customer customer in db.Customers) { CustomerJson j = new CustomerJson(); j.Id = customer.Id; j.AccountNumber = customer.AccountNumber; j.AccountBalance = customer.AccountBalance.ToString(); j.AccountName = customer.AccountName; j.ContactInformation = customer.ContactInformation; j.FirstName = customer.FirstName; j.LastName = customer.LastName; listData.Add(j); } return(Ok(JsonConvert.SerializeObject(listData))); }
// PUT api/<controller>/5 public void Put(CustomerJson customer) { try { List <CustomerJson> allCustomers = GetCustomers(); //CustomerJson newCustomer = JsonConvert.DeserializeObject<CustomerJson>(value); allCustomers.Add(customer); File.WriteAllText(dbFile, JsonConvert.SerializeObject(allCustomers)); } catch { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("Unable to create customer = {0}", customer)), ReasonPhrase = "Probably malformed content" }; throw new HttpResponseException(resp); } }
static void Main(string[] args) { string json = "{\"id\": \"4a17d6fe-a617-4cf8-a850-0fb6bc8576fd\",\"country\": \"DE\",\"_embedded\": {\"company\": {\"name\": \"Apple\",\"industrySector\": \"IT\",\"owner\": \"Klaus Kleber\",\"_embedded\": {\"emailAddresses\": [{\"id\": \"4a17d6fe-a617-4cf8-a850-0fb6bc8576fd\",\"value\": \"[email protected]\",\"type\": \"Business\",\"_links\": {\"self\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"}}}],\"phoneNumbers\": [{\"id\": \"4a17d6fe-a617-4cf8-a850-0fb6bc8576fd\",\"value\": \"01670000000\",\"type\": \"Business\",\"_links\": {\"self\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"}}}],},\"_links\": {\"self\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"},\"phoneNumbers\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"},\"addresses\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"},}},},\"_links\": {\"self\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"},\"legalPerson\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"},\"naturalPerson\": {\"href\": \"https://any-host.com/api/v1/customers/1234\"}}}"; CustomerJson results = JsonConvert.DeserializeObject <CustomerJson>(json); Customer customer = new Customer() { Id = results.id, Country = results.country, Company = new LegalPerson() { EmailAddresses = results._embedded.company._embedded.emailAddresses, PhoneNumbers = results._embedded.company._embedded.phoneNumbers, IndustrySector = results._embedded.company.industrySector, Name = results._embedded.company.name, Owner = results._embedded.company.owner } }; }
public IHttpActionResult GetCustomer(int id) { Customer customer = db.Customers.Find(id); if (customer == null) { return(NotFound()); } CustomerJson j = new CustomerJson(); j.Id = customer.Id; j.AccountNumber = customer.AccountNumber; j.AccountBalance = customer.AccountBalance.ToString(); j.AccountName = customer.AccountName; j.ContactInformation = customer.ContactInformation; j.FirstName = customer.FirstName; j.LastName = customer.LastName; return(Ok(JsonConvert.SerializeObject(j))); }
public JsonResult getCustomers(Int32?customerId) { var list = CustomerJson.from(context.Customer.ToList()); return(Json(new { res = list }, JsonRequestBehavior.AllowGet)); }
public static List <Customer> GetCustomersList() { CustomerJson cs = JsonConvert.DeserializeObject <CustomerJson>(CustomersJSON); return(cs.Customers); }