aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/RatingMappings.cs5
-rw-r--r--src/DevHive.Services/Interfaces/IRateService.cs14
-rw-r--r--src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs (renamed from src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs)4
-rw-r--r--src/DevHive.Services/Services/RateService.cs80
-rw-r--r--src/DevHive.Services/Services/RatingService.cs54
5 files changed, 101 insertions, 56 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs
index 5da1b0d..1dbb7b4 100644
--- a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs
@@ -1,4 +1,6 @@
using AutoMapper;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Post.Rating;
namespace DevHive.Services.Configurations.Mapping
{
@@ -6,7 +8,8 @@ namespace DevHive.Services.Configurations.Mapping
{
public RatingMappings()
{
-
+ // CreateMap<Rating, ReadPostRatingServiceModel>()
+ // .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Post.Id));
}
}
}
diff --git a/src/DevHive.Services/Interfaces/IRateService.cs b/src/DevHive.Services/Interfaces/IRateService.cs
new file mode 100644
index 0000000..359ef55
--- /dev/null
+++ b/src/DevHive.Services/Interfaces/IRateService.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Post.Rating;
+
+namespace DevHive.Services.Interfaces
+{
+ public interface IRateService
+ {
+ Task<ReadPostRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel);
+
+ bool HasUserRatedThisPost(User user, Post post);
+ }
+}
diff --git a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs
index b071e74..8c73aaf 100644
--- a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs
@@ -2,8 +2,10 @@ using System;
namespace DevHive.Services.Models.Post.Rating
{
- public class ReadRatingServiceModel
+ public class ReadPostRatingServiceModel
{
+ public Guid Id { get; set; }
+
public Guid PostId { get; set; }
public int Likes { get; set; }
diff --git a/src/DevHive.Services/Services/RateService.cs b/src/DevHive.Services/Services/RateService.cs
new file mode 100644
index 0000000..204c550
--- /dev/null
+++ b/src/DevHive.Services/Services/RateService.cs
@@ -0,0 +1,80 @@
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post.Rating;
+
+namespace DevHive.Services.Services
+{
+ public class RateService : IRateService
+ {
+ private readonly IPostRepository _postRepository;
+ private readonly IUserRepository _userRepository;
+ private readonly IRatingRepository _ratingRepository;
+ private readonly IMapper _mapper;
+
+ public RateService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper)
+ {
+ this._postRepository = postRepository;
+ this._ratingRepository = ratingRepository;
+ this._userRepository = userRepository;
+ this._mapper = mapper;
+ }
+
+ public async Task<ReadPostRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel)
+ {
+ throw new NotImplementedException();
+ // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId))
+ // throw new ArgumentException("Post does not exist!");
+
+ // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId))
+ // throw new ArgumentException("User does not exist!");
+
+ // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId);
+ // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId);
+
+ // if (this.HasUserRatedThisPost(user, post))
+ // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)");
+
+ // this.Rate(user, post, ratePostServiceModel.Liked);
+
+ // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating);
+ // if (!success)
+ // throw new InvalidOperationException("Unable to rate the post!");
+
+ // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id);
+ // return this._mapper.Map<ReadPostRatingServiceModel>(newRating);
+ }
+
+ public async Task<ReadPostRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId)
+ {
+ throw new NotImplementedException();
+ // Post post = await this._postRepository.GetByIdAsync(postId);
+ // User user = await this._userRepository.GetByIdAsync(userId);
+
+ // if (!this.HasUserRatedThisPost(user, post))
+ // throw new ArgumentException("You haven't rated this post, lmao!");
+ }
+
+ public bool HasUserRatedThisPost(User user, Post post)
+ {
+ throw new NotImplementedException();
+ // return post.Rating.UsersThatRated
+ // .Any(x => x.Id == user.Id);
+ }
+
+ private void Rate(User user, Post post, bool liked)
+ {
+ throw new NotImplementedException();
+ // if (liked)
+ // post.Rating.Rate++;
+ // else
+ // post.Rating.Rate--;
+
+ // post.Rating.UsersThatRated.Add(user);
+ }
+ }
+}
diff --git a/src/DevHive.Services/Services/RatingService.cs b/src/DevHive.Services/Services/RatingService.cs
deleted file mode 100644
index 2c5a6b6..0000000
--- a/src/DevHive.Services/Services/RatingService.cs
+++ /dev/null
@@ -1,54 +0,0 @@
-using System;
-using System.Diagnostics;
-using System.Threading.Tasks;
-using AutoMapper;
-using DevHive.Data.Interfaces.Repositories;
-using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
-using Microsoft.CodeAnalysis.CSharp.Syntax;
-
-namespace DevHive.Services.Services
-{
- public class RatingService
- {
- private readonly IPostRepository _postRepository;
- private readonly IRatingRepository _ratingRepository;
- private readonly IMapper _mapper;
-
- public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IMapper mapper)
- {
- this._postRepository = postRepository;
- this._ratingRepository = ratingRepository;
- this._mapper = mapper;
- }
-
- public async Task<ReadRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel)
- {
- if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId))
- throw new ArgumentNullException("Post does not exist!");
-
- if (!await this._ratingRepository.HasUserRatedThisPost(ratePostServiceModel.UserId, ratePostServiceModel.PostId))
- throw new ArgumentException("You can't rate the same post more then one(duh, amigo)");
-
- Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId);
-
- Rating rating = post.Rating;
- if (ratePostServiceModel.Liked)
- rating.Likes++;
- else
- rating.Dislikes++;
-
- bool success = await this._ratingRepository.EditAsync(rating.Id, rating);
- if (!success)
- throw new InvalidOperationException("Unable to rate the post!");
-
- Rating newRating = await this._ratingRepository.GetByIdAsync(rating.Id);
- return this._mapper.Map<ReadRatingServiceModel>(newRating);
- }
-
- public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId)
- {
- throw new NotImplementedException();
- }
- }
-}