Esempio n. 1
0
        public static void SeedCustomers()
        {
            // Read the contents of the file
            string s = File.ReadAllText("..\\..\\..\\AdaMovieStoreSample\\App_Data\\customers.json");

            // Parse the contents using JSON.NET
            JArray data = (JArray)JsonConvert.DeserializeObject(s);

            CustomerRepository repository = new CustomerRepository();

            // Process the data
            foreach (JToken token in data)
            {
                Customer c = new Customer();
                c.Name = token["name"].Value<string>();
                c.RegisteredAt = token["registered_at"].Value<string>();
                c.Address = token["address"].Value<string>();
                c.City = token["city"].Value<string>();
                c.State = token["state"].Value<string>();
                c.PostalCode = token["postal_code"].Value<string>();
                c.Phone = token["phone"].Value<string>();
                c.AccountCredit = token["account_credit"].Value<decimal>();

                repository.Add(c);
            }
        }
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                Customer movie = new Customer();
                CustomerRepository r = new CustomerRepository();
                r.Remove(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Customer customer = new Customer();
                customer.Name = collection.GetValue("Name").AttemptedValue.ToString();
                customer.RegisteredAt = collection.GetValue("RegisteredAt").AttemptedValue.ToString();
                customer.Address = collection.GetValue("Address").AttemptedValue.ToString();
                customer.City= collection.GetValue("City").AttemptedValue.ToString();
                customer.State = collection.GetValue("State").AttemptedValue.ToString();
                customer.PostalCode = collection.GetValue("PostalCode").AttemptedValue.ToString();
                customer.Phone = collection.GetValue("Phone").AttemptedValue.ToString();
                customer.AccountCredit = decimal.Parse(collection.GetValue("AccountCredit").AttemptedValue.ToString());

                CustomerRepository r = new CustomerRepository();
                r.Add(customer);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public void Add(Customer customer)
        {
            db.Open();
            try
            {
                SqlCommand command = new SqlCommand(
                    "insert into customers (name, registeredAt, address, city, state, postalCode, phone, accountCredit) values (@name, @registered_at, @address, @city, @state, @postal_code, @phone, @account_credit)",
                    this.db);
                command.Parameters.AddWithValue("@name", customer.Name);
                command.Parameters.AddWithValue("@registered_at", customer.RegisteredAt);
                command.Parameters.AddWithValue("@address", customer.Address);
                command.Parameters.AddWithValue("@city", customer.City);
                command.Parameters.AddWithValue("@state", customer.State);
                command.Parameters.AddWithValue("@postal_code", customer.PostalCode);
                command.Parameters.AddWithValue("@phone", customer.Phone);
                command.Parameters.AddWithValue("@account_credit", customer.AccountCredit);

                command.ExecuteNonQuery();
            }
            finally
            {
                db.Close();
            }
        }
        public void Update(Customer customer)
        {
            db.Open();
            try
            {
                SqlCommand command = new SqlCommand(
                    "update customers set name=@name, registeredAt=@registered_at, address=@address, city=@city, state=@state, postalCode=@postal_code, phone=@phone, accountCredit=@account_credit where id=@id",
                    this.db);
                command.Parameters.AddWithValue("@name", customer.Name);
                command.Parameters.AddWithValue("@registered_at", customer.RegisteredAt);
                command.Parameters.AddWithValue("@address", customer.Address);
                command.Parameters.AddWithValue("@city", customer.City);
                command.Parameters.AddWithValue("@state", customer.State);
                command.Parameters.AddWithValue("@postal_code", customer.PostalCode);
                command.Parameters.AddWithValue("@phone", customer.Phone);
                command.Parameters.AddWithValue("@account_credit", customer.AccountCredit);
                command.Parameters.AddWithValue("@id", customer.Id);

                command.ExecuteNonQuery();
            }
            finally
            {
                db.Close();
            }
        }
 public void Save(Customer customer)
 {
     throw new NotImplementedException();
 }