using GraphQL;
using System.Collections.Generic;
using System.Linq;

namespace NewWebApi.Schema
{
    public class Query
    {
        private Context dbContext { get; set; }

        public Query()
        {
            dbContext = new Context();
        }

        [GraphQLMetadata("books")]
        public List<Book> GeBooks()
        {
            return dbContext.Books;
        }

        [GraphQLMetadata("book")]
        public Book GeBook(int id)
        {
            return dbContext.Books.Where(x => x.Id == id).FirstOrDefault();
        }

        [GraphQLMetadata("authors")]
        public List<Author> GeAuthors()
        {
            return dbContext.Authors;
        }

        [GraphQLMetadata("author")]
        public Author GeAuthor(int id)
        {
            return dbContext.Authors.Where(x => x.Id == id).FirstOrDefault();
        }

        [GraphQLMetadata("addAuthor")]
        public void AddAuthor(Author author)
        {
            dbContext.Authors.Add(author);
        }

        [GraphQLMetadata("addBook")]
        public void AddBook(Book book)
        {
            dbContext.Books.Add(book);
        }
    }
}
