public async Task <int> UpdateAuthor(string authorId, Author author)
        {
            int DbResult = 0;

            if (!String.IsNullOrEmpty(authorId) && dataReqiered.IsDataNoEmpty(author))
            {
                AuthorMongoDb oldAuthorData = await db.Authors.Find(new BsonDocument("_id", new ObjectId(authorId))).FirstOrDefaultAsync();

                if (oldAuthorData != null)
                {
                    AuthorMongoDb newAuthorData = new AuthorMongoDb {
                        Id = author.Id, Name = author.Name, Surname = author.Surname
                    };
                    try
                    {
                        await db.Authors.ReplaceOneAsync(new BsonDocument("_id", new ObjectId(authorId)), newAuthorData);

                        DbResult = 1;
                    }
                    catch
                    {
                        return(DbResult);
                    }
                }
            }
            return(DbResult);
        }
Example #2
0
 public Author(AuthorMongoDb author)
 {
     Id            = author.Id;
     Name          = author.Name;
     Surname       = author.Surname;
     AuthorMongoDb = author;
 }
Example #3
0
        public async Task <Author> GetAuthorById(string authorId)
        {
            Author Author = new Author();

            if (!String.IsNullOrEmpty(authorId))
            {
                AuthorMongoDb authorDbResult = await db.Authors.Find(new BsonDocument("_id", new ObjectId(authorId))).FirstOrDefaultAsync();

                if (authorDbResult != null)
                {
                    Author.Id      = authorDbResult.Id;
                    Author.Name    = authorDbResult.Name;
                    Author.Surname = authorDbResult.Surname;
                }
            }

            return(Author);
        }
        public async Task <int> CreateAuthor(Author author)
        {
            int DbResult = 0;

            if (dataReqiered.IsDataNoEmpty(author))
            {
                AuthorMongoDb newAuthor = new AuthorMongoDb {
                    Id = author.Id, Name = author.Name, Surname = author.Surname
                };
                try
                {
                    await db.Authors.InsertOneAsync(newAuthor);

                    DbResult = 1;
                }
                catch
                {
                    return(DbResult);
                }
            }
            return(DbResult);
        }