blob: 15adb1bddb5e9af52366c684bd5d20fcc81d5323 (
plain) (
blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using System;
using DevHive.Web.Models.Post.Post;
using DevHive.Services.Models.Post.Post;
using DevHive.Web.Models.Post.Comment;
using DevHive.Services.Models.Post.Comment;
using Microsoft.AspNetCore.Authorization;
using DevHive.Services.Interfaces;
namespace DevHive.Web.Controllers
{
[ApiController]
[Route("/api/[controller]")]
[Authorize(Roles = "User")]
public class PostController
{
private readonly IPostService _postService;
private readonly IMapper _postMapper;
public PostController(IPostService postService, IMapper mapper)
{
this._postService = postService;
this._postMapper = mapper;
}
//Create
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreatePostWebModel createPostModel)
{
CreatePostServiceModel postServiceModel =
this._postMapper.Map<CreatePostServiceModel>(createPostModel);
bool result = await this._postService.CreatePost(postServiceModel);
if (!result)
return new BadRequestObjectResult("Could not create post!");
return new OkResult();
}
[HttpPost]
[Route("Comment")]
public async Task<IActionResult> AddComment([FromBody] CommentWebModel commentWebModel)
{
CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map<CreateCommentServiceModel>(commentWebModel);
bool result = await this._postService.AddComment(createCommentServiceModel);
if (!result)
return new BadRequestObjectResult("Could not create the Comment");
return new OkResult();
}
//Read
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> GetById(Guid id)
{
PostServiceModel postServiceModel = await this._postService.GetPostById(id);
PostWebModel postWebModel = this._postMapper.Map<PostWebModel>(postServiceModel);
return new OkObjectResult(postWebModel);
}
[HttpGet]
[Route("Comment")]
[AllowAnonymous]
public async Task<IActionResult> GetCommentById(Guid id)
{
CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id);
CommentWebModel commentWebModel = this._postMapper.Map<CommentWebModel>(commentServiceModel);
return new OkObjectResult(commentWebModel);
}
//Update
[HttpPut]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel)
{
UpdatePostServiceModel postServiceModel =
this._postMapper.Map<UpdatePostServiceModel>(updatePostModel);
postServiceModel.IssuerId = id;
bool result = await this._postService.UpdatePost(postServiceModel);
if (!result)
return new BadRequestObjectResult("Could not update post!");
return new OkResult();
}
[HttpPut]
[Route("Comment")]
public async Task<IActionResult> UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel, [FromHeader] string authorization)
{
if (!await this._postService.ValidateJwtForComment(id, authorization))
return new UnauthorizedResult();
UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map<UpdateCommentServiceModel>(commentWebModel);
updateCommentServiceModel.Id = id;
bool result = await this._postService.UpdateComment(updateCommentServiceModel);
if (!result)
return new BadRequestObjectResult("Could not update Comment");
return new OkResult();
}
//Delete
[HttpDelete]
public async Task<IActionResult> Delete(Guid id)
{
bool result = await this._postService.DeletePost(id);
if (!result)
return new BadRequestObjectResult("Could not delete post!");
return new OkResult();
}
[HttpDelete]
[Route("Comment")]
public async Task<IActionResult> DeleteComment(Guid id, [FromHeader] string authorization)
{
if (!await this._postService.ValidateJwtForComment(id, authorization))
return new UnauthorizedResult();
bool result = await this._postService.DeleteComment(id);
if (!result)
return new BadRequestObjectResult("Could not delete Comment");
return new OkResult();
}
}
}
|