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

namespace DevHive.Services.Services
{
	public class CommentService
	{
		private readonly CommentRepository _commentRepository;
		private readonly IMapper _commentMapper;

		public CommentService(CommentRepository commentRepository, IMapper mapper)
		{
			this._commentRepository = commentRepository;
			this._commentMapper = mapper;
		}

		public async Task<bool> CreateComment(CommentServiceModel commentServiceModel)
		{
			Comment comment = this._commentMapper.Map<Comment>(commentServiceModel);
			comment.Date = DateTime.Now;
			bool result = await this._commentRepository.AddAsync(comment);

			return result;
		}
	
		public async Task<GetByIdCommentServiceModel> GetCommentById(Guid id)
		{
			Comment comment = await this._commentRepository.GetByIdAsync(id);

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

			return this._commentMapper.Map<GetByIdCommentServiceModel>(comment);
		}

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

			Comment comment = this._commentMapper.Map<Comment>(commentServiceModel);
			bool result = await this._commentRepository.EditAsync(comment);

			return result;
		}
	
		public async Task<bool> DeleteComment(Guid id)
		{
			if (!await this._commentRepository.DoesCommentExist(id))
				throw new ArgumentException("Comment does not exist!");

			Comment comment = await this._commentRepository.GetByIdAsync(id);
			bool result = await this._commentRepository.DeleteAsync(comment);

			return result;
		}
	}
}