aboutsummaryrefslogtreecommitdiff
path: root/src/Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services')
-rw-r--r--src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Tests/RatingService.Tests.cs448
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs2
-rw-r--r--src/Services/DevHive.Services/Interfaces/IRatingService.cs4
-rw-r--r--src/Services/DevHive.Services/Services/PostService.cs10
-rw-r--r--src/Services/DevHive.Services/Services/RatingService.cs10
8 files changed, 465 insertions, 15 deletions
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs
index aaeab73..486e3d2 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class CreateRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs
index 86b4957..56a5ab0 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class ReadRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs
index 1ea8d8f..923e789 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class UpdateRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs
new file mode 100644
index 0000000..5f84530
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs
@@ -0,0 +1,448 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Rating;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ public class RatingServiceTests
+ {
+ private Mock<IPostRepository> _postRepositoryMock;
+ private Mock<IRatingRepository> _ratingRepositoryMock;
+ private Mock<IUserRepository> _userRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private RatingService _ratingService;
+
+ #region SetUps
+ [SetUp]
+ public void SetUp()
+ {
+ this._postRepositoryMock = new Mock<IPostRepository>();
+ this._ratingRepositoryMock = new Mock<IRatingRepository>();
+ this._userRepositoryMock = new Mock<IUserRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._ratingService = new RatingService(this._postRepositoryMock.Object, this._ratingRepositoryMock.Object, this._userRepositoryMock.Object, this._mapperMock.Object);
+ }
+ #endregion
+
+ #region Create
+ [Test]
+ public async Task RatePost_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ bool isLike = true;
+ Guid id = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel
+ {
+ PostId = postId,
+ UserId = userId,
+ IsLike = isLike
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = userId
+ };
+ Post post = new Post
+ {
+ Id = postId
+ };
+
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._ratingRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>()))
+ .Returns(rating);
+
+ Guid result = await this._ratingService.RatePost(createRatingServiceModel);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task RatePost_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ bool isLike = true;
+ Guid id = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel
+ {
+ PostId = postId,
+ UserId = userId,
+ IsLike = isLike
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = userId
+ };
+ Post post = new Post
+ {
+ Id = postId
+ };
+
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._ratingRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>()))
+ .Returns(rating);
+
+ Guid result = await this._ratingService.RatePost(createRatingServiceModel);
+
+ Assert.AreEqual(result, Guid.Empty);
+ }
+ #endregion
+
+ #region Read
+ [Test]
+ public async Task GetRatingById_ReturnsTheRating_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.GetRatingById(id);
+
+ Assert.AreEqual(isLike, result.IsLike);
+ }
+
+ [Test]
+ public void GetRatingById_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "The rating does not exist";
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public async Task GetRatingByPostAndUser_ReturnsTheRating_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.GetRatingByPostAndUser(user.Id, Guid.Empty);
+
+ Assert.AreEqual(isLike, result.IsLike);
+ }
+
+ [Test]
+ public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "The rating does not exist";
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region Update
+ [Test]
+ public async Task UpdateRating_ReturnsObject_WhenRatingIsUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel);
+
+ Assert.AreEqual(result, readRatingServiceModel);
+ }
+
+ [Test]
+ public async Task UpdateRating_ReturnsNull_WhenRatingIsNotUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+
+ ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel);
+
+ Assert.IsNull(result);
+ }
+
+ [Test]
+ public void UpdateRating_ThrowsException_WhenRatingDoesNotExists()
+ {
+ string exceptionMessage = "Rating does not exist!";
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = Guid.Empty,
+ IsLike = true
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+
+ [Test]
+ public void UpdateRating_ThrowsException_WhenUserHasNotRatedPost()
+ {
+ string exceptionMessage = "User has not rated the post!";
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+ #endregion
+
+ #region Delete
+ [Test]
+ public async Task DeleteRating_ReturnsTrue_WhenRatingIsDeletedSuccessfully()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Rating rating = new Rating
+ {
+ Id = ratingId
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+ this._ratingRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+
+ bool result = await this._ratingService.DeleteRating(ratingId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task DeleteRating_ReturnsFalse_WhenRatingIsNotDeletedSuccessfully()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Rating rating = new Rating
+ {
+ Id = ratingId
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+ this._ratingRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+
+ bool result = await this._ratingService.DeleteRating(ratingId);
+
+ Assert.IsFalse(result);
+ }
+
+ [Test]
+ public void DeleteRating_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "Rating does not exist!";
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.DeleteRating(Guid.Empty));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
index 4534511..9ad5f25 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
@@ -1,6 +1,6 @@
using AutoMapper;
using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Configurations.Mapping
{
diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
index beea821..be33300 100644
--- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs
+++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Interfaces
{
@@ -16,7 +16,5 @@ namespace DevHive.Services.Interfaces
Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel);
Task<bool> DeleteRating(Guid ratingId);
-
- Task<bool> HasUserRatedThisPost(Guid userId, Guid postId);
}
}
diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs
index a3d5117..a565473 100644
--- a/src/Services/DevHive.Services/Services/PostService.cs
+++ b/src/Services/DevHive.Services/Services/PostService.cs
@@ -76,11 +76,21 @@ namespace DevHive.Services.Services
User user = await this._userRepository.GetByIdAsync(post.Creator.Id) ??
throw new ArgumentException("The user does not exist!");
+ int currentRating = 0;
+ foreach (Rating rating in post.Ratings)
+ {
+ if (rating.IsLike)
+ currentRating++;
+ else
+ currentRating--;
+ }
+
ReadPostServiceModel readPostServiceModel = this._postMapper.Map<ReadPostServiceModel>(post);
readPostServiceModel.CreatorFirstName = user.FirstName;
readPostServiceModel.CreatorLastName = user.LastName;
readPostServiceModel.CreatorUsername = user.UserName;
readPostServiceModel.FileUrls = post.Attachments.Select(x => x.FileUrl).ToList();
+ readPostServiceModel.CurrentRating = currentRating;
return readPostServiceModel;
}
diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs
index 6ddba1c..1f77a6e 100644
--- a/src/Services/DevHive.Services/Services/RatingService.cs
+++ b/src/Services/DevHive.Services/Services/RatingService.cs
@@ -8,7 +8,7 @@ using AutoMapper;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Services
{
@@ -81,7 +81,7 @@ namespace DevHive.Services.Services
#region Update
public async Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel)
{
- Rating rating = await this._ratingRepository.GetByIdAsync(updateRatingServiceModel.Id) ??
+ Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId) ??
throw new ArgumentException("Rating does not exist!");
User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ??
@@ -115,11 +115,5 @@ namespace DevHive.Services.Services
return await this._ratingRepository.DeleteAsync(rating);
}
#endregion
-
- public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId)
- {
- return await this._ratingRepository
- .UserRatedPost(userId, postId);
- }
}
}