Example #1
0
        public ActionResult Edit(FormCollection form)
        {
            var appToAdd = gadgetDB.CreateObject <App>(); //new SampleWeb.Models.App();

            // Deserialize (Include white list!)
            TryUpdateModel(appToAdd, new string[] { "Name", "SourceUrl" }, form.ToValueProvider());

            // Validate
            if (String.IsNullOrEmpty(appToAdd.Name))
            {
                ModelState.AddModelError("Name", "Name required!");
            }
            if (String.IsNullOrEmpty(appToAdd.SourceUrl))
            {
                ModelState.AddModelError("SourceUrl", "SourceUrl is required!");
            }

            if (!string.IsNullOrEmpty(appToAdd.SourceUrl))
            {
                HttpWebRequest gadgetRequest = HttpWebRequest.Create(appToAdd.SourceUrl) as HttpWebRequest;

                HttpWebResponse gadgetResponse = gadgetRequest.GetResponse() as HttpWebResponse;

                string gadgetString = null;
                using (Stream stream = gadgetResponse.GetResponseStream())
                {
                    StreamReader sr = new StreamReader(stream);
                    gadgetString = sr.ReadToEnd();
                    sr.Close();
                }
            }


            bool ok = false;

            EntityKey key = null;

            // If valid, save movie to database
            if (ModelState.IsValid)
            {
                gadgetDB.AddToApps(appToAdd);
                gadgetDB.SaveChanges();
                key = gadgetDB.CreateEntityKey("SampleWeb.Models.App", appToAdd);
                ok  = true;
            }



            if (ok)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                // Otherwise, reshow form
                return(View(appToAdd));
            }
        }
Example #2
0
        public ActionResult Create(FormCollection form)
        {
            try
            {
                var appToAdd = gadgetDB.CreateObject <App>(); //new SampleWeb.Models.App();

                // Deserialize (Include white list!)
                TryUpdateModel(appToAdd, new string[] { "Name", "SourceUrl" }, form.ToValueProvider());

                appToAdd.CreateDate = DateTime.UtcNow;
                appToAdd.UpdateDate = DateTime.UtcNow;
                // Validate
                if (String.IsNullOrEmpty(appToAdd.Name))
                {
                    ModelState.AddModelError("Name", "Name required!");
                }
                if (String.IsNullOrEmpty(appToAdd.SourceUrl))
                {
                    ModelState.AddModelError("SourceUrl", "SourceUrl is required!");
                }

                // If valid, save movie to database
                if (ModelState.IsValid)
                {
                    gadgetDB.AddToApps(appToAdd);
                    gadgetDB.SaveChanges();
                }
                else
                {
                    return(View(appToAdd));
                }

                Gadget gadget = gadgetDB.CreateObject <Gadget>();
                gadget.AppID      = appToAdd.AppId;
                gadget.SourceUrl  = appToAdd.SourceUrl;
                gadget.CreateDate = appToAdd.CreateDate;
                gadget.UpdateDate = appToAdd.UpdateDate;

                gadgetDB.AddToGadgets(gadget);
                gadgetDB.SaveChanges();

                appToAdd.LatestGadgetID = gadget.GadgetID;


                GadgetMaster gMaster = GadgetManager.FetchGadget(appToAdd.SourceUrl);

                GadgetContent gContent = gadgetDB.CreateObject <GadgetContent>();
                gContent.GadgetID     = gadget.GadgetID;
                gContent.CreateDate   = appToAdd.CreateDate;
                gContent.UpdateDate   = appToAdd.UpdateDate;
                gContent.RecordTypeID = RecordTypeValues.GadgetXML;
                gContent.Content      = gMaster.RawTag;

                gadgetDB.AddToGadgetContents(gContent);
                gadgetDB.SaveChanges();

                if (gMaster.HasExternalMessageBundles())
                {
                    gContent = gadgetDB.CreateObject <GadgetContent>();
                    gContent.RecordTypeID = RecordTypeValues.MessageBundle;
                    gContent.CreateDate   = appToAdd.CreateDate;
                    gContent.UpdateDate   = appToAdd.UpdateDate;
                    gContent.Content      = gMaster.GetConsolidatedMessageBundles();
                    gadgetDB.AddToGadgetContents(gContent);
                    gadgetDB.SaveChanges();
                }

                appToAdd.LatestGadgetID = gadget.GadgetID;
                gadgetDB.SaveChanges();

                return(RedirectToAction("Details/" + appToAdd.AppId.ToString()));
            }
            catch
            {
                return(View());
            }
        }