Skip to content

Instantly share code, notes, and snippets.

View emrekizildas's full-sized avatar
🎯
Focusing

Emre Kızıldaş emrekizildas

🎯
Focusing
View GitHub Profile
services.AddEntityFrameworkNpgsql()
.AddDbContext<DatabaseContext>()
.BuildServiceProvider();
private readonly DatabaseContext _db;
public ServiceController(DatabaseContext context)
{
_db = context;
}
[HttpGet("{id}")]
public async Task<ActionResult<BookInfoDto>> GetBook(int id)
{
BookInfoDto book = await _db.Books
.Select(x => new BookInfoDto() { BookID = x.BookID, Name = x.Name, AuthorName = x.Author.Name })
.SingleOrDefaultAsync(x => x.BookID == id);
if (book == null)
return NotFound("İstenilen kitap bulunamadı");
return Ok(book);
[HttpGet]
public async Task<IActionResult> GetBooks()
{
List<BookInfoDto> books = await _db.Books
.Select(x => new BookInfoDto() { BookID = x.BookID, Name = x.Name, AuthorName = x.Author.Name })
.ToListAsync();
if (books.Count() == 0)
return NotFound();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreAPI.DTOs;
using CoreAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace CoreAPI.Controllers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace CoreAPI.Controllers
{
[Route("[controller]/[action]")]
public class HomeController : Controller
<html>
<head>
<title>.NET Core Web API</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
</body>
</html>
<div class="container" style="margin-top:50px;">
<div class="row">
<div class="col-md-4">
<button class="btn btn-success" id="kitaplariGetir">Kitapları Getir</button>
</div>
<div class="col-md-8">
<div id="getBooksArea">
<ul id="getBooks"></ul>
</div>
</div>
$("#kitaplariGetir").click(function(){
$.ajax({
type: 'GET',
url: '/api/Service/GetBooks',
success: function(books){
$("#getBooks").html("");
$.each(books, function(index, value){
$("#getBooks").append("<li>Kitap Adı: <b>"+value.name+" - Yazarı: <b>"+value.authorName+"</b>");
});
}
$("#yazarlariGetir").click(function(){
$.ajax({
type: 'GET',
url: '/api/Service/GetAuthors',
success: function(books){
$("#getAuthors").html("");
$.each(books, function(index, value){
$("#getAuthors").append("<li>Yazar Adı: <b>"+value.authorName+" - Kitap Sayısı: <b>"+value.totalBook+"</b>");
});
}