Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BookAPI/BookAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions BookAPI/BookAPI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33103.184
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookAPI", "BookAPI.csproj", "{FE52426A-BCF6-48AA-9441-63448C8FD5D8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FE52426A-BCF6-48AA-9441-63448C8FD5D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE52426A-BCF6-48AA-9441-63448C8FD5D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE52426A-BCF6-48AA-9441-63448C8FD5D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE52426A-BCF6-48AA-9441-63448C8FD5D8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B6210FC6-C5EE-459E-ADC7-0D07807156F6}
EndGlobalSection
EndGlobal
99 changes: 99 additions & 0 deletions BookAPI/Controllers/AuthorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using AutoMapper;
using BookAPI.Dto;
using BookAPI.Model;
using BookAPI.Service.Implementation;
using BookAPI.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BookAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthorController : ControllerBase
{
private readonly IAuthorRepository _authorRepository;
private readonly IMapper _mapper;

public AuthorController(IAuthorRepository authorRepository, IMapper mapper)
{
_authorRepository = authorRepository;
_mapper = mapper;
}

[HttpPost]
public async Task<ActionResult<ApiResponse>> CreateAuthor(CreateAuthor createAuthor)
{
try
{
var author = _mapper.Map<Author>(createAuthor); // map CreateAuthor to Author
await _authorRepository.AddAsync(author);
var result = _mapper.Map<CreateAuthor>(author);
return new ApiResponse { StatusCode = HttpStatusCode.Created, IsSuccess = true, Result = result };
}
catch (Exception ex)
{
var errors = new List<string> { ex.Message };
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet("{id}")]
public async Task<ActionResult<ApiResponse>> GetAuthorById(int id)
{
try
{
var author = await _authorRepository.GetByIdAsync(id);
if (author == null)
{
return new ApiResponse { StatusCode = HttpStatusCode.NotFound, IsSuccess = false };
}
var result = _mapper.Map<CreateAuthor>(author);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = result };
}
catch (Exception ex)
{
var errors = new List<string> { ex.Message };
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet]
public async Task<ActionResult<ApiResponse>> GetAllAuthors()
{
try
{
var authors = await _authorRepository.GetAllAsync();
var result = _mapper.Map<IEnumerable<CreateAuthor>>(authors);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = result };
}
catch (Exception ex)
{
var errors = new List<string> { ex.Message };
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet("{id}/books")]
public async Task<ActionResult<ApiResponse>> GetBooksAttachedToAnAuthor(int id)
{
try
{
var books = await _authorRepository.GetBooksAttachedToAuthor(id);
if (books == null)
{
return new ApiResponse { StatusCode = HttpStatusCode.NotFound, IsSuccess = false };
}
var result = _mapper.Map<IEnumerable<CreateBook>>(books);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = result };
}
catch (Exception ex)
{
var errors = new List<string> { ex.Message };
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}
}

}
82 changes: 82 additions & 0 deletions BookAPI/Controllers/BookController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using AutoMapper;
using BookAPI.Dto;
using BookAPI.Model;
using BookAPI.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BookAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BookController : ControllerBase
{
private readonly IRepository<Book> _bookRepository;
private readonly IMapper _mapper;

public BookController(IRepository<Book> bookRepository, IMapper mapper)
{
_bookRepository = bookRepository;
_mapper = mapper;
}

[HttpPost("CreateBook")]
public async Task<ActionResult<ApiResponse>> CreateBook(CreateBook createBook)
{
var errors = new List<string>();
try
{
var book = _mapper.Map<Book>(createBook);
await _bookRepository.AddAsync(book);
var createdBook = _mapper.Map<CreateBook>(book); // map Book to CreateBook
return new ApiResponse { StatusCode = HttpStatusCode.Created, IsSuccess = true, Result = createdBook };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet("{id}")]
public async Task<ActionResult<ApiResponse>> GetBookById(int id)
{
var errors = new List<string>();
try
{
var book = await _bookRepository.GetByIdAsync(id);
if (book == null)
{
errors.Add($"Book with id {id} not found");
return new ApiResponse { StatusCode = HttpStatusCode.NotFound, IsSuccess = false, ErrorMessages = errors };
}
var thebook = _mapper.Map<CreateBook>(book);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = thebook };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet]
public async Task<ActionResult<ApiResponse>> GetAllBooks()
{
var errors = new List<string>();
try
{
var books = await _bookRepository.GetAllAsync();
var thebooks = _mapper.Map<IEnumerable<CreateBook>>(books);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = thebooks };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}
}

}
110 changes: 110 additions & 0 deletions BookAPI/Controllers/PublisherController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using AutoMapper;
using BookAPI.Dto;
using BookAPI.Model;
using BookAPI.Service.Implementation;
using BookAPI.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BookAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PublisherController : ControllerBase
{
private readonly IPublisherRepository _publisherRepository;
private readonly IMapper _mapper;

public PublisherController(IPublisherRepository publisherRepository, IMapper mapper)
{
_publisherRepository = publisherRepository;
_mapper = mapper;
}

[HttpPost]
public async Task<ActionResult<ApiResponse>> CreatePublisher(CreatePublisher createPublisher)
{
var errors = new List<string>();
try
{
var publisher = _mapper.Map<Publisher>(createPublisher);
await _publisherRepository.AddAsync(publisher);
var createdPublisher = _mapper.Map<CreatePublisher>(publisher);

return new ApiResponse { StatusCode = HttpStatusCode.Created, IsSuccess = true, Result = createdPublisher };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet("{id}")]
public async Task<ActionResult<ApiResponse>> GetPublisher(int id)
{
var errors = new List<string>();
try
{
var publisher = await _publisherRepository.GetByIdAsync(id);
if (publisher == null)
{
errors.Add($"Publisher with id {id} not found");
return new ApiResponse { StatusCode = HttpStatusCode.NotFound, IsSuccess = false, ErrorMessages = errors };
}
var thePublisher = _mapper.Map<CreatePublisher>(publisher);

return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = thePublisher };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet]
public async Task<ActionResult<ApiResponse>> GetAllPublishers()
{
var errors = new List<string>();
try
{
var publishers = await _publisherRepository.GetAllAsync();
var thePublishers = _mapper.Map<IEnumerable<CreatePublisher>>(publishers);
return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = thePublishers };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

[HttpGet("{id}/authors")]
public async Task<ActionResult<ApiResponse>> GetAuthorsAttachedToAPublisher(int id)
{
var errors = new List<string>();
try
{
var publisher = await _publisherRepository.GetByIdAsync(id);
if (publisher == null)
{
errors.Add($"Publisher with id {id} not found");
return new ApiResponse { StatusCode = HttpStatusCode.NotFound, IsSuccess = false, ErrorMessages = errors };
}
var authors = await _publisherRepository.GetAuthorsAttachedToPublisher(id);
var theAuthors = _mapper.Map<IEnumerable<CreateAuthor>>(authors);

return new ApiResponse { StatusCode = HttpStatusCode.OK, IsSuccess = true, Result = theAuthors };
}
catch (Exception ex)
{
errors.Add(ex.Message);
return new ApiResponse { StatusCode = HttpStatusCode.InternalServerError, IsSuccess = false, ErrorMessages = errors };
}
}

}

}
15 changes: 15 additions & 0 deletions BookAPI/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BookAPI.Model;
using Microsoft.EntityFrameworkCore;

namespace BookAPI.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<Publisher> Publishers { get; set; }
public DbSet<Author> Authors { get; set; }
}
}
8 changes: 8 additions & 0 deletions BookAPI/Dto/CreateAuthor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BookAPI.Dto
{
public class CreateAuthor
{
public string Name { get; set; }
public int PublisherId { get; set; }
}
}
14 changes: 14 additions & 0 deletions BookAPI/Dto/CreateBook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace BookAPI.Dto
{
public class CreateBook
{
public string Title { get; set; }
public DateTime PublicationDate { get; set; }
[ForeignKey("Author")]
public int AuthorId { get; set; }
[ForeignKey("Publisher")]
public int PublisherId { get; set; }
}
}
8 changes: 8 additions & 0 deletions BookAPI/Dto/CreatePublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BookAPI.Dto
{
public class CreatePublisher
{
public string Name { get; set; }
public string Address { get; set; }
}
}
Loading