Repository navigation
Expand file tree
/
Copy pathPostsController.cs
More file actions
88 lines (76 loc) · 2.94 KB
/
Copy pathPostsController.cs
File metadata and controls
88 lines (76 loc) · 2.94 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
79
80
81
82
83
84
85
86
87
88
using Blog.Common.Exceptions;
using Blog.Common.Models.Post;
using Blog.Services.Api;
using Blog.Services.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Blog.API.Controllers
{
[Route("api")]
[ApiController]
public class PostsController : ControllerBase
{
private readonly PostService _postService;
private readonly UserHelper _userHelper;
public PostsController(PostService postService, UserHelper userHelper)
{
_postService = postService;
_userHelper = userHelper;
}
// ---- OCHIQ: umumiy post-feed ----
[HttpGet("posts")]
public async Task<IActionResult> GetAllPosts()
{
var posts = await _postService.GetAllPosts();
return Ok(posts);
}
[HttpGet("posts/{postId:int}")]
public async Task<IActionResult> GetPostById(int postId)
{
var post = await _postService.GetPostById(postId);
return Ok(post);
}
// ---- Muayyan blogga tegishli postlar ----
[HttpGet("users/{userId:guid}/blogs/{blogId:int}/posts")]
public async Task<IActionResult> GetBlogPosts(Guid userId, int blogId)
{
var posts = await _postService.GetBlogPosts(userId, blogId);
return Ok(posts);
}
[HttpGet("users/{userId:guid}/blogs/{blogId:int}/posts/{postId:int}")]
public async Task<IActionResult> GetBlogPostById(Guid userId, int blogId, int postId)
{
var post = await _postService.GetBlogPostById(userId, blogId, postId);
return Ok(post);
}
[HttpPost("users/{userId:guid}/blogs/{blogId:int}/posts")]
[Authorize]
public async Task<IActionResult> AddPost(Guid userId, int blogId, [FromBody] CreatePostModel model)
{
EnsureOwner(userId);
var post = await _postService.AddPost(userId, blogId, model);
return Ok(post);
}
[HttpPut("users/{userId:guid}/blogs/{blogId:int}/posts/{postId:int}")]
[Authorize]
public async Task<IActionResult> UpdatePost(Guid userId, int blogId, int postId, [FromBody] UpdatePostModel model)
{
EnsureOwner(userId);
var post = await _postService.UpdatePost(userId, blogId, postId, model);
return Ok(post);
}
[HttpDelete("users/{userId:guid}/blogs/{blogId:int}/posts/{postId:int}")]
[Authorize]
public async Task<IActionResult> DeletePost(Guid userId, int blogId, int postId)
{
EnsureOwner(userId);
var message = await _postService.DeletePost(userId, blogId, postId);
return Ok(new { message });
}
private void EnsureOwner(Guid userId)
{
if (_userHelper.UserId != userId)
throw new ForbiddenException("Faqat o'zingizning postlaringizni boshqarishingiz mumkin");
}
}
}