Ejemplo n.º 1
0
        public async Task <IActionResult> AddBook(BookModel bookModel)
        {
            // create new book
            if (ModelState.IsValid)
            {
                if (bookModel.CoverPhoto != null)
                {
                    string folder = "books/coverImg/";
                    bookModel.CoverImageUrl = await UploadImage(folder, bookModel.CoverPhoto);
                }

                if (bookModel.BookPdf != null)
                {
                    string folder = "books/pdfFile/";
                    bookModel.BookPdfUrl = await UploadImage(folder, bookModel.BookPdf);
                }

                int id = await _bookRepository.AddBookAsync(bookModel);

                if (id > 0)
                {
                    ViewBag.IsSuccess = true;
                    ModelState.Clear();
                    return(View());
                }
            }

            ModelState.AddModelError("", "Something is wrong!");

            return(View(bookModel));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Title,NumPag,AuthorId")] Book book)
        {
            if (ModelState.IsValid)
            {
                await bookRepository.AddBookAsync(book);

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorId"] = new SelectList(bookRepository.GetAuthors(), "Id", "Id", book.AuthorId);
            return(View(book));
        }
Ejemplo n.º 3
0
 public async Task <int> AddBook(string name, string author, string genre)
 {
     return(await _bookRepository.AddBookAsync(name, author, genre));
 }
Ejemplo n.º 4
0
 public async Task AddBookAsync(Book book)
 {
     await bookRepository.AddBookAsync(book);
 }
Ejemplo n.º 5
0
        public GraphQL4BooksMutation(ReviewRepository reviewRepository,
                                     BookRepository bookRepository,
                                     AuthorRepository authorRepository,
                                     UserRepository userRepository)
        {
            FieldAsync <ReviewType>(
                "postReview",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ReviewInputType> > {
                Name = "review"
            }
                    ),
                resolve: async context => {
                var review = context.GetArgument <Review>("review");
                return(await context.TryAsyncResolve(async c => await reviewRepository.AddReviewAsync(review)));
            }
                );

            FieldAsync <ReviewType>(
                "putReview",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ReviewInputType> > {
                Name = "review"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "reviewId"
            }
                    ),
                resolve: async context => {
                var review   = context.GetArgument <Review>("review");
                var reviewId = context.GetArgument <Guid>("reviewId");

                var oldReview = await reviewRepository.GetByIdAsync(reviewId);

                return(await context.TryAsyncResolve(async c => {
                    return await reviewRepository.UpdateReviewAsync(review, oldReview);
                }));
            }
                );

            FieldAsync <BookType>(
                "postBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            }
                    ),
                resolve: async context => {
                var book = context.GetArgument <Book>("book");
                return(await context.TryAsyncResolve(async c => await bookRepository.AddBookAsync(book)));
            }
                );

            FieldAsync <BookType>(
                "putBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "bookId"
            }
                    ),
                resolve: async context => {
                var book   = context.GetArgument <Book>("book");
                var bookId = context.GetArgument <Guid>("bookId");

                var oldBook = await bookRepository.GetByIdAsync(bookId);

                return(await context.TryAsyncResolve(async c => {
                    return await bookRepository.UpdateBookAsync(book, oldBook);
                }));
            }
                );

            FieldAsync <AuthorType>(
                "postAuthor",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AuthorInputType> > {
                Name = "author"
            }
                    ),
                resolve: async context => {
                var author = context.GetArgument <Author>("author");
                return(await context.TryAsyncResolve(async c => await authorRepository.AddAuthorAsync(author)));
            }
                );

            FieldAsync <AuthorType>(
                "putAuthor",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AuthorInputType> > {
                Name = "author"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "authorId"
            }
                    ),
                resolve: async context => {
                var author   = context.GetArgument <Author>("author");
                var authorId = context.GetArgument <Guid>("authorId");

                var oldAuthor = await authorRepository.GetByIdAsync(authorId);

                return(await context.TryAsyncResolve(async c => {
                    return await authorRepository.UpdateAuthorAsync(author, oldAuthor);
                }));
            }
                );

            FieldAsync <UserType>(
                "postUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            }
                    ),
                resolve: async context => {
                var user = context.GetArgument <User>("user");
                return(await context.TryAsyncResolve(async c => await userRepository.AddUserAsync(user)));
            }
                );

            FieldAsync <UserType>(
                "putUser",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <UserInputType> > {
                Name = "user"
            },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "userId"
            }
                    ),
                resolve: async context => {
                var user   = context.GetArgument <User>("user");
                var userId = context.GetArgument <Guid>("userId");

                var oldUser = await userRepository.GetByIdAsync(userId);

                return(await context.TryAsyncResolve(async c => {
                    return await userRepository.UpdateUserAsync(user, oldUser);
                }));
            }
                );
        }
Ejemplo n.º 6
0
    public class BookFacade : RestService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Facade.Book.BookFacade"/> class.
        /// </summary>
        /// <param name="uri">URI.</param>
        public BookFacade(string uri, string dbName): base(uri, dbName)
        {
        }

        /// <summary>
        /// Inserts the book.
        /// </summary>
        /// <returns>Result of execution.</returns>
        /// <param name="book">Book.</param>
        public async Task<bool> InsertBook(Book book)
        {
            try
            {
                if (base.IsConnected())
                {
                    var uri = new Uri(string.Format(base.Uri, string.Empty));
                    var json = JsonConvert.SerializeObject(book);
                    var content = new StringContent(json, Encoding.UTF8, "application/json");

                    HttpResponseMessage response = await client.PostAsync(uri + "/books", content);

                    if (response.IsSuccessStatusCode)
                    {
                        var resul =
                            JsonConvert.DeserializeObject<Result>(response.Content.ToString());
                        Debug.WriteLine($"Book Saved correctly. resul: {resul.Data} ");
                    }

                    return response.IsSuccessStatusCode;
                }
                else
                {
                    var connection = DependencyService.Get<IConnection>();
                    BookRepository repor = new BookRepository(connection.GetFilePath(base.DBName));
                    await repor.AddBookAsync(book);
                    return true;
                }
            }
            catch(Exception ex)
            {
                Utilidades.Log.Log.RecordLog(ex, TypeLog.Error, Pilicy.Client);
                throw ex;
            }
        }

        /// <summary>
        /// Finds all books.
        /// </summary>
        /// <returns>The all books.</returns>
        public async Task<List<Book>> FindAllBooks()
        {
            List<Book> books = new List<Book>();
            try
            {
                if (base.IsConnected())
                {                    var uri = new Uri($"{base.Uri}/books");