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 --- src/DevHive.Data/Repositories/BaseRepository.cs | 59 ----------- src/DevHive.Data/Repositories/CommentRepository.cs | 74 -------------- src/DevHive.Data/Repositories/FeedRepository.cs | 77 --------------- .../Repositories/LanguageRepository.cs | 53 ---------- src/DevHive.Data/Repositories/PostRepository.cs | 105 -------------------- src/DevHive.Data/Repositories/RatingRepository.cs | 35 ------- src/DevHive.Data/Repositories/RoleRepository.cs | 55 ----------- .../Repositories/TechnologyRepository.cs | 53 ---------- src/DevHive.Data/Repositories/UserRepository.cs | 108 --------------------- 9 files changed, 619 deletions(-) delete mode 100644 src/DevHive.Data/Repositories/BaseRepository.cs delete mode 100644 src/DevHive.Data/Repositories/CommentRepository.cs delete mode 100644 src/DevHive.Data/Repositories/FeedRepository.cs delete mode 100644 src/DevHive.Data/Repositories/LanguageRepository.cs delete mode 100644 src/DevHive.Data/Repositories/PostRepository.cs delete mode 100644 src/DevHive.Data/Repositories/RatingRepository.cs delete mode 100644 src/DevHive.Data/Repositories/RoleRepository.cs delete mode 100644 src/DevHive.Data/Repositories/TechnologyRepository.cs delete mode 100644 src/DevHive.Data/Repositories/UserRepository.cs (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs deleted file mode 100644 index ece372e..0000000 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Repositories.Interfaces; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class BaseRepository : IRepository - where TEntity : class - { - private readonly DbContext _context; - - public BaseRepository(DbContext context) - { - this._context = context; - } - - public virtual async Task AddAsync(TEntity entity) - { - await this._context - .Set() - .AddAsync(entity); - - return await this.SaveChangesAsync(); - } - - public virtual async Task GetByIdAsync(Guid id) - { - return await this._context - .Set() - .FindAsync(id); - } - - public virtual async Task EditAsync(Guid id, TEntity newEntity) - { - var entry = this._context.Entry(newEntity); - if (entry.State == EntityState.Detached) - this._context.Attach(newEntity); - - entry.State = EntityState.Modified; - - return await this.SaveChangesAsync(); - } - - public virtual async Task DeleteAsync(TEntity entity) - { - this._context.Remove(entity); - - return await this.SaveChangesAsync(); - } - - public virtual async Task SaveChangesAsync() - { - int result = await _context.SaveChangesAsync(); - - return result >= 1; - } - } -} diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs deleted file mode 100644 index bee7624..0000000 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class CommentRepository : BaseRepository, ICommentRepository - { - private readonly DevHiveContext _context; - - public CommentRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - - #region Read - public override async Task GetByIdAsync(Guid id) - { - return await this._context.Comments - .Include(x => x.Creator) - .Include(x => x.Post) - .FirstOrDefaultAsync(x => x.Id == id); - } - - /// - /// This method returns the comment that is made at exactly the given time and by the given creator - /// - public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) - { - return await this._context.Comments - .FirstOrDefaultAsync(p => p.Creator.Id == issuerId && - p.TimeCreated == timeCreated); - } - - public async Task> GetPostComments(Guid postId) - { - return await this._context.Posts - .SelectMany(x => x.Comments) - .Where(x => x.Post.Id == postId) - .ToListAsync(); - } - #endregion - - #region Update - public override async Task EditAsync(Guid id, Comment newEntity) - { - Comment comment = await this.GetByIdAsync(id); - - this._context - .Entry(comment) - .CurrentValues - .SetValues(newEntity); - - return await this.SaveChangesAsync(); - } - #endregion - - - #region Validations - public async Task DoesCommentExist(Guid id) - { - return await this._context.Comments - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } - #endregion - } -} diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs deleted file mode 100644 index 8d3e5e1..0000000 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using AutoMapper.Internal; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class FeedRepository : IFeedRepository - { - private readonly DevHiveContext _context; - - public FeedRepository(DevHiveContext context) - { - this._context = context; - } - - /// - /// This returns a given amount of posts of all given friends, created before "firstRequestIssued", - /// ordered from latest to oldest (time created). - /// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize). - /// - /// This method is used in the feed page. - /// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts, - /// that are after the first X posts. - /// - public async Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize) - { - List friendsIds = friendsList.Select(f => f.Id).ToList(); - - List posts = await this._context.Posts - .Where(post => post.TimeCreated < firstRequestIssued) - .Where(p => friendsIds.Contains(p.Creator.Id)) - .ToListAsync(); - - // Ordering by descending can't happen in query, because it doesn't order it - // completely correctly (example: in query these two times are ordered - // like this: 2021-01-30T11:49:45, 2021-01-28T21:37:40.701244) - posts = posts - .OrderByDescending(x => x.TimeCreated.ToFileTime()) - .Skip((pageNumber - 1) * pageSize) - .Take(pageSize) - .ToList(); - - return posts; - } - - /// - /// This returns a given amount of posts, that a user has made, created before "firstRequestIssued", - /// ordered from latest to oldest (time created). - /// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize). - /// - /// This method is used in the profile page. - /// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts, - /// that are after the first X posts. - /// - public async Task> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize) - { - List posts = await this._context.Posts - .Where(post => post.TimeCreated < firstRequestIssued) - .Where(p => p.Creator.Id == user.Id) - .ToListAsync(); - - // Look at GetFriendsPosts on why this is done like this - posts = posts - .OrderByDescending(x => x.TimeCreated.ToFileTime()) - .Skip((pageNumber - 1) * pageSize) - .Take(pageSize) - .ToList(); - - return posts; - } - } -} diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs deleted file mode 100644 index 31d0b86..0000000 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class LanguageRepository : BaseRepository, ILanguageRepository - { - private readonly DevHiveContext _context; - - public LanguageRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - - #region Read - public async Task GetByNameAsync(string languageName) - { - return await this._context.Languages - .FirstOrDefaultAsync(x => x.Name == languageName); - } - - /// - /// Returns all technologies that exist in the database - /// - public HashSet GetLanguages() - { - return this._context.Languages.ToHashSet(); - } - #endregion - - #region Validations - public async Task DoesLanguageNameExistAsync(string languageName) - { - return await this._context.Languages - .AsNoTracking() - .AnyAsync(r => r.Name == languageName); - } - - public async Task DoesLanguageExistAsync(Guid id) - { - return await this._context.Languages - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } - #endregion - } -} diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs deleted file mode 100644 index 52c5b4e..0000000 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using DevHive.Data.RelationModels; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class PostRepository : BaseRepository, IPostRepository - { - private readonly DevHiveContext _context; - private readonly IUserRepository _userRepository; - - public PostRepository(DevHiveContext context, IUserRepository userRepository) - : base(context) - { - this._context = context; - this._userRepository = userRepository; - } - - public async Task AddNewPostToCreator(Guid userId, Post post) - { - User user = await this._userRepository.GetByIdAsync(userId); - user.Posts.Add(post); - - return await this.SaveChangesAsync(); - } - - #region Read - public override async Task GetByIdAsync(Guid id) - { - return await this._context.Posts - .Include(x => x.Comments) - .Include(x => x.Creator) - .Include(x => x.Attachments) - // .Include(x => x.Rating) - .FirstOrDefaultAsync(x => x.Id == id); - } - - /// - /// This method returns the post that is made at exactly the given time and by the given creator - /// - public async Task GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) - { - return await this._context.Posts - .FirstOrDefaultAsync(p => p.Creator.Id == creatorId && - p.TimeCreated == timeCreated); - } - - public async Task> GetFileUrls(Guid postId) - { - return (await this.GetByIdAsync(postId)).Attachments.Select(x => x.FileUrl).ToList(); - } - #endregion - - #region Update - public override async Task EditAsync(Guid id, Post newEntity) - { - Post post = await this.GetByIdAsync(id); - // var ratingId = post.Rating.Id; - - this._context - .Entry(post) - .CurrentValues - .SetValues(newEntity); - - List postAttachments = new(); - foreach(var attachment in newEntity.Attachments) - postAttachments.Add(attachment); - post.Attachments = postAttachments; - - post.Comments.Clear(); - foreach(var comment in newEntity.Comments) - post.Comments.Add(comment); - - // post.Rating.Id = ratingId; - - this._context.Entry(post).State = EntityState.Modified; - - return await this.SaveChangesAsync(); - } - #endregion - - #region Validations - public async Task DoesPostExist(Guid postId) - { - return await this._context.Posts - .AsNoTracking() - .AnyAsync(r => r.Id == postId); - } - - public async Task DoesPostHaveFiles(Guid postId) - { - return await this._context.Posts - .AsNoTracking() - .Where(x => x.Id == postId) - .Select(x => x.Attachments) - .AnyAsync(); - } - #endregion - } -} diff --git a/src/DevHive.Data/Repositories/RatingRepository.cs b/src/DevHive.Data/Repositories/RatingRepository.cs deleted file mode 100644 index 1be8fe8..0000000 --- a/src/DevHive.Data/Repositories/RatingRepository.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class RatingRepository : BaseRepository, IRatingRepository - { - private readonly DevHiveContext _context; - private readonly IPostRepository _postRepository; - - public RatingRepository(DevHiveContext context, IPostRepository postRepository) - : base(context) - { - this._context = context; - this._postRepository = postRepository; - } - - public async Task GetRatingByPostId(Guid postId) - { - return await this._context.Rating - .FirstOrDefaultAsync(x => x.Post.Id == postId); - } - - public async Task UserRatedPost(Guid userId, Guid postId) - { - return await this._context.UserRate - .Where(x => x.Post.Id == postId) - .AnyAsync(x => x.User.Id == userId); - } - } -} diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs deleted file mode 100644 index 441efef..0000000 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class RoleRepository : BaseRepository, IRoleRepository - { - private readonly DevHiveContext _context; - - public RoleRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - - #region Read - public async Task GetByNameAsync(string name) - { - return await this._context.Roles - .FirstOrDefaultAsync(x => x.Name == name); - } - #endregion - - public override async Task EditAsync(Guid id, Role newEntity) - { - Role role = await this.GetByIdAsync(id); - - this._context - .Entry(role) - .CurrentValues - .SetValues(newEntity); - - return await this.SaveChangesAsync(); - } - - #region Validations - public async Task DoesNameExist(string name) - { - return await this._context.Roles - .AsNoTracking() - .AnyAsync(r => r.Name == name); - } - - public async Task DoesRoleExist(Guid id) - { - return await this._context.Roles - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } - #endregion - } -} diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs deleted file mode 100644 index 6f0d10f..0000000 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class TechnologyRepository : BaseRepository, ITechnologyRepository - { - private readonly DevHiveContext _context; - - public TechnologyRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - - #region Read - public async Task GetByNameAsync(string technologyName) - { - return await this._context.Technologies - .FirstOrDefaultAsync(x => x.Name == technologyName); - } - - /// - /// Returns all technologies that exist in the database - /// - public HashSet GetTechnologies() - { - return this._context.Technologies.ToHashSet(); - } - #endregion - - #region Validations - public async Task DoesTechnologyNameExistAsync(string technologyName) - { - return await this._context.Technologies - .AsNoTracking() - .AnyAsync(r => r.Name == technologyName); - } - - public async Task DoesTechnologyExistAsync(Guid id) - { - return await this._context.Technologies - .AsNoTracking() - .AnyAsync(x => x.Id == id); - } - #endregion - } -} diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs deleted file mode 100644 index 6e97e60..0000000 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ /dev/null @@ -1,108 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using AutoMapper.Mappers; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using DevHive.Data.RelationModels; -using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class UserRepository : BaseRepository, IUserRepository - { - private readonly DevHiveContext _context; - - public UserRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - - #region Read - public override async Task GetByIdAsync(Guid id) - { - return await this._context.Users - .Include(x => x.Roles) - .Include(x => x.Languages) - .Include(x => x.Technologies) - .Include(x => x.Posts) - .Include(x => x.Friends) - .Include(x => x.ProfilePicture) - .FirstOrDefaultAsync(x => x.Id == id); - } - - public async Task GetByUsernameAsync(string username) - { - return await this._context.Users - .Include(x => x.Roles) - .Include(x => x.Languages) - .Include(x => x.Technologies) - .Include(x => x.Posts) - .Include(x => x.Friends) - .Include(x => x.ProfilePicture) - .FirstOrDefaultAsync(x => x.UserName == username); - } - #endregion - - #region Update - public async Task UpdateProfilePicture(Guid userId, string pictureUrl) - { - User user = await this.GetByIdAsync(userId); - - user.ProfilePicture.PictureURL = pictureUrl; - - return await this.SaveChangesAsync(); - } - #endregion - - #region Validations - public async Task DoesUserExistAsync(Guid id) - { - return await this._context.Users - .AsNoTracking() - .AnyAsync(x => x.Id == id); - } - - public async Task DoesUsernameExistAsync(string username) - { - return await this._context.Users - .AsNoTracking() - .AnyAsync(u => u.UserName == username); - } - - public async Task DoesEmailExistAsync(string email) - { - return await this._context.Users - .AsNoTracking() - .AnyAsync(u => u.Email == email); - } - - public async Task ValidateFriendsCollectionAsync(List usernames) - { - bool valid = true; - - foreach (var username in usernames) - { - if (!await this._context.Users.AnyAsync(x => x.UserName == username)) - { - valid = false; - break; - } - } - return valid; - } - - public bool DoesUserHaveThisUsername(Guid id, string username) - { - return this._context.Users - .AsNoTracking() - .Any(x => x.Id == id && - x.UserName == username); - } - #endregion - } -} -- cgit v1.2.3