1 | namespace backend.Controllers;
|
---|
2 |
|
---|
3 | using backend.DTOs;
|
---|
4 | using backend.Entities;
|
---|
5 | using backend.Helpers;
|
---|
6 | using backend.Models;
|
---|
7 | using backend.Services;
|
---|
8 | using Microsoft.AspNetCore.Mvc;
|
---|
9 |
|
---|
10 | [ApiController]
|
---|
11 | [Route("[controller]")]
|
---|
12 | public class UsersController : ControllerBase
|
---|
13 | {
|
---|
14 | private readonly IUserService _userService = null;
|
---|
15 | private readonly IRestaurantService _restaurantService = null;
|
---|
16 |
|
---|
17 | public UsersController(IUserService userService, IRestaurantService restaurantService)
|
---|
18 | {
|
---|
19 | _userService = userService;
|
---|
20 | _restaurantService = restaurantService;
|
---|
21 | }
|
---|
22 |
|
---|
23 | [HttpPost("login")]
|
---|
24 | public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
|
---|
25 | {
|
---|
26 | var response = await _userService.Authenticate(model);
|
---|
27 |
|
---|
28 | if (response == null)
|
---|
29 | throw new Exception("Email or password is incorrect");
|
---|
30 |
|
---|
31 | return response;
|
---|
32 | }
|
---|
33 |
|
---|
34 | [HttpGet("authed")]
|
---|
35 | public async Task<AuthenticateResponse> IsAuthed()
|
---|
36 | {
|
---|
37 | int userId = 0;
|
---|
38 | try
|
---|
39 | {
|
---|
40 | userId = (int)this.HttpContext.Items["User"];
|
---|
41 | }catch (Exception ex){ return null; }
|
---|
42 | User user = await _userService.GetById(userId);
|
---|
43 | return new AuthenticateResponse() { Email=user.Email, Id = user.Id, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed};
|
---|
44 | }
|
---|
45 |
|
---|
46 | [HttpPost("confirm")]
|
---|
47 | public async Task ConfirmEmail()
|
---|
48 | {
|
---|
49 | int userId = 0;
|
---|
50 | try
|
---|
51 | {
|
---|
52 | userId = (int)this.HttpContext.Items["User"];
|
---|
53 | }
|
---|
54 | catch (Exception ex) { return; }
|
---|
55 | User user = await _userService.GetById(userId);
|
---|
56 | await _userService.SendEmailConfirmation(user.Email);
|
---|
57 | }
|
---|
58 |
|
---|
59 | [HttpPost("reset")]
|
---|
60 | public async Task ResetPassword(string email)
|
---|
61 | {
|
---|
62 | await _userService.SendPasswordReset(email);
|
---|
63 | }
|
---|
64 |
|
---|
65 | [HttpPost("confirmed")]
|
---|
66 | public async Task ConfirmedEmail(string validityString)
|
---|
67 | {
|
---|
68 | int userId = 0;
|
---|
69 | try
|
---|
70 | {
|
---|
71 | userId = (int)this.HttpContext.Items["User"];
|
---|
72 | }
|
---|
73 | catch (Exception ex) { return; }
|
---|
74 | User user = await _userService.GetById(userId);
|
---|
75 | await _userService.ConfirmEmail(user, validityString);
|
---|
76 | }
|
---|
77 |
|
---|
78 | [HttpPost("reseted")]
|
---|
79 | public async Task ResetedPassword(string validityString, string newPassword)
|
---|
80 | {
|
---|
81 | await _userService.ResetPassword(validityString, newPassword);
|
---|
82 | }
|
---|
83 |
|
---|
84 | [HttpPost("register")]
|
---|
85 | public async Task<AuthenticateResponse> Register(CreateUserRequest req)
|
---|
86 | {
|
---|
87 | bool isFirst = await _restaurantService.GetRestaurant() == null;
|
---|
88 | var response = await _userService.Register(req, isFirst);
|
---|
89 | if (isFirst)
|
---|
90 | {
|
---|
91 | await _restaurantService.CreateRestaurant("", response.Id);
|
---|
92 | }
|
---|
93 | return response;
|
---|
94 | }
|
---|
95 | }
|
---|