You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomerController.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.Services;
  3. using Diligent.WebAPI.Data.Entities;
  4. using Diligent.WebAPI.Host.DTOs.Customer;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.AspNetCore.Mvc;
  8. namespace Diligent.WebAPI.Host.Controllers
  9. {
  10. [ApiVersion("1.0")]
  11. [ApiController]
  12. [Route("v{version:apiVersion}/[controller]")]
  13. public class CustomerController : ControllerBase
  14. {
  15. // sifra za svakog od user-a je "Nekasifra123!"
  16. private readonly UserManager<Customer> _customerManager;
  17. private readonly RoleManager<Roles> _roleManager;
  18. private readonly IAuthenticationService _authenticationService;
  19. private readonly IMapper _mapper;
  20. private readonly ICustomerService _customerService;
  21. public CustomerController(UserManager<Customer> customerManager, RoleManager<Roles> roleManager, IAuthenticationService authenticationService,
  22. IMapper mapper, ICustomerService customerService)
  23. {
  24. _customerManager = customerManager;
  25. _roleManager = roleManager;
  26. _authenticationService = authenticationService;
  27. _mapper = mapper;
  28. _customerService = customerService;
  29. }
  30. [HttpPost("login")]
  31. public async Task<ActionResult<CustomerReadDTO>> Login(CustomerLoginDTO customerLoginDTO)
  32. {
  33. if (!await _authenticationService.ValidateCustomer(customerLoginDTO.Username, customerLoginDTO.Password))
  34. return BadRequest("Authentication failed.Wrong Username or password");
  35. Customer customer = await _customerService.GetCustomer(customerLoginDTO.Username);
  36. var customerReadDTO = _mapper.Map<CustomerReadDTO>(customer);
  37. customerReadDTO.Token = await _authenticationService.GenerateToken();
  38. customerReadDTO.Roles = (List<string>)await _customerManager.GetRolesAsync(customer);
  39. return customerReadDTO;
  40. }
  41. [HttpPost("addRole")]
  42. public async Task<ActionResult> CreateRole(string name)
  43. {
  44. IdentityResult result = await _roleManager.CreateAsync(new Roles() { Name = name });
  45. if (!result.Succeeded)
  46. {
  47. foreach (IdentityError error in result.Errors)
  48. ModelState.AddModelError("", error.Description);
  49. return BadRequest(ModelState);
  50. }
  51. return StatusCode(201);
  52. }
  53. [HttpPost("register")]
  54. public async Task<ActionResult<CustomerReadDTO>> Register(CustomerCreateDTO customerCreateDTO)
  55. {
  56. Customer customer = new()
  57. {
  58. FirstName = customerCreateDTO.FirstName,
  59. LastName = customerCreateDTO.LastName,
  60. Email = customerCreateDTO.Email,
  61. UserName = customerCreateDTO.Username
  62. };
  63. var result = await _customerManager.CreateAsync(customer, customerCreateDTO.Password);
  64. await _customerManager.AddToRoleAsync(customer, "Support");
  65. if (!result.Succeeded)
  66. {
  67. foreach (IdentityError error in result.Errors)
  68. ModelState.AddModelError("", error.Description);
  69. return BadRequest(ModelState);
  70. }
  71. await _authenticationService.ValidateCustomer(customer.UserName,customerCreateDTO.Password);
  72. var customerReadDTO = _mapper.Map<CustomerReadDTO>(customer);
  73. customerReadDTO.Token = await _authenticationService.GenerateToken();
  74. customerReadDTO.Roles = (List<string>)await _customerManager.GetRolesAsync(customer);
  75. return customerReadDTO;
  76. }
  77. }
  78. }