Beispiel #1
0
        public ActionResult Add(FormCollection form)
        {
            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!");
            }

            string gadgetString = null;

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

                HttpWebResponse gadgetResponse = gadgetRequest.GetResponse() as HttpWebResponse;


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

            bool ok = false;

            // If valid, save movie to database
            if (ModelState.IsValid)
            {
                gadgetDB.AddToApps(appToAdd);
                gadgetDB.SaveChanges();
                ok = true;
            }
            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;

            GadgetContent gContent = gadgetDB.CreateObject <GadgetContent>();

            gContent.GadgetID     = gadget.GadgetID;
            gContent.CreateDate   = appToAdd.CreateDate;
            gContent.UpdateDate   = appToAdd.UpdateDate;
            gContent.RecordTypeID = 1;
            gContent.Content      = gadgetString;

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

            if (ok)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                // Otherwise, reshow form
                return(View(appToAdd));
            }
        }
Beispiel #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());
            }
        }