❔ i want to add jwt in my api i am making should I put this in the same controller as my register
using HotelRservationAPI.DTO;
using HotelRservationAPI.Interface;
using HotelRservationAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HotelRservationAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegisterController : Controller
{
private readonly IRegisterRepository _registerRepository;
public RegisterController(IRegisterRepository registerRepository)
{
_registerRepository = registerRepository;
}
#region Post
[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult CreateRegister([FromBody] RegisterDto registerCreate)
{
if (registerCreate == null)
return BadRequest(ModelState);
var category = _registerRepository.GetRegisters()
.Where(c => c.Username.Trim().ToUpper() == registerCreate.Username.TrimEnd().ToUpper())
.FirstOrDefault();
if (category != null)
{
ModelState.AddModelError("", "Category already exists");
return StatusCode(422, ModelState);
}
if (!ModelState.IsValid)
return BadRequest(ModelState);
var categoryMap = new Register
{
Username = registerCreate.Username,
Password = registerCreate.Password,
Email = registerCreate.Email
};
if (!_registerRepository.CreateRegister(categoryMap))
{
ModelState.AddModelError("", "Something went wrong while saving");
return StatusCode(500, ModelState);
}
return Ok("Successfully created");
}
#endregion
}
}
using HotelRservationAPI.DTO;
using HotelRservationAPI.Interface;
using HotelRservationAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HotelRservationAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegisterController : Controller
{
private readonly IRegisterRepository _registerRepository;
public RegisterController(IRegisterRepository registerRepository)
{
_registerRepository = registerRepository;
}
#region Post
[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult CreateRegister([FromBody] RegisterDto registerCreate)
{
if (registerCreate == null)
return BadRequest(ModelState);
var category = _registerRepository.GetRegisters()
.Where(c => c.Username.Trim().ToUpper() == registerCreate.Username.TrimEnd().ToUpper())
.FirstOrDefault();
if (category != null)
{
ModelState.AddModelError("", "Category already exists");
return StatusCode(422, ModelState);
}
if (!ModelState.IsValid)
return BadRequest(ModelState);
var categoryMap = new Register
{
Username = registerCreate.Username,
Password = registerCreate.Password,
Email = registerCreate.Email
};
if (!_registerRepository.CreateRegister(categoryMap))
{
ModelState.AddModelError("", "Something went wrong while saving");
return StatusCode(500, ModelState);
}
return Ok("Successfully created");
}
#endregion
}
}
2 Replies
I don't see what any of this has to do with JWTs
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.