aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services/PostService.cs
blob: 0c0fd5c10269804d8be97f63c8b20de0d151a41c (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
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Models;
using DevHive.Data.Repositories;
using DevHive.Services.Models.Post.Comment;
using DevHive.Services.Models.Post.Post;

namespace DevHive.Services.Services
{
	public class PostService
	{
		private readonly PostRepository _postRepository;
		private readonly IMapper _postMapper;

		public PostService(PostRepository postRepository, IMapper postMapper)
		{
			this._postRepository = postRepository;
			this._postMapper = postMapper;
		}

		//Create
		public async Task<bool> CreatePost(CreatePostServiceModel postServiceModel)
		{
			Post post = this._postMapper.Map<Post>(postServiceModel);

			return await this._postRepository.AddAsync(post);
		}

		public async Task<bool> AddComment(CreateCommentServiceModel commentServiceModel)
		{
			commentServiceModel.TimeCreated = DateTime.Now;
			Comment comment = this._postMapper.Map<Comment>(commentServiceModel);
			
			bool result = await this._postRepository.AddCommentAsync(comment);

			return result;
		}
	
		//Read
		public async Task<PostServiceModel> GetPostById(Guid id)
		{
			Post post = await this._postRepository.GetByIdAsync(id) 
				?? throw new ArgumentException("Post does not exist!");

			return this._postMapper.Map<PostServiceModel>(post);
		}

		public async Task<CommentServiceModel> GetCommentById(Guid id)
		{
			Comment comment = await this._postRepository.GetCommentByIdAsync(id);

			if(comment == null)
				throw new ArgumentException("The comment does not exist");

			return this._postMapper.Map<CommentServiceModel>(comment);
		}

		//Update
		public async Task<bool> UpdatePost(UpdatePostServiceModel postServiceModel)
		{
			if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId))
				throw new ArgumentException("Comment does not exist!");

			Post post = this._postMapper.Map<Post>(postServiceModel);
			return await this._postRepository.EditAsync(post);
		}

		public async Task<bool> UpdateComment(UpdateCommentServiceModel commentServiceModel)
		{
			if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id))
				throw new ArgumentException("Comment does not exist!");

			Comment comment = this._postMapper.Map<Comment>(commentServiceModel);
			bool result = await this._postRepository.EditCommentAsync(comment);

			return result;
		}

		//Delete
		public async Task<bool> DeletePost(Guid id)
		{
			Post post = await this._postRepository.GetByIdAsync(id);
			return await this._postRepository.DeleteAsync(post);
		}
	
		public async Task<bool> DeleteComment(Guid id)
		{
			if (!await this._postRepository.DoesCommentExist(id))
				throw new ArgumentException("Comment does not exist!");

			Comment comment = await this._postRepository.GetCommentByIdAsync(id);
			bool result = await this._postRepository.DeleteCommentAsync(comment);

			return result;
		}
	}
}