From 23ff9e95cb7693879742695a90059be5e4de7074 Mon Sep 17 00:00:00 2001 From: Ashraful Haque Date: Tue, 2 Mar 2021 19:51:46 +0000 Subject: [PATCH 1/2] added books controller --- Controllers/BooksController.cs | 61 ++++++++++++++++++++++++++++++++++ DTO/BookDTO.cs | 11 ++++++ Data/Context.cs | 2 ++ Models/Book.cs | 12 +++++++ Models/Book_description.cs | 13 ++++++++ 5 files changed, 99 insertions(+) create mode 100644 Controllers/BooksController.cs create mode 100644 DTO/BookDTO.cs create mode 100644 Models/Book.cs create mode 100644 Models/Book_description.cs diff --git a/Controllers/BooksController.cs b/Controllers/BooksController.cs new file mode 100644 index 0000000..b5dece5 --- /dev/null +++ b/Controllers/BooksController.cs @@ -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> 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 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; + } + } +} \ No newline at end of file diff --git a/DTO/BookDTO.cs b/DTO/BookDTO.cs new file mode 100644 index 0000000..ef7fd22 --- /dev/null +++ b/DTO/BookDTO.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Data/Context.cs b/Data/Context.cs index b538cbe..bc5e93b 100644 --- a/Data/Context.cs +++ b/Data/Context.cs @@ -7,5 +7,7 @@ namespace TestApplication.Data { public Context(DbContextOptions options) : base(options) {} public DbSet Values {get; set;} + public DbSet Book {get; set;} + public DbSet Book_Description {get; set;} } } \ No newline at end of file diff --git a/Models/Book.cs b/Models/Book.cs new file mode 100644 index 0000000..62ef418 --- /dev/null +++ b/Models/Book.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Models/Book_description.cs b/Models/Book_description.cs new file mode 100644 index 0000000..be63d81 --- /dev/null +++ b/Models/Book_description.cs @@ -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; } + } +} \ No newline at end of file From 5a2d5ff36618826ed8a27e37d2c7e5028a7cc201 Mon Sep 17 00:00:00 2001 From: Ashraful Haque Date: Tue, 2 Mar 2021 20:06:17 +0000 Subject: [PATCH 2/2] made get_book method async --- Controllers/BooksController.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Controllers/BooksController.cs b/Controllers/BooksController.cs index b5dece5..53bae93 100644 --- a/Controllers/BooksController.cs +++ b/Controllers/BooksController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using TestApplication.Data; using TestApplication.DTO; using System.Linq; +using Microsoft.EntityFrameworkCore; namespace TestApplication.Controllers { @@ -19,7 +20,7 @@ namespace TestApplication.Controllers } [HttpGet] - public ActionResult> GetBooks() + public async Task>> GetBooks() { var book = from books in _context.Book join book_descriptions in _context.Book_Description on books.id equals book_descriptions.book_id @@ -32,7 +33,7 @@ namespace TestApplication.Controllers Book_description = book_descriptions.book_description }; - return Ok(book); + return await book.ToListAsync(); } [HttpGet("{id}")]