forked from techwithpat/Web-API-With-ASP.NET-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookRepository.cs
More file actions
50 lines (42 loc) · 1.26 KB
/
Copy pathBookRepository.cs
File metadata and controls
50 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using BookAPI.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BookAPI.Repositories
{
public class BookRepository : IBookRepository
{
private readonly BookContext _context;
public BookRepository(BookContext context)
{
_context = context;
}
public async Task<Book> Create(Book book)
{
_context.Books.Add(book);
await _context.SaveChangesAsync();
return book;
}
public async Task Delete(int id)
{
var bookToDelete = await _context.Books.FindAsync(id);
_context.Books.Remove(bookToDelete);
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<Book>> Get()
{
return await _context.Books.ToListAsync();
}
public async Task<Book> Get(int id)
{
return await _context.Books.FindAsync(id);
}
public async Task Update(Book book)
{
_context.Entry(book).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
}
}