From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../Post/ReadPostServiceModel.cs | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs (limited to 'src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs') diff --git a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs new file mode 100644 index 0000000..a7aa882 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Common.Models.Misc; + +namespace DevHive.Services.Models.Post +{ + public class ReadPostServiceModel + { + public Guid PostId { get; set; } + + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } = new(); + + public List FileUrls { get; set; } + // public List Files { get; set; } = new(); + } +} -- cgit v1.2.3 From a851adfac47a26cae83e9161d37902a219e5ebf3 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sun, 21 Feb 2021 22:21:08 +0200 Subject: model update for rating --- src/Data/DevHive.Data.Models/Interfaces/IRating.cs | 7 +- src/Data/DevHive.Data.Models/Post.cs | 4 +- src/Data/DevHive.Data.Models/Rating.cs | 5 +- src/Data/DevHive.Data/DevHiveContext.cs | 5 +- .../DevHive.Data/Interfaces/IRatingRepository.cs | 3 +- .../DevHive.Data/Repositories/PostRepository.cs | 2 +- .../DevHive.Data/Repositories/RatingRepository.cs | 5 +- .../Post/Rating/CreateRatingServiceModel.cs | 13 ++++ .../Post/Rating/RatePostServiceModel.cs | 13 ---- .../Post/Rating/ReadPostRatingServiceModel.cs | 15 ---- .../Post/Rating/ReadRatingServiceModel.cs | 15 ++++ .../Post/ReadPostServiceModel.cs | 3 +- .../DevHive.Services/Interfaces/IRateService.cs | 14 ---- .../DevHive.Services/Interfaces/IRatingService.cs | 14 ++++ .../DevHive.Services/Services/PostService.cs | 1 + .../DevHive.Services/Services/RateService.cs | 80 ---------------------- .../DevHive.Services/Services/RatingService.cs | 80 ++++++++++++++++++++++ .../DevHive.Web.Models/Post/ReadPostWebModel.cs | 2 + .../Extensions/ConfigureDependencyInjection.cs | 2 +- .../Configurations/Mapping/RatingMappings.cs | 4 +- src/Web/DevHive.Web/Controllers/RateController.cs | 8 +-- 21 files changed, 152 insertions(+), 143 deletions(-) create mode 100644 src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services/Interfaces/IRateService.cs create mode 100644 src/Services/DevHive.Services/Interfaces/IRatingService.cs delete mode 100644 src/Services/DevHive.Services/Services/RateService.cs create mode 100644 src/Services/DevHive.Services/Services/RatingService.cs (limited to 'src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs') diff --git a/src/Data/DevHive.Data.Models/Interfaces/IRating.cs b/src/Data/DevHive.Data.Models/Interfaces/IRating.cs index 1b081b0..f04f823 100644 --- a/src/Data/DevHive.Data.Models/Interfaces/IRating.cs +++ b/src/Data/DevHive.Data.Models/Interfaces/IRating.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using DevHive.Data.Models; @@ -5,10 +6,10 @@ namespace DevHive.Data.Models.Interfaces { public interface IRating : IModel { - // Post Post { get; set; } + bool IsLike { get; set; } - int Rate { get; set; } + Post Post { get; set; } - // HashSet UsersThatRated { get; set; } + User User { get; set; } } } diff --git a/src/Data/DevHive.Data.Models/Post.cs b/src/Data/DevHive.Data.Models/Post.cs index 15b6b77..716248f 100644 --- a/src/Data/DevHive.Data.Models/Post.cs +++ b/src/Data/DevHive.Data.Models/Post.cs @@ -19,7 +19,9 @@ namespace DevHive.Data.Models public List Comments { get; set; } = new(); - public Rating Rating { get; set; } = new(); + public List Ratings { get; set; } + + public int CurrentRating { get; set; } public List Attachments { get; set; } = new(); } diff --git a/src/Data/DevHive.Data.Models/Rating.cs b/src/Data/DevHive.Data.Models/Rating.cs index 13fdbce..8743a3e 100644 --- a/src/Data/DevHive.Data.Models/Rating.cs +++ b/src/Data/DevHive.Data.Models/Rating.cs @@ -6,12 +6,13 @@ namespace DevHive.Data.Models { public class Rating : IRating { + //if adding rating to comments change Post for intreface IRatable! public Guid Id { get; set; } - public Guid PostId { get; set; } + public User User { get; set; } public Post Post { get; set; } - public int Rate { get; set; } + public bool IsLike { get; set; } } } diff --git a/src/Data/DevHive.Data/DevHiveContext.cs b/src/Data/DevHive.Data/DevHiveContext.cs index ece3439..b0bbdc6 100644 --- a/src/Data/DevHive.Data/DevHiveContext.cs +++ b/src/Data/DevHive.Data/DevHiveContext.cs @@ -88,11 +88,10 @@ namespace DevHive.Data builder.Entity() .HasOne(x => x.Post) - .WithOne(x => x.Rating) - .HasForeignKey(x => x.PostId); + .WithMany(x => x.Ratings); builder.Entity() - .HasOne(x => x.Rating) + .HasMany(x => x.Ratings) .WithOne(x => x.Post); /* User Rated Posts */ diff --git a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs index b5aff88..c8636b6 100644 --- a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories.Interfaces; @@ -7,7 +8,7 @@ namespace DevHive.Data.Interfaces { public interface IRatingRepository : IRepository { - Task GetRatingByPostId(Guid postId); + Task> GetRatingsByPostId(Guid postId); Task UserRatedPost(Guid userId, Guid postId); } } diff --git a/src/Data/DevHive.Data/Repositories/PostRepository.cs b/src/Data/DevHive.Data/Repositories/PostRepository.cs index b6c5e37..0ab1afb 100644 --- a/src/Data/DevHive.Data/Repositories/PostRepository.cs +++ b/src/Data/DevHive.Data/Repositories/PostRepository.cs @@ -36,7 +36,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Comments) .Include(x => x.Creator) .Include(x => x.Attachments) - // .Include(x => x.Rating) + .Include(x => x.CurrentRating) .FirstOrDefaultAsync(x => x.Id == id); } diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index b361693..2f56aee 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces; @@ -19,10 +20,10 @@ namespace DevHive.Data.Repositories this._postRepository = postRepository; } - public async Task GetRatingByPostId(Guid postId) + public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating - .FirstOrDefaultAsync(x => x.Post.Id == postId); + .Where(x => x.Post.Id == postId).ToListAsync(); } public async Task UserRatedPost(Guid userId, Guid postId) diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs new file mode 100644 index 0000000..aaeab73 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class CreateRatingServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs deleted file mode 100644 index d4eb7bd..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -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/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs deleted file mode 100644 index 8c73aaf..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class ReadPostRatingServiceModel - { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - - public int Likes { get; set; } - - public int Dislikes { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..dbc7ecc --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class ReadRatingServiceModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs index a7aa882..33d6520 100644 --- a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs +++ b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs @@ -21,6 +21,7 @@ namespace DevHive.Services.Models.Post public List Comments { get; set; } = new(); public List FileUrls { get; set; } - // public List Files { get; set; } = new(); + + public int CurrentRating { get; set; } } } diff --git a/src/Services/DevHive.Services/Interfaces/IRateService.cs b/src/Services/DevHive.Services/Interfaces/IRateService.cs deleted file mode 100644 index 359ef55..0000000 --- a/src/Services/DevHive.Services/Interfaces/IRateService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; - -namespace DevHive.Services.Interfaces -{ - public interface IRateService - { - Task RatePost(RatePostServiceModel ratePostServiceModel); - - bool HasUserRatedThisPost(User user, Post post); - } -} diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs new file mode 100644 index 0000000..adb4313 --- /dev/null +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.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 IRatingService + { + Task RatePost(CreateRatingServiceModel ratePostServiceModel); + + bool HasUserRatedThisPost(User user, Post post); + } +} diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs index a3d5117..4bece90 100644 --- a/src/Services/DevHive.Services/Services/PostService.cs +++ b/src/Services/DevHive.Services/Services/PostService.cs @@ -46,6 +46,7 @@ namespace DevHive.Services.Services post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); post.TimeCreated = DateTime.Now; + post.CurrentRating = 0; bool success = await this._postRepository.AddAsync(post); if (success) diff --git a/src/Services/DevHive.Services/Services/RateService.cs b/src/Services/DevHive.Services/Services/RateService.cs deleted file mode 100644 index caf4b80..0000000 --- a/src/Services/DevHive.Services/Services/RateService.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Interfaces; -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 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(newRating); - } - - public async Task 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/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs new file mode 100644 index 0000000..45ff7c0 --- /dev/null +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -0,0 +1,80 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Rating; + +namespace DevHive.Services.Services +{ + public class RatingService : IRatingService + { + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; + private readonly IRatingRepository _ratingRepository; + private readonly IMapper _mapper; + + public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper) + { + this._postRepository = postRepository; + this._ratingRepository = ratingRepository; + this._userRepository = userRepository; + this._mapper = mapper; + } + + public async Task RatePost(CreateRatingServiceModel 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(newRating); + } + + public async Task 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/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs index 3ae93aa..d6ea1f4 100644 --- a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs +++ b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs @@ -21,5 +21,7 @@ namespace DevHive.Web.Models.Post public List Comments { get; set; } public List FileUrls { get; set; } + + public int CurrentRating { get; set; } } } diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index c547951..153b17f 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -32,7 +32,7 @@ namespace DevHive.Web.Configurations.Extensions cloudName: configuration.GetSection("Cloud").GetSection("cloudName").Value, apiKey: configuration.GetSection("Cloud").GetSection("apiKey").Value, apiSecret: configuration.GetSection("Cloud").GetSection("apiSecret").Value)); - services.AddTransient(); + services.AddTransient(); } } } diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs index a29e06c..1b43b2b 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -8,9 +8,9 @@ namespace DevHive.Web.Configurations.Mapping { public RatingMappings() { - CreateMap(); + CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/Web/DevHive.Web/Controllers/RateController.cs b/src/Web/DevHive.Web/Controllers/RateController.cs index 72eb932..7f8a95f 100644 --- a/src/Web/DevHive.Web/Controllers/RateController.cs +++ b/src/Web/DevHive.Web/Controllers/RateController.cs @@ -13,11 +13,11 @@ namespace DevHive.Web.Controllers [Route("api/[controller]")] public class RateController { - private readonly IRateService _rateService; + private readonly IRatingService _rateService; private readonly IUserService _userService; private readonly IMapper _mapper; - public RateController(IRateService rateService, IUserService userService, IMapper mapper) + public RateController(IRatingService rateService, IUserService userService, IMapper mapper) { this._rateService = rateService; this._userService = userService; @@ -28,10 +28,10 @@ namespace DevHive.Web.Controllers [Authorize(Roles = "Admin,User")] public async Task RatePost(Guid userId, [FromBody] RatePostWebModel ratePostWebModel, [FromHeader] string authorization) { - RatePostServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); + CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); ratePostServiceModel.UserId = userId; - ReadPostRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); + ReadRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map(readPostRatingServiceModel); return new OkObjectResult(readPostRatingWebModel); -- cgit v1.2.3