source: resTools_backend/backend/Controllers/UsersController.cs

Last change on this file was 13f1472, checked in by Danilo <danilo.najkov@…>, 2 years ago

vip functionallity + menu fields + alergens filtering + google/fb login + email queueing

  • Property mode set to 100644
File size: 3.2 KB
Line 
1namespace backend.Controllers;
2
3using backend.DTOs;
4using backend.Entities;
5using backend.Helpers;
6using backend.Models;
7using backend.Services;
8using Microsoft.AspNetCore.Mvc;
9
10[ApiController]
11[Route("[controller]")]
12public 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 }
42 catch (Exception ex)
43 {
44 var usr = await _userService.GetByEmail((string)this.HttpContext.Items["User"]);
45 if(usr == null)
46 {
47 return null;
48 }
49 userId = usr.Id;
50 }
51 User user = await _userService.GetById(userId);
52 return new AuthenticateResponse() { Email=user.Email, Id = user.Id, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed, isVip = user.IsVip};
53 }
54
55 [HttpPost("confirm")]
56 public async Task ConfirmEmail()
57 {
58 int userId = 0;
59 try
60 {
61 userId = (int)this.HttpContext.Items["User"];
62 }
63 catch (Exception ex) { return; }
64 User user = await _userService.GetById(userId);
65 await _userService.SendEmailConfirmation(user.Email);
66 }
67
68 [HttpPost("reset")]
69 public async Task ResetPassword(string email)
70 {
71 await _userService.SendPasswordReset(email);
72 }
73
74 [HttpPost("confirmed")]
75 public async Task ConfirmedEmail(string validityString)
76 {
77 int userId = 0;
78 try
79 {
80 userId = (int)this.HttpContext.Items["User"];
81 }
82 catch (Exception ex) { return; }
83 User user = await _userService.GetById(userId);
84 await _userService.ConfirmEmail(user, validityString);
85 }
86
87 [HttpPost("reseted")]
88 public async Task ResetedPassword(string validityString, string newPassword)
89 {
90 await _userService.ResetPassword(validityString, newPassword);
91 }
92
93 [HttpPost("register")]
94 public async Task<AuthenticateResponse> Register(CreateUserRequest req)
95 {
96 bool isFirst = await _restaurantService.GetRestaurant() == null;
97 var response = await _userService.Register(req, isFirst);
98 if (isFirst)
99 {
100 await _restaurantService.CreateRestaurant("", response.Id);
101 }
102 return response;
103 }
104
105 [Authorize]
106 [HttpGet()]
107 public async Task<List<UserResponse>> GetUsers()
108 {
109 return await _userService.GetUsers();
110 }
111
112 [Authorize]
113 [HttpPost("{id}/vip")]
114 public async Task UpdateVip(int id, bool newStatus)
115 {
116 await _userService.UpdateVipStatus(id,newStatus);
117 }
118}
Note: See TracBrowser for help on using the repository browser.