Repository navigation
Expand file tree
/
Copy pathUsersController.cs
More file actions
78 lines (68 loc) · 2.52 KB
/
Copy pathUsersController.cs
File metadata and controls
78 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Blog.Common.Exceptions;
using Blog.Common.Models.User;
using Blog.Services.Api;
using Blog.Services.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Blog.API.Controllers
{
[Route("api/users")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly UserService _userService;
private readonly UserHelper _userHelper;
public UsersController(UserService userService, UserHelper userHelper)
{
_userService = userService;
_userHelper = userHelper;
}
// GET api/users -> barcha foydalanuvchilar (login/parol chiqarilmaydi, faqat ochiq ma'lumot)
[HttpGet]
public async Task<IActionResult> GetAllUsers()
{
var users = await _userService.GetAllUsers();
return Ok(users);
}
[HttpGet("{userId:guid}")]
[Authorize]
public async Task<IActionResult> GetUserById(Guid userId)
{
var user = await _userService.GetUserById(userId);
return Ok(user);
}
// POST api/users/register -> ro'yxatdan o'tish, darhol token bilan qaytadi
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] CreateUserModel model)
{
var result = await _userService.AddUser(model);
return Ok(result);
}
// POST api/users/login -> token bilan qaytadi
[HttpPost("login")]
public async Task<IActionResult> Login([FromBody] LoginUserModel model)
{
var result = await _userService.Login(model);
return Ok(result);
}
[HttpPut("{userId:guid}")]
[Authorize]
public async Task<IActionResult> UpdateUser(Guid userId, [FromBody] UpdateUserModel model)
{
// Faqat o'zining profilini o'zgartira oladi
if (_userHelper.UserId != userId)
throw new ForbiddenException("Faqat o'zingizning profilingizni o'zgartira olasiz");
var user = await _userService.UpdateUser(userId, model);
return Ok(user);
}
[HttpDelete("{userId:guid}")]
[Authorize]
public async Task<IActionResult> DeleteUser(Guid userId)
{
if (_userHelper.UserId != userId)
throw new ForbiddenException("Faqat o'zingizning profilingizni o'chira olasiz");
var message = await _userService.DeleteUser(userId);
return Ok(new { message });
}
}
}