Ayush-projects / team3

0 stars 0 forks source link

Repo #2

Open AvirupBh opened 1 year ago

AvirupBh commented 1 year ago

using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web;

namespace BookCrudOperationASP_Net.Data { public class Repository : IBooksRepository { private readonly BookDbContext entities;

    public Repository(BookDbContext bookDbContext)
    {
        this.entities = bookDbContext;
    }

    public List<Book> GetAllBooks()
    {
        return this.entities.Books.ToList();

    }

    public Book GetBookById(int id)
    {
        var sbook=this.entities.Books.Find(id);
        if(sbook==null)
        return null;
        return sbook;
    }

    public bool AddBook(Book book)
    {
        if(book==null)
        return false;
        var sb=this.entities.Books.Where(b=>b.isbn==book.isbn).FirstOrDefault();
        if(sb!=null)
        return false;
        this.entities.Books.Add(book);
        this.entities.SaveChanges();
        return true;
        //throw new NotImplementedException();
    }

    public bool UpdateBook(Book book)
    {
         if(book==null)
        return false;
        var eb=this.entities.Books.Find(book.Id);
        if(eb==null)
        return false;

        var sb=this.entities.Books.Where(b=>b.isbn==book.isbn);
        if(sb==null)
        return false;
        eb.isbn=book.isbn;
        eb.Title=book.Title;
        eb.Author=book.Author;
        eb.Publisher=book.Publisher;
        eb.Pages=book.Pages;
        return true;
    }

    public bool DeleteBook(int id)
    {
        var sb=this.entities.Books.Find(id);
        if(sb==null)
        return false;
        this.entities.Books.Remove(sb);
        return this.entities.SaveChanges()>0;
    }
}

}