added books controller
This commit is contained in:
parent
083afac1d4
commit
23ff9e95cb
|
|
@ -0,0 +1,61 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TestApplication.Data;
|
||||
using TestApplication.DTO;
|
||||
using System.Linq;
|
||||
|
||||
namespace TestApplication.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class BooksController : ControllerBase
|
||||
{
|
||||
private readonly Context _context;
|
||||
|
||||
public BooksController(Context context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<IEnumerable<BookDTO>> GetBooks()
|
||||
{
|
||||
var book = from books in _context.Book
|
||||
join book_descriptions in _context.Book_Description on books.id equals book_descriptions.book_id
|
||||
select new BookDTO
|
||||
{
|
||||
Book_id = books.id,
|
||||
Book_price = books.price,
|
||||
ISBN = books.isbn,
|
||||
Book_name = book_descriptions.book_name,
|
||||
Book_description = book_descriptions.book_description
|
||||
};
|
||||
|
||||
return Ok(book);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<BookDTO> GetBooks_byId(int id)
|
||||
{
|
||||
var book = from books in _context.Book
|
||||
join book_descriptions in _context.Book_Description on books.id equals book_descriptions.book_id
|
||||
select new BookDTO
|
||||
{
|
||||
Book_id = books.id,
|
||||
Book_price = books.price,
|
||||
ISBN = books.isbn,
|
||||
Book_name = book_descriptions.book_name,
|
||||
Book_description = book_descriptions.book_description
|
||||
};
|
||||
|
||||
var book_by_id = book.ToList().Find(x => x.Book_id == id);
|
||||
|
||||
if (book_by_id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return book_by_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
namespace TestApplication.DTO
|
||||
{
|
||||
public class BookDTO
|
||||
{
|
||||
public int Book_id { get; set; }
|
||||
public decimal Book_price { get; set; }
|
||||
public string ISBN { get; set; }
|
||||
public string Book_name { get; set; }
|
||||
public string Book_description { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -7,5 +7,7 @@ namespace TestApplication.Data
|
|||
{
|
||||
public Context(DbContextOptions<Context> options) : base(options) {}
|
||||
public DbSet<Values> Values {get; set;}
|
||||
public DbSet<Book> Book {get; set;}
|
||||
public DbSet<Book_description> Book_Description {get; set;}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models
|
||||
{
|
||||
public class Book
|
||||
{
|
||||
[Key]
|
||||
public int id { get; set; }
|
||||
public decimal price { get; set; }
|
||||
public string isbn { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models
|
||||
{
|
||||
public class Book_description
|
||||
{
|
||||
[Key]
|
||||
public int id { get; set; }
|
||||
public int book_id { get; set; }
|
||||
public string book_name { get; set; }
|
||||
public string book_description { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue