Esempio n. 1
0
        public static void updateModel(PagesContext db, string Model, int id, string data)
        {
            if (Model.Equals("Pages"))
            {
                Page change = db.Pages.Where(b => b.PageId == id).ToList().First();
                if (change == null)
                {
                    Console.WriteLine("Object not found");
                }
                else
                {
                    Page obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Page>(data);
                    if (obj.UrlName != null) change.UrlName = obj.UrlName;
                    if (obj.Title != null) change.Title = obj.Title;
                    if (obj.Description != null) change.Description = obj.Description;
                    if (obj.Content != null) change.Content = obj.Content;
                    Console.WriteLine("Page updated");
                }
            }
            else if (Model.Equals("NavLinks"))
            {
                NavLink change = db.NavLinks.Where(b => b.NavLinkId == id).ToList().First();
                if (change == null)
                {
                    Console.WriteLine("Object not found");
                }
                else
                {
                    NavLink obj = Newtonsoft.Json.JsonConvert.DeserializeObject<NavLink>(data);
                    if (obj.ParentLinkId != 0) change.ParentLinkId = obj.ParentLinkId;
                    if (obj.PageId != 0) change.PageId = obj.PageId;
                    if (obj.Title != null) change.Title = obj.Title;
                    Console.WriteLine("NavLink updated");
                }
            }
            else if (Model.Equals("RelatedPages"))
            {
                RelatedPages change = db.RelatedPages.Where(b => b.RelatedPagesId == id).ToList().First();
                if (change == null)
                {
                    Console.WriteLine("Object not found");
                }
                else
                {
                    RelatedPages obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RelatedPages>(data);
                    if (obj.Page1Id != 0) change.Page1Id = obj.Page1Id;
                    if (obj.Page2Id != 0) change.Page2Id = obj.Page2Id;
                    Console.WriteLine("RelatedPages updated");
                }

            }
            db.SaveChanges();
        }
Esempio n. 2
0
 public static void deleteModel(PagesContext db, string Model, int id)
 {
     if (Model.Equals("Pages"))
     {
         Page toRemove = db.Pages.Where(b => b.PageId == id).ToList().First();
         if (toRemove == null)
         {
             Console.WriteLine("Object not found");
         }
         else
         {
             Console.WriteLine("Page removed");
         }
         db.Pages.Remove(toRemove);
     }
     else if (Model.Equals("NavLinks"))
     {
         NavLink toRemove = db.NavLinks.Where(b => b.NavLinkId == id).ToList().First();
         if (toRemove == null)
         {
             Console.WriteLine("Object not found");
         }
         else
         {
             Console.WriteLine("NavLink removed");
         }
         db.NavLinks.Remove(toRemove);
     }
     else if (Model.Equals("RelatedPages"))
     {
         RelatedPages toRemove = db.RelatedPages.Where(b => b.RelatedPagesId == id).ToList().First();
         if (toRemove == null)
         {
             Console.WriteLine("Object not found");
         }
         else
         {
             Console.WriteLine("RelatedPages removed");
         }
         db.RelatedPages.Remove(toRemove);
     }
     db.SaveChanges();
 }
Esempio n. 3
0
        public static void Main()
        {
            //add Pages {UrlName:"example", Title:"example", Description:"example", Content:"example"}
            //update Pages 1 {UrlName:"newexample"}
            //delete Pages 1
            //list all
            using (var db = new PagesContext())
            {
                db.Database.Migrate();
                db.SaveChanges();
                Console.WriteLine("You can now work with db.");
                Console.WriteLine("Available commands: add, update, delete, list all, q to quit");
                Console.WriteLine("add Pages { UrlName: \"example\", Title: \"example\", Description: \"example\", Content: \"example\"}");
                Console.WriteLine("update Pages 1 { UrlName: \"newexample\"}");
                Console.WriteLine("delete Pages 1");
                Console.WriteLine("Enter commands:");
                string str;
                while ((str = Console.ReadLine()) != "q")
                {
                    try
                    {
                        if (str.StartsWith("add"))
                        {
                            var parts = str.Split(' ');
                            string model = parts[1];
                            string json = str.Substring(parts[1].Length + 5);
                            addModel(db, model, json);
                        }
                        else if (str.StartsWith("update"))
                        {
                            var parts = str.Split(' ');
                            string model = parts[1];
                            int id = Convert.ToInt32(parts[2]);
                            string json = str.Substring(parts[1].Length + parts[2].Length + 9);
                            updateModel(db, model, id, json);
                        }
                        else if (str.StartsWith("delete"))
                        {
                            var parts = str.Split(' ');
                            string model = parts[1];
                            int id = Convert.ToInt32(parts[2]);
                            deleteModel(db, model, id);
                        }
                        else if (str.StartsWith("list"))
                        {
                            Console.WriteLine("Pages:");
                            foreach (var i in db.Pages)
                            {

                                Console.WriteLine(i.ToString() + " AddedDate: " + db.Entry(i).Property("AddedDate").CurrentValue.ToString());
                            }
                            Console.WriteLine();
                            Console.WriteLine("NavLinks:");
                            foreach (var i in db.NavLinks)
                            {
                                Console.WriteLine(i.ToString());
                            }
                            Console.WriteLine();
                            Console.WriteLine("RelatedPages:");
                            foreach (var i in db.RelatedPages)
                            {
                                Console.WriteLine(i.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine("Wrong input");
                        }
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Wrong input");
                    }
                }
            }
        }
Esempio n. 4
0
        public static void addModel(PagesContext db, string Model, string data)
        {
            if (Model.Equals("Pages"))
            {
                Page obj = null;
                try
                {
                    obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Page>(data);
                }
                catch(Exception e)
                {
                    Console.WriteLine("Wrong input");
                    return;
                }
                db.Pages.Add(obj);
                db.SaveChanges();
                Console.WriteLine("Page added, id: " + obj.PageId);
            }
            else if (Model.Equals("NavLinks"))
            {
                NavLink obj = null;
                try
                {
                    obj = Newtonsoft.Json.JsonConvert.DeserializeObject<NavLink>(data);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Wrong input");
                    return;
                }
                db.NavLinks.Add(obj);
                db.SaveChanges();
                Console.WriteLine("NavLink added, id: " + obj.NavLinkId);
            }
            else if (Model.Equals("RelatedPages"))
            {
               RelatedPages obj = null;
                try
                {
                    obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RelatedPages>(data);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Wrong input");
                    return;
                }
                if (obj.Page1Id == 0 || obj.Page2Id == 0)
                {
                    Console.WriteLine("Wrong data in json, try again");
                }
                else
                {
                    db.RelatedPages.Add(obj);
                    db.SaveChanges();
                    Console.WriteLine("RelatedPages added, id: " + obj.RelatedPagesId);

                }
            }
        }