source: resTools_backend/backend/Controllers/UsersController.cs

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

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

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[b66b3ac]1namespace backend.Controllers;
2
[057037b]3using backend.DTOs;
[d76b7ee]4using backend.Entities;
[b66b3ac]5using backend.Helpers;
6using backend.Models;
7using backend.Services;
8using Microsoft.AspNetCore.Mvc;
9
10[ApiController]
11[Route("[controller]")]
12public class UsersController : ControllerBase
13{
[057037b]14 private readonly IUserService _userService = null;
[899b19d]15 private readonly IRestaurantService _restaurantService = null;
[b66b3ac]16
[899b19d]17 public UsersController(IUserService userService, IRestaurantService restaurantService)
[b66b3ac]18 {
19 _userService = userService;
[899b19d]20 _restaurantService = restaurantService;
[b66b3ac]21 }
22
[057037b]23 [HttpPost("login")]
24 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
[b66b3ac]25 {
[057037b]26 var response = await _userService.Authenticate(model);
[b66b3ac]27
28 if (response == null)
[057037b]29 throw new Exception("Email or password is incorrect");
[b66b3ac]30
[057037b]31 return response;
[b66b3ac]32 }
33
[d76b7ee]34 [HttpGet("authed")]
35 public async Task<AuthenticateResponse> IsAuthed()
36 {
37 int userId = 0;
38 try
39 {
40 userId = (int)this.HttpContext.Items["User"];
[13f1472]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 }
[d76b7ee]51 User user = await _userService.GetById(userId);
[13f1472]52 return new AuthenticateResponse() { Email=user.Email, Id = user.Id, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed, isVip = user.IsVip};
[a26f6a1]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);
[d76b7ee]91 }
92
[057037b]93 [HttpPost("register")]
94 public async Task<AuthenticateResponse> Register(CreateUserRequest req)
[b66b3ac]95 {
[899b19d]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 }
[057037b]102 return response;
[b66b3ac]103 }
[13f1472]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 }
[b66b3ac]118}
Note: See TracBrowser for help on using the repository browser.