Exemplo n.º 1
0
        public BookmarkItemView(BookmarksDBEntities DB, Bookmark bookmark, User viewer)
        {
            Id   = bookmark.Id;
            Name = bookmark.Name;
            Url  = bookmark.Url;
            if (bookmark.CategoryId != 0)
            {
                Category category = DB.Categories.Find(bookmark.CategoryId);
                if (category != null)
                {
                    Category = category.Name;
                }
            }
            else
            {
                Category = "";
            }

            AllowEdit = (viewer.Admin) || (bookmark.UserId == viewer.Id);
            if (bookmark.UserId == viewer.Id)
            {
                OwnerShip  = (bookmark.Shared ? "Shared" : "Private");
                OwnerEmail = "";
            }
            else
            {
                User ownerUser = DB.Users.Find(bookmark.UserId);
                if (ownerUser != null)
                {
                    OwnerShip  = ownerUser.GetFullName();
                    OwnerEmail = ownerUser.Email;
                }
            }
        }
Exemplo n.º 2
0
        public static List <BookmarkItemView> BookmarkList(this BookmarksDBEntities DB, User viewer, string SortBy, bool ascendant)
        {
            List <BookmarkItemView> bookmarkItems = new List <BookmarkItemView>();

            foreach (Bookmark bookmark in DB.Bookmarks)
            {
                if ((viewer.Admin) || (viewer.Id == bookmark.UserId) || (bookmark.Shared))
                {
                    bookmarkItems.Add(new BookmarkItemView(DB, bookmark, viewer));
                }
            }
            switch (SortBy)
            {
            case "Name":
                if (ascendant)
                {
                    bookmarkItems = bookmarkItems.OrderBy(b => b.Name).ToList();
                }
                else
                {
                    bookmarkItems = bookmarkItems.OrderByDescending(b => b.Name).ToList();
                }
                break;

            case "Url":
                if (ascendant)
                {
                    bookmarkItems = bookmarkItems.OrderBy(b => b.Url).ToList();
                }
                else
                {
                    bookmarkItems = bookmarkItems.OrderByDescending(b => b.Url).ToList();
                }
                break;

            case "OwnerShip":
                if (ascendant)
                {
                    bookmarkItems = bookmarkItems.OrderBy(b => b.OwnerShip).ToList();
                }
                else
                {
                    bookmarkItems = bookmarkItems.OrderByDescending(b => b.OwnerShip).ToList();
                }
                break;

            case "Category":
                if (ascendant)
                {
                    bookmarkItems = bookmarkItems.OrderBy(b => b.Category).ToList();
                }
                else
                {
                    bookmarkItems = bookmarkItems.OrderByDescending(b => b.Category).ToList();
                }
                break;
            }

            return(bookmarkItems);
        }
Exemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Users CRUD queries

        public static User Add(this BookmarksDBEntities DB, User user)
        {
            if (user != null)
            {
                User new_User = DB.Users.Add(user);
                DB.SaveChanges();
                return(new_User);
            }
            return(null);
        }
Exemplo n.º 4
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Categories CRUD queries

        public static Category Add(this BookmarksDBEntities DB, Category category)
        {
            if (category != null)
            {
                Category new_Category = DB.Categories.Add(category);
                DB.SaveChanges();
                return(new_Category);
            }
            return(null);
        }
Exemplo n.º 5
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Bookmarks CRUD queries

        public static Bookmark Add(this BookmarksDBEntities DB, Bookmark bookmark)
        {
            if (bookmark != null)
            {
                Bookmark new_Bookmark = DB.Bookmarks.Add(bookmark);
                DB.SaveChanges();
                return(new_Bookmark);
            }
            return(null);
        }
Exemplo n.º 6
0
        public static List <User> SortedUsers(this BookmarksDBEntities DB, string sortBy, bool ascendant)
        {
            List <User> users = DB.Users.ToList();

            if (users.Count > 1)
            {
                switch (sortBy)
                {
                case "Name":
                    if (ascendant)
                    {
                        users = users.OrderBy(b => b.FirstName).ThenBy(b => b.LastName).ToList();
                    }
                    else
                    {
                        users = users.OrderByDescending(b => b.FirstName).ThenByDescending(b => b.LastName).ToList();
                    }
                    break;

                case "CreationDate":
                    if (ascendant)
                    {
                        users = users.OrderBy(b => b.CreationDate).ToList();
                    }
                    else
                    {
                        users = users.OrderByDescending(b => b.CreationDate).ToList();
                    }
                    break;

                case "BirthDate":
                    if (ascendant)
                    {
                        users = users.OrderBy(b => b.BirthDate).ToList();
                    }
                    else
                    {
                        users = users.OrderByDescending(b => b.BirthDate).ToList();
                    }
                    break;

                case "Sex":
                    if (ascendant)
                    {
                        users = users.OrderBy(b => b.Sex).ToList();
                    }
                    else
                    {
                        users = users.OrderByDescending(b => b.Sex).ToList();
                    }
                    break;
                }
            }
            return(users);
        }
