Example #1
0
        public ActionResult Edit(int BId, string professionTags, string UserTags, string filesToUpload)
        {
            var book = unitOfWork.BookRepository.GetByID(BId);

            if (!book.Users.Any(u => AuthorizationHelper.isRelevant(u.UserId)))
            {
                throw new JsonCustomException(ControllerError.ajaxError);
            }
            if (TryUpdateModel(book, "", new[] { "title", "abtrct", "print", "translator", "writer" }))
            {
                UpdSertBookProffs(professionTags, book);
                if (!AuthorizationHelper.isAdmin())
                {
                    UpSertBookUsers(UserTags, book);
                }
                unitOfWork.BookRepository.Update(book);
                unitOfWork.Save();

                var filesString = filesToUpload.Split(',');
                switch (filesString.Count())
                {
                case 0:
                    throw new JsonCustomException(ControllerError.bookMustUpload);

                case 1:
                    if (!UploadHelper.CheckExtenstionFast(filesString.First(), UploadHelper.FileTypes.doc))
                    {
                        throw new JsonCustomException(ControllerError.bookMustUploadOneDoc);
                    }
                    break;

                case 2:
                    if (!(filesString.Any(i => UploadHelper.CheckExtenstionFast(i, UploadHelper.FileTypes.doc)) &&
                          filesString.Any(i => UploadHelper.CheckExtenstionFast(i, UploadHelper.FileTypes.image))))
                    {
                        throw new JsonCustomException(ControllerError.bookMustUploadOneDocAndImage);
                    }
                    break;

                default:
                    throw new JsonCustomException(ControllerError.fileCountExceeded);
                }

                var fileUploadResult = UploadHelper.UpdateUploadedFiles(filesToUpload, null, "Book");
                book.image = fileUploadResult.ImagesToUpload;
                book.file  = fileUploadResult.DocsToUpload;

                unitOfWork.BookRepository.Update(book);
                unitOfWork.Save();
                return(Json(new { URL = Url.Action("Detail", "Book", new { BId = book.BookId, BName = StringHelper.URLName(book.title) }) }));
            }
            throw new ModelStateException(this.ModelState);
        }
Example #2
0
        public ActionResult Create([Bind(Include = "title,abtrct,print,translator,writer")] Book book, string professionTags, string UserTags, string filesToUpload)
        {
            if (ModelState.IsValid)
            {
                book.date = DateTime.UtcNow;
                unitOfWork.BookRepository.Insert(book);
                UpdSertBookProffs(professionTags, book);
                UpSertBookUsers(UserTags, book);

                var filesString = filesToUpload.Split(',');
                switch (filesString.Count())
                {
                case 0:
                    throw new JsonCustomException(ControllerError.bookMustUpload);

                case 1:
                    if (!UploadHelper.CheckExtenstionFast(filesString.First(), UploadHelper.FileTypes.doc))
                    {
                        throw new JsonCustomException(ControllerError.bookMustUploadOneDoc);
                    }
                    break;

                case 2:
                    if (!(filesString.Any(i => UploadHelper.CheckExtenstionFast(i, UploadHelper.FileTypes.doc)) &&
                          filesString.Any(i => UploadHelper.CheckExtenstionFast(i, UploadHelper.FileTypes.image))))
                    {
                        throw new JsonCustomException(ControllerError.bookMustUploadOneDocAndImage);
                    }
                    break;

                default:
                    throw new JsonCustomException(ControllerError.fileCountExceeded);
                }

                var fileUploadResult = UploadHelper.UpdateUploadedFiles(filesToUpload, null, "Book");
                book.image = fileUploadResult.ImagesToUpload;
                book.file  = fileUploadResult.DocsToUpload;

                unitOfWork.BookRepository.Update(book);
                unitOfWork.Save();

                foreach (var user in book.Users)
                {
                    FeedHelper.FeedInsert(FeedType.Book,
                                          book.BookId,
                                          user.UserId);
                }
                return(Json(new { URL = Url.Action("Detail", "Book", new { BId = book.BookId, BName = StringHelper.URLName(book.title) }) }));
            }
            throw new ModelStateException(this.ModelState);
        }