示例#1
0
        public ActionResult DeleteAuthor(int id)
        {
            try
            {
                var profile = (UserProfile)Session["UserInfo"];
                if (profile == null || profile.Role == RolesCustom.USER)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                using (var db = new SDCContext())
                    using (var trans = db.Database.BeginTransaction())
                    {
                        //delete books
                        //delete book images
                        //delete author

                        var books = db.Books
                                    .Include(b => b.Pictures)
                                    .Where(b => b.Authors.Any(a => a.Id == id)).ToArray();

                        foreach (var book in books)
                        {
                            //delete book images
                            foreach (var pic in book.Pictures.ToArray())
                            {
                                //delete from s3
                                if (!String.IsNullOrEmpty(pic.Key))
                                {
                                    S3.DeleteFile(pic.Key);
                                }
                                //delete from db
                                db.BookPictures.Remove(pic);
                            }

                            //delete book
                            db.Books.Remove(book);
                        }

                        var author = db.Authors
                                     .Include(a => a.Books)
                                     .Include(a => a.Books.Select(b => b.Pictures))
                                     .First(a => a.Id == id);

                        db.Authors.Remove(author);
                        db.SaveChanges();
                        trans.Commit();
                    }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#2
0
        public ActionResult DeleteBookPicture(int id)
        {
            try
            {
                var profile = (UserProfile)this.Session["UserInfo"];

                using (var db = new SDCContext())
                    using (var trans = db.Database.BeginTransaction())
                    {
                        var picture = db.BookPictures
                                      .Include(p => p.Book)
                                      .Include(p => p.Book.Pictures)
                                      .Include(p => p.Book.Shelf)
                                      .Include(p => p.Book.Shelf.Owner)
                                      .FirstOrDefault(p => p.Id == id);

                        if (picture != null)
                        {
                            if (picture.Book.Shelf.Owner.UserId == profile.UserId ||
                                profile.IsAdmin || profile.IsCurator)
                            {
                                picture.Book.Pictures.Remove(picture);
                                db.SaveChanges();

                                try
                                {
                                    S3.DeleteFile(picture.Key);
                                }
                                catch (Exception ex)
                                {
                                    //todo: log
                                    trans.Rollback();
                                    return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                                }
                                trans.Commit();
                            }
                            else
                            {
                                throw new Exception("Unauthorized");
                            }
                        }
                    }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                //todo: log.
                throw ex;
            }
        }
示例#3
0
        public ActionResult UploadAvatar(UserProfileViewModel model)
        {
            if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0 && model.ImageUpload.ContentLength < 1024 * 1024)
            {
                var profile = db.UserProfiles.First(p => p.UserName == User.Identity.Name);

                var customExisting = db.Avatars.FirstOrDefault(p => p.CustomForUserId == profile.UserId);
                if (customExisting != null)
                {
                    if (!String.IsNullOrEmpty(customExisting.Key))
                    {
                        S3.DeleteFile(customExisting.Key);
                    }

                    S3File f = S3.UploadUserAvatar(
                        profile.UserId.ToString(),
                        model.ImageUpload.FileName,
                        model.ImageUpload.InputStream);

                    customExisting.Url = f.Url;
                    customExisting.Key = f.Key;
                    profile.Avatar     = customExisting;
                }
                else
                {
                    var f = S3.UploadUserAvatar(
                        profile.UserId.ToString(),
                        model.ImageUpload.FileName,
                        model.ImageUpload.InputStream);

                    Avatar custom = new Avatar()
                    {
                        CustomForUserId = profile.UserId,
                        Url             = f.Url,
                        Key             = f.Url
                    };
                    db.Avatars.Add(custom);
                    profile.Avatar = custom;
                }

                db.SaveChanges();

                ((UserProfile)Session["UserInfo"]).Avatar = profile.Avatar;
            }

            return(RedirectToAction("Index"));
        }
示例#4
0
        public ActionResult DeleteBook(int deleteBookId)
        {
            using (var db = new SDCContext())
            {
                var book = db.Books
                           .Include(b => b.Pictures)
                           .Include(b => b.Shelf)
                           .Include(b => b.Shelf.Owner)
                           .FirstOrDefault(b => b.Id == deleteBookId);
                if (book != null)
                {
                    var shelfId = book.Shelf.Id;

                    // only admin, curator or shelf owner can delete it.
                    var profile = (UserProfile)Session["UserInfo"];
                    if (profile.Role == RolesCustom.ADMIN ||
                        profile.Role == RolesCustom.CURATOR ||
                        book.Shelf.Owner.UserId == profile.UserId)
                    {
                        //f**k this.
                        profile = db.UserProfiles.Find(profile.UserId);

                        //remove book images
                        foreach (var pic in book.Pictures)
                        {
                            db.BookPictures.Remove(pic);
                            S3.DeleteFile(pic.Key);
                        }

                        string shelfName = book.Shelf.Name;

                        db.Books.Remove(book);
                        db.SaveChanges();

                        //activity
                        SDC.Library.Helpers.ActivityHelper.Activity_BookRemoved(db, profile, book, shelfName);

                        return(RedirectToAction("Details", "Shelves", new { id = shelfId }));
                    }
                }
            }
            //any other case
            return(RedirectToAction("Index", "Home"));
        }