Beispiel #1
0
        public static Book AddBook(AddBookViewModel form)
        {
            try
            {
                var context = new ClanographyContext();
                var book = new Book();
                book.Name = form.BookName;
                book.Description = form.Description;
                book.OwnerEmail = HttpContext.Current.User.Identity.Name;
                book.CreationDate = DateTime.Now;
                book.ModifiedDate = DateTime.Now;
                context.Books.Add(book);
                context.SaveChanges();
                BookPermission bp = new BookPermission();

                //Add Self Permission
                bp.BookID = book.ID;
                bp.EmailAddress = HttpContext.Current.User.Identity.Name;
                bp.PermissionFlag = 1;
                context.BookPermissions.Add(bp);
                context.SaveChanges();
                //Add Permissions For Others
                if (!string.IsNullOrWhiteSpace(form.PeopleWithAccess))
                {
                    string[] emails = form.PeopleWithAccess.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                    foreach (var email in emails)
                    {
                        if (Helpers.Common.IsValidEmail(email))
                        {
                            //Sends An Email
                            bp = new BookPermission();
                            bp.BookID = book.ID;
                            bp.EmailAddress = email;
                            bp.PermissionFlag = 1;
                            context.BookPermissions.Add(bp);
                        }
                    }
                    context.SaveChanges();
                }
                return book;
            }
            catch (Exception)
            {
                return null;
            }
        }
Beispiel #2
0
        public static bool UpdateBook(EditBookViewModel form)
        {
            try
            {
                var context = new ClanographyContext();
                Book newbook = context.Books.FirstOrDefault(i => i.ID == form.ID);
                newbook.ModifiedDate = DateTime.Now;
                newbook.ModifierEmail = HttpContext.Current.User.Identity.Name;
                newbook.Description = form.Description;
                context.SaveChanges();
                //Add Permissions For Others
                if (!string.IsNullOrWhiteSpace(form.PeopleWithAccess))
                {
                    //Clear Permissions
                    BookPermission bp = new BookPermission();
                    var bplist = context.BookPermissions.Where(i => i.BookID == form.ID);
                    foreach (var item in bplist)
                    {
                        context.BookPermissions.Remove(item);
                    }
                    //self Permission
                    bp.BookID = form.ID;
                    bp.EmailAddress = HttpContext.Current.User.Identity.Name;
                    bp.PermissionFlag = 1;
                    context.BookPermissions.Add(bp);

                    string[] emails = form.PeopleWithAccess.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                    foreach (var email in emails)
                    {
                        if (Helpers.Common.IsValidEmail(email))
                        {
                            //Sends An Email
                            bp = new BookPermission();
                            bp.BookID = form.ID;
                            bp.EmailAddress = email;
                            bp.PermissionFlag = 1;
                            context.BookPermissions.Add(bp);
                        }
                    }
                    context.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }