Skip to content

Commit

Permalink
Initial commit: Add ASP.NET Core project with CRUD operations
Browse files Browse the repository at this point in the history
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
Clifftech123 committed Jan 7, 2025
1 parent 5bb8e70 commit 985f401
Show file tree
Hide file tree
Showing 20 changed files with 790 additions and 0 deletions.
22 changes: 22 additions & 0 deletions RepositoryPattern.API/RepositoryPattern.API.sln
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
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);
}

}
}
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 RepositoryPattern.API/RepositoryPattern.API/Data/AppDbContext.cs
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; }
}

}
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; }
}


}
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; }
}

}
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; }
}
}
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; }
}
}
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>();

}
}
}
Loading

0 comments on commit 985f401

Please sign in to comment.