public string Create(Customer customer) { Result r = new Result { ErrorCode = 0, ErrorMessage = "Create customer successful." }; CustomerValidator validator = new CustomerValidator(); ValidationResult results = validator.Validate(customer); if (!results.IsValid) { r.ErrorCode = -1; r.ErrorMessage = results.Errors.First().ErrorMessage; return(JsonConvert.SerializeObject(r, _settings)); } customer.Id = Guid.NewGuid(); CustomerDS.Customers.Add(customer); CustomerDS.Serialize(DateTime.Now); r.Data = customer; // Return current customer // Broadcast to all clients CustomerHub.Instance.Clients.All.add(JsonConvert.SerializeObject(customer, _settings)); return(JsonConvert.SerializeObject(r, _settings)); }
public string Update(Customer customer) { Result r = new Result { ErrorCode = 0, ErrorMessage = "Update customer successful." }; Customer item = CustomerDS.Customers.Find(c => c.Id == customer.Id); if (customer == null) { r.ErrorCode = -1; r.ErrorMessage = "Could not find customer with id=" + customer.Id + "."; } else { CustomerValidator validator = new CustomerValidator(); ValidationResult results = validator.Validate(customer); if (!results.IsValid) { r.ErrorCode = -1; r.ErrorMessage = results.Errors.First().ErrorMessage; return(JsonConvert.SerializeObject(r, _settings)); } item.FirstName = customer.FirstName; item.LastName = customer.LastName; item.Email = customer.Email; item.Phone = customer.Phone; CustomerDS.Serialize(DateTime.Now); } r.Data = customer; // Broadcast to all clients CustomerHub.Instance.Clients.All.update(JsonConvert.SerializeObject(customer, _settings)); return(JsonConvert.SerializeObject(r, _settings)); }
public string Delete(Guid id) { Result r = new Result { ErrorCode = 0, ErrorMessage = "Delete customer successful." }; var customer = CustomerDS.Customers.First(c => c.Id == id); bool ok = (customer == null) ? false : CustomerDS.Customers.Remove(customer); CustomerDS.Serialize(DateTime.Now); if (!ok) { r.ErrorCode = -1; r.ErrorMessage = "Could not find customer with id=" + id; } r.Data = customer; // Broadcast to all clients CustomerHub.Instance.Clients.All.remove(JsonConvert.SerializeObject(customer, _settings)); return(JsonConvert.SerializeObject(r, _settings)); }