Exemplo n.º 7
0
 public static bool CategoryExist(this BookmarksDBEntities DB, string name)
 {
     foreach (Category category in DB.Categories)
     {
         if (category.Name == name)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 8
0
 public static void Delete(this BookmarksDBEntities DB, Bookmark bookmark)
 {
     if (bookmark != null)
     {
         Bookmark bookmarkToDelete = DB.Bookmarks.Find(bookmark.Id);
         if (bookmarkToDelete != null)
         {
             DB.Entry(bookmarkToDelete).State = System.Data.Entity.EntityState.Deleted;
             DB.SaveChanges();
         }
     }
 }
Exemplo n.º 9
0
 public static User Update(this BookmarksDBEntities DB, User user)
 {
     if (user != null)
     {
         User userToUpdate = DB.Users.Find(user.Id);
         if (userToUpdate != null)
         {
             userToUpdate.Update(user);
             DB.Entry(userToUpdate).State = System.Data.Entity.EntityState.Modified;
             DB.SaveChanges();
             return(DB.Users.Find(userToUpdate.Id));
         }
     }
     return(null);
 }
Exemplo n.º 10
0
 public static Category Update(this BookmarksDBEntities DB, Category category)
 {
     if (category != null)
     {
         Category categoryToUpdate = DB.Categories.Find(category.Id);
         if (categoryToUpdate != null)
         {
             categoryToUpdate.Update(category);
             DB.Entry(categoryToUpdate).State = System.Data.Entity.EntityState.Modified;
             DB.SaveChanges();
             return(DB.Categories.Find(categoryToUpdate.Id));
         }
     }
     return(null);
 }
Exemplo n.º 11
0
 public static Bookmark Update(this BookmarksDBEntities DB, Bookmark bookmark)
 {
     if (bookmark != null)
     {
         Bookmark bookmarkToUpdate = DB.Bookmarks.Find(bookmark.Id);
         if (bookmarkToUpdate != null)
         {
             bookmarkToUpdate.Update(bookmark);
             DB.Entry(bookmarkToUpdate).State = System.Data.Entity.EntityState.Modified;
             DB.SaveChanges();
             return(DB.Bookmarks.Find(bookmarkToUpdate.Id));
         }
     }
     return(null);
 }
Exemplo n.º 12
0
 public static void Delete(this BookmarksDBEntities DB, User user)
 {
     if (user != null)
     {
         User userToDelete = DB.Users.Find(user.Id);
         if (userToDelete != null)
         {
             foreach (Bookmark bookmark in DB.Bookmarks.Where(b => b.UserId == user.Id))
             {
                 DB.Entry(bookmark).State = System.Data.Entity.EntityState.Deleted;
             }
             DB.Entry(userToDelete).State = System.Data.Entity.EntityState.Deleted;
             DB.SaveChanges();
         }
     }
 }
Exemplo n.º 13
0
 public static void Delete(this BookmarksDBEntities DB, Category category)
 {
     if (category != null)
     {
         Category categoryToDelete = DB.Categories.Find(category.Id);
         if (categoryToDelete != null)
         {
             foreach (Bookmark bookmark in DB.Bookmarks.Where(b => b.CategoryId == categoryToDelete.Id))
             {
                 bookmark.CategoryId      = 0;
                 DB.Entry(bookmark).State = System.Data.Entity.EntityState.Modified;
             }
             DB.Entry(categoryToDelete).State = System.Data.Entity.EntityState.Deleted;
             DB.SaveChanges();
         }
     }
 }
Exemplo n.º 14
0
 public static User FindByUserName(this BookmarksDBEntities DB, string userName)
 {
     return(DB.Users.Where(u => u.UserName == userName).FirstOrDefault());
 }