-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit: Add ASP.NET Core project with CRUD operations
Added a new Visual Studio solution and project files for RepositoryPattern.API. Configured appsettings for development and production environments. Set up Program.cs to include services for controllers, Swagger, database context, repositories, and AutoMapper. Implemented AuthorController and BookController for CRUD operations. Created AppDbContext to define the database context with Authors and Books DbSets. Defined contract classes and entity models for authors and books. Configured AutoMapper mappings in MappingProfile. Added initial migration files to set up the database schema. Configured launchSettings for development environment. Implemented BaseRepository and IBaseRepository for generic repository pattern.
- Loading branch information
1 parent
5bb8e70
commit 985f401
Showing
20 changed files
with
790 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.12.35506.116 d17.12 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RepositoryPattern.API", "RepositoryPattern.API\RepositoryPattern.API.csproj", "{B3EA3231-ED52-431E-96B6-DAFA3173E1B4}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B3EA3231-ED52-431E-96B6-DAFA3173E1B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B3EA3231-ED52-431E-96B6-DAFA3173E1B4}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B3EA3231-ED52-431E-96B6-DAFA3173E1B4}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B3EA3231-ED52-431E-96B6-DAFA3173E1B4}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
82 changes: 82 additions & 0 deletions
82
RepositoryPattern.API/RepositoryPattern.API/Controllers/AuthorController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RepositoryPattern.API.Domain.Contracts; | ||
using RepositoryPattern.API.Domain.Entities; | ||
using RepositoryPattern.API.Repositories; | ||
|
||
namespace RepositoryPattern.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api/")] | ||
public class AuthorController : ControllerBase | ||
{ | ||
private readonly IBaseRepository<Author> _authorRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public AuthorController(IBaseRepository<Author> authorRepository, IMapper mapper) | ||
{ | ||
_authorRepository = authorRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
|
||
[HttpGet("authors")] | ||
public async Task<IActionResult> GetAuthors() | ||
{ | ||
var authors = await _authorRepository.GetAll(a => a.Books); | ||
var authorDtos = _mapper.Map<List<GetAuthorDto>>(authors); | ||
return Ok(authorDtos); | ||
} | ||
|
||
|
||
[HttpGet("authors/{id}")] | ||
public async Task<IActionResult> GetAuthor(Guid id) | ||
{ | ||
var author = await _authorRepository.Get(id, a => a.Books); | ||
if (author == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var authorDto = _mapper.Map<GetAuthorDto>(author); | ||
return Ok(authorDto); | ||
} | ||
|
||
[HttpPost("authors")] | ||
public async Task<IActionResult> CreateAuthor([FromBody] CreateAuthor createAuthor) | ||
{ | ||
var author = _mapper.Map<Author>(createAuthor); | ||
var createdAuthor = await _authorRepository.Add(author); | ||
var authorDto = _mapper.Map<GetAuthorDto>(createdAuthor); | ||
return CreatedAtAction(nameof(GetAuthor), new { id = authorDto.Id }, authorDto); | ||
} | ||
|
||
|
||
[HttpPut("authors/{id}")] | ||
public async Task<IActionResult> UpdateAuthor(Guid id, [FromBody] UpdateAuthor updateAuthor) | ||
{ | ||
var author = await _authorRepository.Get(id); | ||
if (author == null) | ||
{ | ||
return NotFound(); | ||
} | ||
_mapper.Map(updateAuthor, author); | ||
var updatedAuthor = await _authorRepository.Update(author); | ||
var authorDto = _mapper.Map<GetAuthorDto>(updatedAuthor); | ||
return Ok(authorDto); | ||
} | ||
|
||
|
||
[HttpDelete("authors/{id}")] | ||
public async Task<IActionResult> DeleteAuthor(Guid id) | ||
{ | ||
var author = await _authorRepository.Delete(id); | ||
if (author == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var authorDto = _mapper.Map<GetAuthorDto>(author); | ||
return Ok(authorDto); | ||
} | ||
|
||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
RepositoryPattern.API/RepositoryPattern.API/Controllers/BookController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RepositoryPattern.API.Domain.Contracts; | ||
using RepositoryPattern.API.Domain.Entities; | ||
using RepositoryPattern.API.Repositories; | ||
|
||
namespace RepositoryPattern.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api/")] | ||
public class BookController : ControllerBase | ||
{ | ||
|
||
private readonly IBaseRepository<Book> _bookRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public BookController(IBaseRepository<Book> bookRepository, IMapper mapper) | ||
{ | ||
_bookRepository = bookRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
|
||
[HttpGet("books")] | ||
public async Task<IActionResult> GetBooks() | ||
{ | ||
var books = await _bookRepository.GetAll(a => a.Author); | ||
var bookDtos = _mapper.Map<List<GetBookDto>>(books); | ||
return Ok(bookDtos); | ||
} | ||
|
||
|
||
[HttpGet("books/{id}")] | ||
public async Task<IActionResult> GetBook(Guid id) | ||
{ | ||
var book = await _bookRepository.Get(id, a => a.Author); | ||
if (book == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var bookDto = _mapper.Map<GetBookDto>(book); | ||
return Ok(bookDto); | ||
} | ||
|
||
[HttpPost("books")] | ||
public async Task<IActionResult> CreateBook([FromBody] CreateBook createBook) | ||
{ | ||
var book = _mapper.Map<Book>(createBook); | ||
var createdBook = await _bookRepository.Add(book); | ||
var bookDto = _mapper.Map<GetBookDto>(createdBook); | ||
return CreatedAtAction(nameof(GetBook), new { id = bookDto.Id }, bookDto); | ||
} | ||
|
||
|
||
|
||
[HttpPut("books/{id}")] | ||
|
||
public async Task<IActionResult> UpdateBook(Guid id, [FromBody] UpdateBook updateBook) | ||
{ | ||
var book = await _bookRepository.Get(id); | ||
if (book == null) | ||
{ | ||
return NotFound(); | ||
} | ||
_mapper.Map(updateBook, book); | ||
var updatedBook = await _bookRepository.Update(book); | ||
var bookDto = _mapper.Map<GetBookDto>(updatedBook); | ||
return Ok(bookDto); | ||
} | ||
|
||
|
||
|
||
[HttpDelete("books/{id}")] | ||
public async Task<IActionResult> DeleteBook(Guid id) | ||
{ | ||
var book = await _bookRepository.Get(id); | ||
if (book == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var deletedBook = await _bookRepository.Delete(id); | ||
var bookDto = _mapper.Map<GetBookDto>(deletedBook); | ||
return Ok(bookDto); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
RepositoryPattern.API/RepositoryPattern.API/Data/AppDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using RepositoryPattern.API.Domain.Entities; | ||
|
||
namespace RepositoryPattern.API.Data | ||
{ | ||
public class AppDbContext : DbContext | ||
{ | ||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Author> Authors { get; set; } | ||
public DbSet<Book> Books { get; set; } | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
RepositoryPattern.API/RepositoryPattern.API/Domain/Contracts/AuthorContract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace RepositoryPattern.API.Domain.Contracts | ||
{ | ||
public record CreateAuthor | ||
{ | ||
public string Name { get; init; } | ||
public string Bio { get; init; } | ||
} | ||
|
||
public record UpdateAuthor | ||
{ | ||
public string Name { get; init; } | ||
public string Bio { get; init; } | ||
} | ||
|
||
|
||
public record DeleteAuthor | ||
{ | ||
public Guid Id { get; init; } | ||
|
||
|
||
} | ||
|
||
public record GetAuthor | ||
{ | ||
public Guid Id { get; init; } | ||
} | ||
|
||
|
||
public class GetAuthorDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public string Bio { get; set; } | ||
public ICollection<GetBookDto> Books { get; set; } | ||
} | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
RepositoryPattern.API/RepositoryPattern.API/Domain/Contracts/BookContract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace RepositoryPattern.API.Domain.Contracts | ||
{ | ||
public record CreateBook | ||
{ | ||
public string Title { get; init; } | ||
public double Price { get; init; } | ||
public string Description { get; init; } | ||
public Guid AuthorId { get; init; } | ||
} | ||
|
||
|
||
public record UpdateBook | ||
{ | ||
public string Title { get; init; } | ||
public double Price { get; init; } | ||
public string Description { get; init; } | ||
public Guid AuthorId { get; init; } | ||
} | ||
|
||
|
||
public record DeleteBook | ||
{ | ||
public Guid Id { get; init; } | ||
} | ||
|
||
|
||
public record GetBook | ||
{ | ||
public Guid Id { get; init; } | ||
} | ||
|
||
|
||
public class GetBookDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Title { get; set; } | ||
public double Price { get; set; } | ||
public string Description { get; set; } | ||
public Guid AuthorId { get; set; } | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
RepositoryPattern.API/RepositoryPattern.API/Domain/Entities/Author.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace RepositoryPattern.API.Domain.Entities | ||
{ | ||
public class Author | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public string Bio { get; set; } | ||
|
||
public ICollection<Book> Books { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
RepositoryPattern.API/RepositoryPattern.API/Domain/Entities/Book.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace RepositoryPattern.API.Domain.Entities | ||
{ | ||
public class Book | ||
{ | ||
public Guid Id { get; set; } | ||
public string Title { get; set; } | ||
public double Price { get; set; } | ||
public string Description { get; set; } | ||
public Guid AuthorId { get; set; } | ||
public Author Author { get; set; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
RepositoryPattern.API/RepositoryPattern.API/Domain/Mapping/MappingProfile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using AutoMapper; | ||
using RepositoryPattern.API.Domain.Contracts; | ||
using RepositoryPattern.API.Domain.Entities; | ||
|
||
namespace RepositoryPattern.API.Domain.Mapping | ||
{ | ||
public class MappingProfile : Profile | ||
{ | ||
public MappingProfile() | ||
{ | ||
CreateMap<Author, GetAuthorDto>() | ||
.ForMember(dest => dest.Books, opt => opt.MapFrom(src => src.Books)); | ||
CreateMap<CreateAuthor, Author>(); | ||
CreateMap<UpdateAuthor, Author>(); | ||
CreateMap<DeleteAuthor, Author>(); | ||
CreateMap<GetAuthor, Author>(); | ||
|
||
|
||
// Mapping for Book | ||
|
||
CreateMap<Book, GetBookDto>(); | ||
CreateMap<CreateBook, Book>(); | ||
CreateMap<UpdateBook, Book>(); | ||
CreateMap<DeleteBook, Book>(); | ||
CreateMap<GetBook, Book>(); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.