Ejemplo n.º 1
0
        public async Task <GenricResponse> MarkRead(UserBookRequest request)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <UserBooks>(Config.UserBooks);
            var            userbook   = (await collection.FindAsync(doc => doc.UserId == request.UserId)).FirstOrDefault();

            if (userbook == null)
            {
                response.Message = "User not found";
            }
            else
            {
                if (userbook.Books.Any(x => x.BookId == request.BookId))
                {
                    var book = userbook.Books.FirstOrDefault(x => x.BookId == request.BookId);
                    book.IsRead = true;
                    await collection.ReplaceOneAsync(doc => doc.Id == userbook.Id, userbook);

                    response.IsSuccesful = true;
                }
                else
                {
                    response.Message = "Book not found";
                }
            }
            return(response);
        }
        public async Task <GenricResponse> Delete(BookDeleteRequest request)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <Book>(Config.Books);
            await collection.DeleteOneAsync(doc => doc.Id == request.Id);

            response.IsSuccesful = true;
            return(response);
        }
        public async Task <GenricResponse> Put(Book book)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <Book>(Config.Books);
            await collection.ReplaceOneAsync(doc => doc.Id == book.Id, book);

            response.IsSuccesful = true;
            return(response);
        }
Ejemplo n.º 4
0
        public async Task <GenricResponse> BookReview(BookReview review)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <BookReview>(Config.BookReview);

            review.Id = review.Id == Guid.Empty ? Guid.NewGuid() : review.Id;
            await collection.InsertOneAsync(review);

            response.IsSuccesful = true;
            return(response);
        }
Ejemplo n.º 5
0
        public async Task <GenricResponse> AddToRead(UserBookRequest request)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <UserBooks>(Config.UserBooks);
            var            userbook   = (await collection.FindAsync(doc => doc.UserId == request.UserId)).FirstOrDefault();

            if (userbook == null)
            {
                userbook = new UserBooks()
                {
                    Id     = Guid.NewGuid(),
                    UserId = request.UserId
                };
                userbook.Books.Add(new UserBook()
                {
                    BookId = request.BookId
                });

                await collection.InsertOneAsync(userbook);
            }
            else
            {
                if (!userbook.Books.Any(x => x.BookId == request.BookId))
                {
                    userbook.Books.Add(new UserBook()
                    {
                        BookId = request.BookId
                    });

                    await collection.ReplaceOneAsync(doc => doc.Id == userbook.Id, userbook);
                }
                {
                    response.Message = "Book is already added";
                    return(response);
                }
            }

            response.IsSuccesful = true;
            return(response);
        }
        public async Task <GenricResponse> Post(Book book)
        {
            GenricResponse response   = new GenricResponse();
            MongoClient    dbClient   = new MongoClient(Config.ConnectionString);
            var            database   = dbClient.GetDatabase(Config.Database);
            var            collection = database.GetCollection <Book>(Config.Books);
            var            books      = (await collection.FindAsync(new BsonDocument())).ToList();

            book.Id = (book.Id == Guid.Empty ? Guid.NewGuid() : book.Id);
            if (books.Any(x => x.Id == book.Id))
            {
                response.Message = "Book Id is already in use";
            }
            else
            {
                await collection.InsertOneAsync(book);

                response.IsSuccesful = true;
            }
            return(response);
        }