Guest User

Untitled

a guest
Dec 15th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. // Stuff
  4.  
  5. // get jwt claims, something like:
  6. // clientId = Request.Claims["ClientId"]; However Request only seems to be accessible within a controller
  7.  
  8. // newConnectionString = logic to replace the value found in Configuration.GetConnectionString("Default") with my desired database connection string
  9.  
  10. services.AddDbContext<KahunaDbContext>(options =>
  11. options.UseSqlServer(Configuration.GetConnectionString("Default"))); // current state
  12. // options.UseSqlServer(newConnectionString)); // desired state
  13. }
  14.  
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using Microsoft.EntityFrameworkCore;
  19.  
  20. namespace API.Persistence.Repositories
  21. {
  22. public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
  23. {
  24. private readonly KahunaDbContext context;
  25. public int ClienteId { get; set; }
  26.  
  27. public BaseRepository(KahunaDbContext context)
  28. {
  29. }
  30.  
  31. public void Crear(TEntity entity)
  32. {
  33. context.Set<TEntity>().Add(entity);
  34. }
  35.  
  36. public async Task<List<TEntity>> GetTodos()
  37. {
  38. return await this.context.Set<TEntity>().ToListAsync();
  39. }
  40.  
  41. public TEntity GetSingle(int id)
  42. {
  43. return this.context.Set<TEntity>().Find(id);
  44. }
  45.  
  46. public void Borrar(TEntity entity)
  47. {
  48. this.context.Remove(entity);
  49. }
  50. }
  51.  
  52. public interface IBaseRepository<T>
  53. {
  54. int ClienteId { get; set; }
  55. void Crear(T entity);
  56. Task<List<T>> GetTodos();
  57. void Borrar(T entity);
  58. T GetSingle(int id);
  59. }
  60. }
  61.  
  62. using System.Collections.Generic;
  63. using System.Threading.Tasks;
  64. using AutoMapper;
  65. using Microsoft.AspNetCore.Authorization;
  66. using Microsoft.AspNetCore.Mvc;
  67. using APIPersistence;
  68. using API.Persistence.Repositories;
  69.  
  70. namespace API.Controllers
  71. {
  72. public class BaseController<TEntity, TEntityResource> : Controller
  73. {
  74. private readonly IMapper mapper;
  75. private readonly IUnitOfWork unitOfWork;
  76. private readonly IBaseRepository<TEntity> repository;
  77.  
  78. public BaseController(IBaseRepository<TEntity> repository, IUnitOfWork unitOfWork, IMapper mapper)
  79. {
  80. }
  81.  
  82. [HttpGet]
  83. [Authorize]
  84. virtual public async Task<IActionResult> Get()
  85. {
  86. List<TEntity> TEntitys = await this.repository.GetTodos();
  87. // List<TEntityResource> TEntityResource = this.mapper.Map<List<TEntity>, List<TEntityResource>>(TEntitys);
  88. return Ok(TEntitys);
  89. }
  90.  
  91. [HttpPost]
  92. public async Task<IActionResult> Post([FromBody] TEntityResource TEntityResource)
  93. {
  94. if (!ModelState.IsValid)
  95. return BadRequest(ModelState);
  96.  
  97. TEntity tEntity = this.mapper.Map<TEntityResource, TEntity>(TEntityResource);
  98.  
  99. this.repository.Crear(tEntity);
  100.  
  101. await this.unitOfWork.CompleteAsync();
  102.  
  103. return Ok(tEntity);
  104. }
  105.  
  106. [HttpDelete("{id}")]
  107. public async Task<IActionResult> Delete(int id)
  108. {
  109. if (!ModelState.IsValid)
  110. return BadRequest(ModelState);
  111.  
  112. TEntity TEntity = this.repository.GetSingle(id);
  113. this.repository.Borrar(TEntity);
  114. await this.unitOfWork.CompleteAsync();
  115.  
  116. return Ok(true);
  117. }
  118.  
  119. [HttpPut("{id}")]
  120. public async Task<IActionResult> Put(int id, [FromBody] TEntityResource TEntityResource)
  121. {
  122. if (!ModelState.IsValid)
  123. return BadRequest(ModelState);
  124.  
  125. TEntity TEntity = this.repository.GetSingle(id);
  126. this.mapper.Map<TEntityResource, TEntity>(TEntityResource, TEntity);
  127.  
  128. await this.unitOfWork.CompleteAsync();
  129.  
  130. return Ok(true);
  131. }
  132. }
  133. }
Add Comment
Please, Sign In to add comment