Example #1
0
        public async Task <IActionResult> Edit(int id, [Bind("CustomerID,Name")] Customer customer)
        {
            if (id != customer.CustomerID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    db.Update(customer);
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Example #2
0
        public async Task <IActionResult> Create([Bind("CustomerID,AuthKey,Date")] Device device)
        {
            Device deviceBuilder = device;

            deviceBuilder.Date    = DateTime.Now;
            deviceBuilder.AuthKey = "temp";

            if (ModelState.IsValid)
            {
                // Create device in DB
                db.Add(deviceBuilder);
                await db.SaveChangesAsync();

                Device localDevice = await db.Device.LastAsync();

                // Create device in cloud
                Task <bool> t1 = cloud.CreateDevice(localDevice);
                Task.WaitAll(t1);

                // When creation is complete on cloud-side
                if (t1.Result)
                {
                    // Key from Azure IOT
                    Task <string> t2 = cloud.GetConnectionString(localDevice);
                    Task.WaitAll(t2);

                    localDevice.AuthKey = t2.Result;

                    db.Update(localDevice);
                    await db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            //Not successful
            return(RedirectToAction(nameof(Index)));
        }