aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/PostController.cs
blob: 151c6888018a0807f7df3ef4f1bfddb73e15be68 (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
141
142
143
144
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,Admin")]
	public class PostController
	{
		private readonly IPostService _postService;
		private readonly IMapper _postMapper;

		public PostController(IPostService postService, IMapper postMapper)
		{
			this._postService = postService;
			this._postMapper = postMapper;
		}

		#region Create
		[HttpPost]
		public async Task<IActionResult> Create(Guid userId, [FromBody] CreatePostWebModel createPostWebModel)
		{
			CreatePostServiceModel createPostServiceModel =
				this._postMapper.Map<CreatePostServiceModel>(createPostWebModel);
			createPostServiceModel.CreatorId = userId;

			Guid id = await this._postService.CreatePost(createPostServiceModel);

			return id == Guid.Empty ?
				new BadRequestObjectResult("Could not create post!") :
				new OkObjectResult(new { Id = id });
		}

		[HttpPost]
		[Route("Comment")]
		public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel)
		{
			CreateCommentServiceModel createCommentServiceModel =
				this._postMapper.Map<CreateCommentServiceModel>(createCommentWebModel);
			createCommentServiceModel.CreatorId = userId;

			Guid id = await this._postService.AddComment(createCommentServiceModel);

			return id == Guid.Empty ?
				new BadRequestObjectResult("Could not create comment!") :
				new OkObjectResult(new { Id = id });
		}
		#endregion

		#region Read
		[HttpGet]
		[AllowAnonymous]
		public async Task<IActionResult> GetById(Guid id)
		{
			ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id);
			ReadPostWebModel postWebModel = this._postMapper.Map<ReadPostWebModel>(postServiceModel);

			return new OkObjectResult(postWebModel);
		}

		[HttpGet]
		[Route("Comment")]
		[AllowAnonymous]
		public async Task<IActionResult> GetCommentById(Guid id)
		{
			ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id);
			ReadCommentWebModel readCommentWebModel = this._postMapper.Map<ReadCommentWebModel>(readCommentServiceModel);

			return new OkObjectResult(readCommentWebModel);
		}
		#endregion

		#region Update
		[HttpPut]
		public async Task<IActionResult> Update(Guid userId, [FromBody] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization)
		{
			if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization))
				return new UnauthorizedResult();

			UpdatePostServiceModel updatePostServiceModel =
				this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel);
			updatePostServiceModel.CreatorId = userId;

			Guid id = await this._postService.UpdatePost(updatePostServiceModel);

			return id == Guid.Empty ?
				new BadRequestObjectResult("Unable to update post!") :
				new OkObjectResult(new { Id = id });
		}

		[HttpPut]
		[Route("Comment")]
		public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization)
		{
			if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization))
				return new UnauthorizedResult();

			UpdateCommentServiceModel updateCommentServiceModel =
				this._postMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel);
			updateCommentServiceModel.CreatorId = userId;

			Guid id = await this._postService.UpdateComment(updateCommentServiceModel);

			return id == Guid.Empty ?
				new BadRequestObjectResult("Unable to update comment!") :
				new OkObjectResult(new { Id = id });
		}
		#endregion

		#region Delete
		[HttpDelete]
		public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization)
		{
			if (!await this._postService.ValidateJwtForPost(id, authorization))
				return new UnauthorizedResult();

			return await this._postService.DeletePost(id) ?
				new OkResult() :
				new BadRequestObjectResult("Could not delete Comment");
		}

		[HttpDelete]
		[Route("Comment")]
		public async Task<IActionResult> DeleteComment(Guid id, [FromHeader] string authorization)
		{
			if (!await this._postService.ValidateJwtForComment(id, authorization))
				return new UnauthorizedResult();

			return await this._postService.DeleteComment(id) ?
				new OkResult() :
				new BadRequestObjectResult("Could not delete Comment");
		}
		#endregion
	}
}