aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data/Repositories
diff options
context:
space:
mode:
authorKamen Mladenov <kamen.d.mladenov@protonmail.com>2021-04-09 19:51:35 +0300
committerGitHub <noreply@github.com>2021-04-09 19:51:35 +0300
commit233f38915ba0079079233eff55434ef349c05c45 (patch)
tree6c5f69017865bcab87355e910c87339453da1406 /src/DevHive.Data/Repositories
parentf4a70c6430db923af9fa9958a11c2d6612cb52cc (diff)
parenta992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff)
downloadDevHive-233f38915ba0079079233eff55434ef349c05c45.tar
DevHive-233f38915ba0079079233eff55434ef349c05c45.tar.gz
DevHive-233f38915ba0079079233eff55434ef349c05c45.zip
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/DevHive.Data/Repositories')
-rw-r--r--src/DevHive.Data/Repositories/BaseRepository.cs59
-rw-r--r--src/DevHive.Data/Repositories/CommentRepository.cs74
-rw-r--r--src/DevHive.Data/Repositories/FeedRepository.cs77
-rw-r--r--src/DevHive.Data/Repositories/LanguageRepository.cs53
-rw-r--r--src/DevHive.Data/Repositories/PostRepository.cs105
-rw-r--r--src/DevHive.Data/Repositories/RatingRepository.cs35
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs55
-rw-r--r--src/DevHive.Data/Repositories/TechnologyRepository.cs53
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs108
9 files changed, 0 insertions, 619 deletions
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<TEntity> : IRepository<TEntity>
- where TEntity : class
- {
- private readonly DbContext _context;
-
- public BaseRepository(DbContext context)
- {
- this._context = context;
- }
-
- public virtual async Task<bool> AddAsync(TEntity entity)
- {
- await this._context
- .Set<TEntity>()
- .AddAsync(entity);
-
- return await this.SaveChangesAsync();
- }
-
- public virtual async Task<TEntity> GetByIdAsync(Guid id)
- {
- return await this._context
- .Set<TEntity>()
- .FindAsync(id);
- }
-
- public virtual async Task<bool> 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<bool> DeleteAsync(TEntity entity)
- {
- this._context.Remove(entity);
-
- return await this.SaveChangesAsync();
- }
-
- public virtual async Task<bool> 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<Comment>, ICommentRepository
- {
- private readonly DevHiveContext _context;
-
- public CommentRepository(DevHiveContext context)
- : base(context)
- {
- this._context = context;
- }
-
- #region Read
- public override async Task<Comment> GetByIdAsync(Guid id)
- {
- return await this._context.Comments
- .Include(x => x.Creator)
- .Include(x => x.Post)
- .FirstOrDefaultAsync(x => x.Id == id);
- }
-
- /// <summary>
- /// This method returns the comment that is made at exactly the given time and by the given creator
- /// </summary>
- public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated)
- {
- return await this._context.Comments
- .FirstOrDefaultAsync(p => p.Creator.Id == issuerId &&
- p.TimeCreated == timeCreated);
- }
-
- public async Task<List<Comment>> 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<bool> 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<bool> 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;
- }
-
- /// <summary>
- /// 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.
- /// </summary>
- public async Task<List<Post>> GetFriendsPosts(List<User> friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize)
- {
- List<Guid> friendsIds = friendsList.Select(f => f.Id).ToList();
-
- List<Post> 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;
- }
-
- /// <summary>
- /// 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.
- /// </summary>
- public async Task<List<Post>> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize)
- {
- List<Post> 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<Language>, ILanguageRepository
- {
- private readonly DevHiveContext _context;
-
- public LanguageRepository(DevHiveContext context)
- : base(context)
- {
- this._context = context;
- }
-
- #region Read
- public async Task<Language> GetByNameAsync(string languageName)
- {
- return await this._context.Languages
- .FirstOrDefaultAsync(x => x.Name == languageName);
- }
-
- /// <summary>
- /// Returns all technologies that exist in the database
- /// </summary>
- public HashSet<Language> GetLanguages()
- {
- return this._context.Languages.ToHashSet();
- }
- #endregion
-
- #region Validations
- public async Task<bool> DoesLanguageNameExistAsync(string languageName)
- {
- return await this._context.Languages
- .AsNoTracking()
- .AnyAsync(r => r.Name == languageName);
- }
-
- public async Task<bool> 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<Post>, 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<bool> 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<Post> 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);
- }
-
- /// <summary>
- /// This method returns the post that is made at exactly the given time and by the given creator
- /// </summary>
- public async Task<Post> GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated)
- {
- return await this._context.Posts
- .FirstOrDefaultAsync(p => p.Creator.Id == creatorId &&
- p.TimeCreated == timeCreated);
- }
-
- public async Task<List<string>> GetFileUrls(Guid postId)
- {
- return (await this.GetByIdAsync(postId)).Attachments.Select(x => x.FileUrl).ToList();
- }
- #endregion
-
- #region Update
- public override async Task<bool> 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> 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<bool> DoesPostExist(Guid postId)
- {
- return await this._context.Posts
- .AsNoTracking()
- .AnyAsync(r => r.Id == postId);
- }
-
- public async Task<bool> 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<Rating>, 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<Rating> GetRatingByPostId(Guid postId)
- {
- return await this._context.Rating
- .FirstOrDefaultAsync(x => x.Post.Id == postId);
- }
-
- public async Task<bool> 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<Role>, IRoleRepository
- {
- private readonly DevHiveContext _context;
-
- public RoleRepository(DevHiveContext context)
- : base(context)
- {
- this._context = context;
- }
-
- #region Read
- public async Task<Role> GetByNameAsync(string name)
- {
- return await this._context.Roles
- .FirstOrDefaultAsync(x => x.Name == name);
- }
- #endregion
-
- public override async Task<bool> 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<bool> DoesNameExist(string name)
- {
- return await this._context.Roles
- .AsNoTracking()
- .AnyAsync(r => r.Name == name);
- }
-
- public async Task<bool> 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<Technology>, ITechnologyRepository
- {
- private readonly DevHiveContext _context;
-
- public TechnologyRepository(DevHiveContext context)
- : base(context)
- {
- this._context = context;
- }
-
- #region Read
- public async Task<Technology> GetByNameAsync(string technologyName)
- {
- return await this._context.Technologies
- .FirstOrDefaultAsync(x => x.Name == technologyName);
- }
-
- /// <summary>
- /// Returns all technologies that exist in the database
- /// </summary>
- public HashSet<Technology> GetTechnologies()
- {
- return this._context.Technologies.ToHashSet();
- }
- #endregion
-
- #region Validations
- public async Task<bool> DoesTechnologyNameExistAsync(string technologyName)
- {
- return await this._context.Technologies
- .AsNoTracking()
- .AnyAsync(r => r.Name == technologyName);
- }
-
- public async Task<bool> 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<User>, IUserRepository
- {
- private readonly DevHiveContext _context;
-
- public UserRepository(DevHiveContext context)
- : base(context)
- {
- this._context = context;
- }
-
- #region Read
- public override async Task<User> 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<User> 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<bool> 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<bool> DoesUserExistAsync(Guid id)
- {
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(x => x.Id == id);
- }
-
- public async Task<bool> DoesUsernameExistAsync(string username)
- {
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(u => u.UserName == username);
- }
-
- public async Task<bool> DoesEmailExistAsync(string email)
- {
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(u => u.Email == email);
- }
-
- public async Task<bool> ValidateFriendsCollectionAsync(List<string> 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
- }
-}