Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ConfigureServices(IServiceCollection services)
- {
- // Stuff
- // get jwt claims, something like:
- // clientId = Request.Claims["ClientId"]; However Request only seems to be accessible within a controller
- // newConnectionString = logic to replace the value found in Configuration.GetConnectionString("Default") with my desired database connection string
- services.AddDbContext<KahunaDbContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("Default"))); // current state
- // options.UseSqlServer(newConnectionString)); // desired state
- }
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- namespace API.Persistence.Repositories
- {
- public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
- {
- private readonly KahunaDbContext context;
- public int ClienteId { get; set; }
- public BaseRepository(KahunaDbContext context)
- {
- }
- public void Crear(TEntity entity)
- {
- context.Set<TEntity>().Add(entity);
- }
- public async Task<List<TEntity>> GetTodos()
- {
- return await this.context.Set<TEntity>().ToListAsync();
- }
- public TEntity GetSingle(int id)
- {
- return this.context.Set<TEntity>().Find(id);
- }
- public void Borrar(TEntity entity)
- {
- this.context.Remove(entity);
- }
- }
- public interface IBaseRepository<T>
- {
- int ClienteId { get; set; }
- void Crear(T entity);
- Task<List<T>> GetTodos();
- void Borrar(T entity);
- T GetSingle(int id);
- }
- }
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using APIPersistence;
- using API.Persistence.Repositories;
- namespace API.Controllers
- {
- public class BaseController<TEntity, TEntityResource> : Controller
- {
- private readonly IMapper mapper;
- private readonly IUnitOfWork unitOfWork;
- private readonly IBaseRepository<TEntity> repository;
- public BaseController(IBaseRepository<TEntity> repository, IUnitOfWork unitOfWork, IMapper mapper)
- {
- }
- [HttpGet]
- [Authorize]
- virtual public async Task<IActionResult> Get()
- {
- List<TEntity> TEntitys = await this.repository.GetTodos();
- // List<TEntityResource> TEntityResource = this.mapper.Map<List<TEntity>, List<TEntityResource>>(TEntitys);
- return Ok(TEntitys);
- }
- [HttpPost]
- public async Task<IActionResult> Post([FromBody] TEntityResource TEntityResource)
- {
- if (!ModelState.IsValid)
- return BadRequest(ModelState);
- TEntity tEntity = this.mapper.Map<TEntityResource, TEntity>(TEntityResource);
- this.repository.Crear(tEntity);
- await this.unitOfWork.CompleteAsync();
- return Ok(tEntity);
- }
- [HttpDelete("{id}")]
- public async Task<IActionResult> Delete(int id)
- {
- if (!ModelState.IsValid)
- return BadRequest(ModelState);
- TEntity TEntity = this.repository.GetSingle(id);
- this.repository.Borrar(TEntity);
- await this.unitOfWork.CompleteAsync();
- return Ok(true);
- }
- [HttpPut("{id}")]
- public async Task<IActionResult> Put(int id, [FromBody] TEntityResource TEntityResource)
- {
- if (!ModelState.IsValid)
- return BadRequest(ModelState);
- TEntity TEntity = this.repository.GetSingle(id);
- this.mapper.Map<TEntityResource, TEntity>(TEntityResource, TEntity);
- await this.unitOfWork.CompleteAsync();
- return Ok(true);
- }
- }
- }
Add Comment
Please, Sign In to add comment