Esempio n. 1
0
        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 = CrudDS<Customer>.Items.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;
                CrudDS<Customer>.Serialize(DateTime.Now);
            }

            r.Data = customer;

            // Broadcast to all clients
            CustomerHub.Instance.Clients.All.update(JsonConvert.SerializeObject(customer, _settings));

            return JsonConvert.SerializeObject(r, _settings);
        }
Esempio n. 3
0
 public void Update(Customer customer)
 {
     Clients.All.update(JsonConvert.SerializeObject(customer, _settings));
 }
Esempio n. 4
0
 public void Remove(Customer customer)
 {
     Clients.All.remove(JsonConvert.SerializeObject(customer, _settings));
 }
Esempio n. 5
0
 public void Add(Customer customer)
 {
     Clients.All.add(JsonConvert.SerializeObject(customer, _settings));
 }