From 866a5a15b8b722bc78d065f73adc0c465f264f55 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 30 Jan 2021 18:45:18 +0200 Subject: IDFK --- .../Configurations/Mapping/RatingMappings.cs | 12 +++++ .../Models/Post/Rating/RatePostServiceModel.cs | 13 ++++++ .../Models/Post/Rating/ReadRatingServiceModel.cs | 13 ++++++ src/DevHive.Services/Services/RatingService.cs | 54 ++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 src/DevHive.Services/Configurations/Mapping/RatingMappings.cs create mode 100644 src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs create mode 100644 src/DevHive.Services/Services/RatingService.cs (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs new file mode 100644 index 0000000..5da1b0d --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -0,0 +1,12 @@ +using AutoMapper; + +namespace DevHive.Services.Configurations.Mapping +{ + public class RatingMappings : Profile + { + public RatingMappings() + { + + } + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs new file mode 100644 index 0000000..d4eb7bd --- /dev/null +++ b/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class RatePostServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool Liked { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..b071e74 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class ReadRatingServiceModel + { + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} diff --git a/src/DevHive.Services/Services/RatingService.cs b/src/DevHive.Services/Services/RatingService.cs new file mode 100644 index 0000000..2c5a6b6 --- /dev/null +++ b/src/DevHive.Services/Services/RatingService.cs @@ -0,0 +1,54 @@ +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 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(newRating); + } + + public async Task RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + } + } +} -- cgit v1.2.3