aboutsummaryrefslogtreecommitdiff
path: root/src/Data
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/Data
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/Data')
-rw-r--r--src/Data/DevHive.Data.Models/Comment.cs17
-rw-r--r--src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj10
-rw-r--r--src/Data/DevHive.Data.Models/Language.cs14
-rw-r--r--src/Data/DevHive.Data.Models/Post.cs25
-rw-r--r--src/Data/DevHive.Data.Models/ProfilePicture.cs18
-rw-r--r--src/Data/DevHive.Data.Models/Rating.cs16
-rw-r--r--src/Data/DevHive.Data.Models/RelationalModels/PostAttachments.cs16
-rw-r--r--src/Data/DevHive.Data.Models/Role.cs16
-rw-r--r--src/Data/DevHive.Data.Models/Technology.cs14
-rw-r--r--src/Data/DevHive.Data.Models/User.cs29
-rw-r--r--src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs168
-rw-r--r--src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj21
-rw-r--r--src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs140
-rw-r--r--src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs133
-rw-r--r--src/Data/DevHive.Data.Tests/PostRepository.Tests.cs142
-rw-r--r--src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs188
-rw-r--r--src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs116
-rw-r--r--src/Data/DevHive.Data/ConnectionString.json5
-rw-r--r--src/Data/DevHive.Data/DevHive.Data.csproj24
-rw-r--r--src/Data/DevHive.Data/DevHiveContext.cs103
-rw-r--r--src/Data/DevHive.Data/DevHiveContextFactory.cs23
-rw-r--r--src/Data/DevHive.Data/Interfaces/ICommentRepository.cs15
-rw-r--r--src/Data/DevHive.Data/Interfaces/IFeedRepository.cs13
-rw-r--r--src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs16
-rw-r--r--src/Data/DevHive.Data/Interfaces/IPostRepository.cs18
-rw-r--r--src/Data/DevHive.Data/Interfaces/IProfilePictureRepository.cs11
-rw-r--r--src/Data/DevHive.Data/Interfaces/IRatingRepository.cs15
-rw-r--r--src/Data/DevHive.Data/Interfaces/IRepository.cs21
-rw-r--r--src/Data/DevHive.Data/Interfaces/IRoleRepository.cs14
-rw-r--r--src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs16
-rw-r--r--src/Data/DevHive.Data/Interfaces/IUserRepository.cs23
-rw-r--r--src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs600
-rw-r--r--src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs526
-rw-r--r--src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs598
-rw-r--r--src/Data/DevHive.Data/Repositories/BaseRepository.cs59
-rw-r--r--src/Data/DevHive.Data/Repositories/CommentRepository.cs73
-rw-r--r--src/Data/DevHive.Data/Repositories/FeedRepository.cs77
-rw-r--r--src/Data/DevHive.Data/Repositories/LanguageRepository.cs53
-rw-r--r--src/Data/DevHive.Data/Repositories/PostRepository.cs102
-rw-r--r--src/Data/DevHive.Data/Repositories/ProfilePictureRepository.cs25
-rw-r--r--src/Data/DevHive.Data/Repositories/RatingRepository.cs79
-rw-r--r--src/Data/DevHive.Data/Repositories/RoleRepository.cs56
-rw-r--r--src/Data/DevHive.Data/Repositories/TechnologyRepository.cs53
-rw-r--r--src/Data/DevHive.Data/Repositories/UserRepository.cs152
44 files changed, 3853 insertions, 0 deletions
diff --git a/src/Data/DevHive.Data.Models/Comment.cs b/src/Data/DevHive.Data.Models/Comment.cs
new file mode 100644
index 0000000..8a58edd
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Comment.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace DevHive.Data.Models
+{
+ public class Comment
+ {
+ public Guid Id { get; set; }
+
+ public Post Post { get; set; }
+
+ public User Creator { get; set; }
+
+ public string Message { get; set; }
+
+ public DateTime TimeCreated { get; set; }
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj b/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj
new file mode 100644
index 0000000..2958f86
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ </PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0"/>
+ <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4"/>
+ <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/>
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/src/Data/DevHive.Data.Models/Language.cs b/src/Data/DevHive.Data.Models/Language.cs
new file mode 100644
index 0000000..b9e538a
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Language.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+
+namespace DevHive.Data.Models
+{
+ public class Language
+ {
+ public Guid Id { get; set; }
+
+ public string Name { get; set; }
+
+ public HashSet<User> Users { get; set; } = new();
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/Post.cs b/src/Data/DevHive.Data.Models/Post.cs
new file mode 100644
index 0000000..a5e7642
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Post.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations.Schema;
+using DevHive.Data.Models.Relational;
+
+namespace DevHive.Data.Models
+{
+ [Table("Posts")]
+ public class Post
+ {
+ public Guid Id { get; set; }
+
+ public User Creator { get; set; }
+
+ public string Message { get; set; }
+
+ public DateTime TimeCreated { get; set; }
+
+ public List<Comment> Comments { get; set; } = new();
+
+ public List<Rating> Ratings { get; set; }
+
+ public List<PostAttachments> Attachments { get; set; } = new();
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/ProfilePicture.cs b/src/Data/DevHive.Data.Models/ProfilePicture.cs
new file mode 100644
index 0000000..e8166d7
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/ProfilePicture.cs
@@ -0,0 +1,18 @@
+using System;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace DevHive.Data.Models
+{
+ [Table("ProfilePictures")]
+ public class ProfilePicture
+ {
+ public const string DefaultURL = "/assets/icons/tabler-icon-user.svg";
+
+ public Guid Id { get; set; }
+
+ public Guid UserId { get; set; }
+ public User User { get; set; }
+
+ public string PictureURL { get; set; }
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/Rating.cs b/src/Data/DevHive.Data.Models/Rating.cs
new file mode 100644
index 0000000..c950697
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Rating.cs
@@ -0,0 +1,16 @@
+using System;
+
+namespace DevHive.Data.Models
+{
+ public class Rating
+ {
+ //if adding rating to comments change Post for intreface IRatable!
+ public Guid Id { get; set; }
+
+ public User User { get; set; }
+
+ public Post Post { get; set; }
+
+ public bool IsLike { get; set; }
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/RelationalModels/PostAttachments.cs b/src/Data/DevHive.Data.Models/RelationalModels/PostAttachments.cs
new file mode 100644
index 0000000..8c814fc
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/RelationalModels/PostAttachments.cs
@@ -0,0 +1,16 @@
+using System;
+using System.ComponentModel.DataAnnotations.Schema;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Models.Relational
+{
+ [Table("PostAttachments")]
+ public class PostAttachments
+ {
+ public Guid Id { get; set; }
+
+ public Post Post { get; set; }
+
+ public string FileUrl { get; set; }
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/Role.cs b/src/Data/DevHive.Data.Models/Role.cs
new file mode 100644
index 0000000..5456cb0
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Role.cs
@@ -0,0 +1,16 @@
+using System.ComponentModel.DataAnnotations.Schema;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Identity;
+using System;
+
+namespace DevHive.Data.Models
+{
+ [Table("Roles")]
+ public class Role : IdentityRole<Guid>
+ {
+ public const string DefaultRole = "User";
+ public const string AdminRole = "Admin";
+
+ public HashSet<User> Users { get; set; } = new();
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/Technology.cs b/src/Data/DevHive.Data.Models/Technology.cs
new file mode 100644
index 0000000..0dc077f
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/Technology.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+
+namespace DevHive.Data.Models
+{
+ public class Technology
+ {
+ public Guid Id { get; set; }
+
+ public string Name { get; set; }
+
+ public HashSet<User> Users { get; set; } = new();
+ }
+}
diff --git a/src/Data/DevHive.Data.Models/User.cs b/src/Data/DevHive.Data.Models/User.cs
new file mode 100644
index 0000000..d3789ec
--- /dev/null
+++ b/src/Data/DevHive.Data.Models/User.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations.Schema;
+using Microsoft.AspNetCore.Identity;
+
+namespace DevHive.Data.Models
+{
+ [Table("Users")]
+ public class User : IdentityUser<Guid>
+ {
+ public string FirstName { get; set; }
+
+ public string LastName { get; set; }
+
+ public ProfilePicture ProfilePicture { get; set; } = new() { PictureURL = ProfilePicture.DefaultURL };
+
+ public HashSet<Language> Languages { get; set; } = new();
+
+ public HashSet<Technology> Technologies { get; set; } = new();
+
+ public HashSet<Role> Roles { get; set; } = new();
+
+ public HashSet<Post> Posts { get; set; } = new();
+
+ public HashSet<User> Friends { get; set; } = new();
+
+ public HashSet<Comment> Comments { get; set; } = new();
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs
new file mode 100644
index 0000000..004d418
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs
@@ -0,0 +1,168 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class CommentRepositoryTests
+ {
+ private const string COMMENT_MESSAGE = "Comment message";
+ private DevHiveContext _context;
+ private CommentRepository _commentRepository;
+
+ #region SetUp
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._commentRepository = new CommentRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByIdAsync
+ [Test]
+ public async Task GetByIdAsync_ReturnsTheCorrectComment_IfItExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ Comment resultComment = await this._commentRepository.GetByIdAsync(comment.Id);
+
+ Assert.AreEqual(comment.Id, resultComment.Id, "GetByIdAsync does not return the correct comment when it exists.");
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfCommentDoesNotExist()
+ {
+ Comment resultComment = await this._commentRepository.GetByIdAsync(Guid.Empty);
+
+ Assert.IsNull(resultComment, "GetByIdAsync does not return null when the comment does not exist");
+ }
+ #endregion
+
+ #region GetPostComments
+ [Test]
+ public async Task GetPostComments_ReturnsAllCommentsForPost_IfAnyExist()
+ {
+ List<Comment> comments = new List<Comment>
+ {
+ new Comment(),
+ new Comment(),
+ new Comment()
+ };
+ Post post = new Post
+ {
+ Id = Guid.NewGuid(),
+ Comments = comments
+ };
+
+ this._context.Posts.Add(post);
+ await this._context.SaveChangesAsync();
+
+ List<Comment> resultComments = await this._commentRepository.GetPostComments(post.Id);
+
+ Assert.AreEqual(comments.Count, resultComments.Count, "GetPostComments does not return the comments for a given post correctly");
+ }
+
+ [Test]
+ public async Task GetPostComments_ReturnsEmptyList_WhenPostDoesNotExist()
+ {
+ List<Comment> resultComments = await this._commentRepository.GetPostComments(Guid.Empty);
+
+ Assert.IsEmpty(resultComments, "GetPostComments does not return empty string when post does not exist");
+ }
+ #endregion
+
+ #region GetCommentByIssuerAndTimeCreatedAsync
+ [Test]
+ public async Task GetCommentByCreatorAndTimeCreatedAsync_ReturnsTheCorrectComment_IfItExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated);
+
+ Assert.AreEqual(comment.Id, resultComment.Id, "GetCommentByIssuerAndTimeCreatedAsync does not return the corect comment when it exists");
+ }
+
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist()
+ {
+ Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now);
+
+ Assert.IsNull(resultComment, "GetCommentByIssuerAndTimeCreatedAsync does not return null when the comment does not exist");
+ }
+ #endregion
+
+ #region EditAsync
+ [Test]
+ public async Task EditAsync_ReturnsTrue_WhenCommentIsUpdatedSuccessfully()
+ {
+ string newMessage = "New message!";
+ Comment comment = await this.AddEntity();
+ Comment updatedComment = new Comment
+ {
+ Id = comment.Id,
+ Message = newMessage
+ };
+
+ bool result = await this._commentRepository.EditAsync(comment.Id, updatedComment);
+
+ Assert.IsTrue(result, "EditAsync does not return true when comment is updated successfully");
+ }
+ #endregion
+
+ #region DoesCommentExist
+ [Test]
+ public async Task DoesCommentExist_ReturnsTrue_WhenTheCommentExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ bool result = await this._commentRepository.DoesCommentExist(comment.Id);
+
+ Assert.IsTrue(result, "DoesCommentExist does not return true whenm the Comment exists");
+ }
+
+ [Test]
+ public async Task DoesCommentExist_ReturnsFalse_WhenTheCommentDoesNotExist()
+ {
+ bool result = await this._commentRepository.DoesCommentExist(Guid.Empty);
+
+ Assert.IsFalse(result, "DoesCommentExist does not return false whenm the Comment" +
+ " does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task<Comment> AddEntity()
+ {
+ User creator = new() { Id = Guid.NewGuid() };
+ Comment comment = new()
+ {
+ Id = Guid.NewGuid(),
+ Message = COMMENT_MESSAGE,
+ Creator = creator,
+ TimeCreated = DateTime.Now
+ };
+
+ this._context.Comments.Add(comment);
+ await this._context.SaveChangesAsync();
+
+ return comment;
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj
new file mode 100644
index 0000000..e9b33e5
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj
@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4"/>
+ <PackageReference Include="Moq" Version="4.16.1"/>
+ <PackageReference Include="NUnit" Version="3.13.1"/>
+ <PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/>
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1"/>
+ <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj"/>
+ </ItemGroup>
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project> \ No newline at end of file
diff --git a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs
new file mode 100644
index 0000000..5d66bfc
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs
@@ -0,0 +1,140 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class FeedRepositoryTests
+ {
+ private const int PAGE_NUMBER = 1;
+ private const int PAGE_SIZE = 10;
+
+ private DevHiveContext _context;
+ private FeedRepository _feedRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._feedRepository = new FeedRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetFriendsPosts
+ [Test]
+ public async Task GetFriendsPosts_ReturnsListOfPosts_WhenTheyExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ dummyUser.Posts = await this.CreateDummyPosts(dummyUser);
+ List<User> friendsList = new()
+ {
+ dummyUser
+ };
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.GreaterOrEqual(resultList.Count, dummyUser.Posts.Count, "GetFriendsPosts does not return the posts corrtrectly");
+ }
+
+ [Test]
+ public async Task GetFriendsPosts_ReturnsEmptyList_WhenNoSuitablePostsExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ List<User> friendsList = new()
+ {
+ dummyUser
+ };
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.LessOrEqual(resultList.Count, 0, "GetFriendsPosts does not return all correct posts");
+ }
+ #endregion
+
+ #region GetUsersPosts
+ [Test]
+ public async Task GetUsersPosts_ReturnsAllPostsOfTheUser_IfAnyExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ HashSet<Post> posts = await this.CreateDummyPosts(dummyUser);
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetUsersPosts(dummyUser, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.GreaterOrEqual(resultList.Count, posts.Count, "GetUsersPosts does not return the posts corrtrectly");
+ }
+
+ [Test]
+ public async Task GetUsersPosts_ReturnsEmptyList_WhenNoSuitablePostsExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetUsersPosts(dummyUser, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.LessOrEqual(resultList.Count, 0, "GetUsersPosts does not return empty list when no suitable posts exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private User CreateDummyUser()
+ {
+ HashSet<Role> roles = new()
+ {
+ new Role()
+ {
+ Id = Guid.NewGuid(),
+ Name = Role.DefaultRole
+ },
+ };
+
+ return new()
+ {
+ Id = Guid.NewGuid(),
+ UserName = "pioneer10",
+ FirstName = "Spas",
+ LastName = "Spasov",
+ Email = "abv@abv.bg",
+ Roles = roles
+ };
+ }
+
+ private async Task<HashSet<Post>> CreateDummyPosts(User user)
+ {
+ HashSet<Post> posts = new HashSet<Post>
+ {
+ new Post { Creator = user, TimeCreated = DateTime.Now },
+ new Post{ Creator = user, TimeCreated = DateTime.Now },
+ new Post{ Creator = user, TimeCreated = DateTime.Now }
+ };
+
+ await this._context.Posts.AddRangeAsync(posts);
+ await this._context.SaveChangesAsync();
+
+ return posts;
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs
new file mode 100644
index 0000000..c7d4dc7
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class LenguageRepositoryTests
+ {
+ private const string LANGUAGE_NAME = "Language test name";
+ private DevHiveContext _context;
+ private LanguageRepository _languageRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._languageRepository = new LanguageRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectLanguage_IfItExists()
+ {
+ await this.AddEntity();
+
+ Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault();
+
+ Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME);
+
+ Assert.AreEqual(language.Id, languageResult.Id);
+ }
+
+ [Test]
+ public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME);
+
+ Assert.IsNull(languageResult);
+ }
+ #endregion
+
+ #region GetLanguages
+ [Test]
+ public async Task GetLanguages_ReturnsAllLanguages()
+ {
+ await this.AddEntity();
+ await this.AddEntity("secondLanguage");
+ await this.AddEntity("thirdLanguage");
+
+ HashSet<Language> languages = this._languageRepository.GetLanguages();
+
+ Assert.GreaterOrEqual(languages.Count, 3, "GetLanguages does not get all Languages");
+ }
+ #endregion
+
+ #region DoesLanguageExistAsync
+ [Test]
+ public async Task DoesLanguageExist_ReturnsTrue_IfIdExists()
+ {
+ await this.AddEntity();
+ Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault();
+
+ Guid id = language.Id;
+
+ bool result = await this._languageRepository.DoesLanguageExistAsync(id);
+
+ Assert.IsTrue(result, "DoesLanguageExistAsync returns flase when language exists");
+ }
+
+ [Test]
+ public async Task DoesLanguageExist_ReturnsFalse_IfIdDoesNotExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this._languageRepository.DoesLanguageExistAsync(id);
+
+ Assert.IsFalse(result, "DoesLanguageExistAsync returns true when language does not exist");
+ }
+ #endregion
+
+ #region DoesTechnologyNameExistAsync
+ [Test]
+ public async Task DoesLanguageNameExist_ReturnsTrue_IfLanguageExists()
+ {
+ await this.AddEntity();
+
+ bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME);
+
+ Assert.IsTrue(result, "DoesLanguageNameExists returns true when language name does not exist");
+ }
+
+ [Test]
+ public async Task DoesLanguageNameExist_ReturnsFalse_IfLanguageDoesNotExists()
+ {
+ bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME);
+
+ Assert.False(result, "DoesTechnologyNameExistAsync returns true when language name does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddEntity(string name = LANGUAGE_NAME)
+ {
+ Language language = new()
+ {
+ Id = Guid.NewGuid(),
+ Name = name
+ };
+
+ await this._context.Languages.AddAsync(language);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs
new file mode 100644
index 0000000..005769f
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs
@@ -0,0 +1,142 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Data.Models.Relational;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class PostRepositoryTests
+ {
+ private const string POST_MESSAGE = "Post test message";
+ private DevHiveContext _context;
+ private Mock<IUserRepository> _userRepository;
+ private PostRepository _postRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._userRepository = new Mock<IUserRepository>();
+
+ this._postRepository = new PostRepository(this._context, this._userRepository.Object);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region AddNewPostToCreator
+ // [Test]
+ // public async Task AddNewPostToCreator_ReturnsTrue_WhenNewPostIsAddedToCreator()
+ // {
+ // Post post = await this.AddEntity();
+ // User user = new User { Id = Guid.NewGuid() };
+ //
+ // this.UserRepository.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
+ //
+ // bool result = await this.PostRepository.AddNewPostToCreator(user.Id, post);
+ //
+ // Assert.IsTrue(result, "AddNewPostToCreator does not return true when Post Is Added To Creator successfully");
+ // }
+ #endregion
+
+ #region GetByIdAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists()
+ {
+ Post post = await this.AddEntity();
+
+ Post resultTechnology = await this._postRepository.GetByIdAsync(post.Id);
+
+ Assert.AreEqual(post.Id, resultTechnology.Id, "GetByIdAsync does not return the correct post");
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Post resultPost = await this._postRepository.GetByIdAsync(Guid.NewGuid());
+
+ Assert.IsNull(resultPost);
+ }
+ #endregion
+
+ #region GetPostByCreatorAndTimeCreatedAsync
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsTheCorrectPost_IfItExists()
+ {
+ Post post = await this.AddEntity();
+
+ Post resultPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated);
+
+ Assert.AreEqual(post.Id, resultPost.Id, "GetPostByCreatorAndTimeCreatedAsync does not return the corect post when it exists");
+ }
+
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist()
+ {
+ await this.AddEntity();
+
+ Post resutPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now);
+
+ Assert.IsNull(resutPost, "GetPostByCreatorAndTimeCreatedAsync does not return null when the post does not exist");
+ }
+ #endregion
+
+ #region DoesPostExist
+ [Test]
+ public async Task DoesPostExist_ReturnsTrue_WhenThePostExists()
+ {
+ Post post = await this.AddEntity();
+
+ bool result = await this._postRepository.DoesPostExist(post.Id);
+
+ Assert.IsTrue(result, "DoesPostExist does not return true whenm the Post exists");
+ }
+
+ [Test]
+ public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist()
+ {
+ bool result = await this._postRepository.DoesPostExist(Guid.Empty);
+
+ Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task<Post> AddEntity()
+ {
+ User creator = new() { Id = Guid.NewGuid() };
+ await this._context.Users.AddAsync(creator);
+ Post post = new()
+ {
+ Message = POST_MESSAGE,
+ Id = Guid.NewGuid(),
+ Creator = creator,
+ TimeCreated = DateTime.Now,
+ Attachments = new List<PostAttachments> { new PostAttachments { FileUrl = "kur" }, new PostAttachments { FileUrl = "za" }, new PostAttachments { FileUrl = "tva" } },
+ Comments = new List<Comment>()
+ };
+
+ await this._context.Posts.AddAsync(post);
+ await this._context.SaveChangesAsync();
+
+ return post;
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
new file mode 100644
index 0000000..b5299b0
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
@@ -0,0 +1,188 @@
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using System.Linq;
+using System;
+using System.Collections.Generic;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class RatingRepositoryTests
+ {
+ private DevHiveContext _context;
+ private RatingRepository _ratingRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+ this._ratingRepository = new RatingRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetById
+ [Test]
+ public async Task GetByIdAsync_ReturnsTheCorrectRating_IfItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ await AddDummyRating(ratingId);
+
+ Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId);
+
+ Assert.AreEqual(ratingResult.Id, ratingId);
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist()
+ {
+ Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid());
+
+ Assert.IsNull(ratingResult);
+ }
+ #endregion
+
+ #region GetByPostId
+ [Test]
+ public async Task GetRatingsByPostId_ReturnsFilledListOfRatings_WhenTheyExist()
+ {
+ Guid postId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyRating(Guid.NewGuid(), postId);
+ await AddDummyRating(Guid.NewGuid(), postId);
+
+ List<Rating> result = await this._ratingRepository.GetRatingsByPostId(postId);
+
+ Assert.IsNotEmpty(result);
+ }
+
+ [Test]
+ public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings()
+ {
+ List<Rating> result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid());
+
+ Assert.IsEmpty(result);
+ }
+ #endregion
+
+ #region GetByUserAndPostId
+ [Test]
+ public async Task GetRatingByUserAndPostId_ReturnsRating_WhenItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyUser(userId);
+ await AddDummyRating(ratingId, postId, userId);
+
+ Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId);
+
+ Assert.AreEqual(result.Id, ratingId);
+ }
+
+ [Test]
+ public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist()
+ {
+ Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid());
+
+ Assert.IsNull(result);
+ }
+ #endregion
+
+ #region UserRatedPost
+ [Test]
+ public async Task UserRatedPost_ReturnsTrue_WhenUserHasRatedPost()
+ {
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyUser(userId);
+ await AddDummyRating(Guid.NewGuid(), postId, userId);
+
+ bool result = await this._ratingRepository.UserRatedPost(userId, postId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost()
+ {
+ bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid());
+
+ Assert.IsFalse(result);
+ }
+ #endregion
+
+ #region DoesRatingExist
+ [Test]
+ public async Task DoesRatingExist_ReturnsTrue_WhenItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ await AddDummyRating(ratingId);
+
+ bool result = await this._ratingRepository.DoesRatingExist(ratingId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist()
+ {
+ bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid());
+
+ Assert.IsFalse(result);
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddDummyRating(Guid ratingId, Guid postId = default(Guid), Guid userId = default(Guid))
+ {
+ Rating rating = new Rating
+ {
+ Id = ratingId,
+ Post = this._context.Posts.FirstOrDefault(x => x.Id == postId),
+ User = this._context.Users.FirstOrDefault(x => x.Id == userId)
+ };
+
+ await this._context.Rating.AddAsync(rating);
+ await this._context.SaveChangesAsync();
+ }
+
+ private async Task AddDummyPost(Guid postId)
+ {
+ Post post = new Post()
+ {
+ Id = postId,
+ Message = "Never gonna give you up"
+ };
+
+ await this._context.Posts.AddAsync(post);
+ await this._context.SaveChangesAsync();
+ }
+
+ private async Task AddDummyUser(Guid userId)
+ {
+ User user = new User()
+ {
+ Id = userId
+ };
+
+ await this._context.Users.AddAsync(user);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs
new file mode 100644
index 0000000..d268777
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs
@@ -0,0 +1,116 @@
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class TechnologyRepositoryTests
+ {
+ private const string TECHNOLOGY_NAME = "Technology test name";
+ private DevHiveContext _context;
+ private TechnologyRepository _technologyRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._technologyRepository = new TechnologyRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectTechnology_IfItExists()
+ {
+ await this.AddEntity();
+
+ Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault();
+
+ Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME);
+
+ Assert.AreEqual(technology.Id, resultTechnology.Id);
+ }
+
+ [Test]
+ public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME);
+
+ Assert.IsNull(resultTechnology);
+ }
+ #endregion
+
+ #region DoesTechnologyExistAsync
+ [Test]
+ public async Task DoesTechnologyExist_ReturnsTrue_IfIdExists()
+ {
+ await this.AddEntity();
+ Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault();
+ Guid id = technology.Id;
+
+ bool result = await this._technologyRepository.DoesTechnologyExistAsync(id);
+
+ Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists");
+ }
+
+ [Test]
+ public async Task DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this._technologyRepository.DoesTechnologyExistAsync(id);
+
+ Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist");
+ }
+ #endregion
+
+ #region DoesTechnologyNameExistAsync
+ [Test]
+ public async Task DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists()
+ {
+ await this.AddEntity();
+
+ bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME);
+
+ Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist");
+ }
+
+ [Test]
+ public async Task DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists()
+ {
+ bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME);
+
+ Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddEntity(string name = TECHNOLOGY_NAME)
+ {
+ Technology technology = new()
+ {
+ Name = name
+ };
+
+ this._context.Technologies.Add(technology);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data/ConnectionString.json b/src/Data/DevHive.Data/ConnectionString.json
new file mode 100644
index 0000000..ec065d1
--- /dev/null
+++ b/src/Data/DevHive.Data/ConnectionString.json
@@ -0,0 +1,5 @@
+{
+ "ConnectionStrings": {
+ "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=;"
+ }
+}
diff --git a/src/Data/DevHive.Data/DevHive.Data.csproj b/src/Data/DevHive.Data/DevHive.Data.csproj
new file mode 100644
index 0000000..62320f7
--- /dev/null
+++ b/src/Data/DevHive.Data/DevHive.Data.csproj
@@ -0,0 +1,24 @@
+<Project Sdk="Microsoft.NET.Sdk">
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ </PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="AutoMapper" Version="10.1.1"/>
+ <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1"/>
+ <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4"/>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ <PrivateAssets>all</PrivateAssets>
+ </PackageReference>
+ <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0"/>
+ <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2"/>
+ <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Data.Models\DevHive.Data.Models.csproj"/>
+ </ItemGroup>
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project> \ No newline at end of file
diff --git a/src/Data/DevHive.Data/DevHiveContext.cs b/src/Data/DevHive.Data/DevHiveContext.cs
new file mode 100644
index 0000000..0606864
--- /dev/null
+++ b/src/Data/DevHive.Data/DevHiveContext.cs
@@ -0,0 +1,103 @@
+using System;
+using DevHive.Data.Models;
+using DevHive.Data.Models.Relational;
+using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data
+{
+ public class DevHiveContext : IdentityDbContext<User, Role, Guid>
+ {
+ public DevHiveContext(DbContextOptions<DevHiveContext> options)
+ : base(options) { }
+
+ public DbSet<Technology> Technologies { get; set; }
+ public DbSet<Language> Languages { get; set; }
+ public DbSet<Post> Posts { get; set; }
+ public DbSet<PostAttachments> PostAttachments { get; set; }
+ public DbSet<Comment> Comments { get; set; }
+ public DbSet<Rating> Rating { get; set; }
+ public DbSet<ProfilePicture> ProfilePicture { get; set; }
+
+ protected override void OnModelCreating(ModelBuilder builder)
+ {
+ /* User */
+ builder.Entity<User>()
+ .HasIndex(x => x.UserName)
+ .IsUnique();
+
+ /* Roles */
+ builder.Entity<User>()
+ .HasMany(x => x.Roles)
+ .WithMany(x => x.Users);
+
+ /* Languages */
+ builder.Entity<User>()
+ .HasMany(x => x.Languages)
+ .WithMany(x => x.Users)
+ .UsingEntity(x => x.ToTable("LanguageUser"));
+
+ builder.Entity<Language>()
+ .HasMany(x => x.Users)
+ .WithMany(x => x.Languages)
+ .UsingEntity(x => x.ToTable("LanguageUser"));
+
+ /* Technologies */
+ builder.Entity<User>()
+ .HasMany(x => x.Technologies)
+ .WithMany(x => x.Users)
+ .UsingEntity(x => x.ToTable("TechnologyUser"));
+
+ builder.Entity<Technology>()
+ .HasMany(x => x.Users)
+ .WithMany(x => x.Technologies)
+ .UsingEntity(x => x.ToTable("TechnologyUser"));
+
+ /* Post */
+ builder.Entity<Post>()
+ .HasOne(x => x.Creator)
+ .WithMany(x => x.Posts);
+
+ builder.Entity<Post>()
+ .HasMany(x => x.Comments)
+ .WithOne(x => x.Post);
+
+ /* Post attachments */
+ builder.Entity<PostAttachments>()
+ .HasOne(x => x.Post)
+ .WithMany(x => x.Attachments);
+
+ /* Comment */
+ builder.Entity<Comment>()
+ .HasOne(x => x.Post)
+ .WithMany(x => x.Comments);
+
+ builder.Entity<Comment>()
+ .HasOne(x => x.Creator)
+ .WithMany(x => x.Comments);
+
+ /* Rating */
+ builder.Entity<Rating>()
+ .HasKey(x => x.Id);
+
+ builder.Entity<Rating>()
+ .HasOne(x => x.Post)
+ .WithMany(x => x.Ratings);
+
+ builder.Entity<Post>()
+ .HasMany(x => x.Ratings)
+ .WithOne(x => x.Post);
+
+ /* Profile Picture */
+ builder.Entity<ProfilePicture>()
+ .HasKey(x => x.Id);
+
+ builder.Entity<User>()
+ .HasOne(x => x.ProfilePicture)
+ .WithOne(x => x.User)
+ .HasForeignKey<ProfilePicture>(x => x.UserId);
+
+ base.OnModelCreating(builder);
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/DevHiveContextFactory.cs b/src/Data/DevHive.Data/DevHiveContextFactory.cs
new file mode 100644
index 0000000..f4849d7
--- /dev/null
+++ b/src/Data/DevHive.Data/DevHiveContextFactory.cs
@@ -0,0 +1,23 @@
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Design;
+using Microsoft.Extensions.Configuration;
+using System.IO;
+
+namespace DevHive.Data
+{
+ public class DevHiveContextFactory : IDesignTimeDbContextFactory<DevHiveContext>
+ {
+ public DevHiveContext CreateDbContext(string[] args)
+ {
+ var configuration = new ConfigurationBuilder()
+ .SetBasePath(Directory.GetCurrentDirectory())
+ .AddJsonFile("ConnectionString.json")
+ .Build();
+
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseNpgsql(configuration.GetConnectionString("DEV"));
+
+ return new DevHiveContext(optionsBuilder.Options);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs b/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs
new file mode 100644
index 0000000..7b553ec
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface ICommentRepository : IRepository<Comment>
+ {
+ Task<List<Comment>> GetPostComments(Guid postId);
+
+ Task<bool> DoesCommentExist(Guid id);
+ Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IFeedRepository.cs b/src/Data/DevHive.Data/Interfaces/IFeedRepository.cs
new file mode 100644
index 0000000..bda51fa
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IFeedRepository.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IFeedRepository
+ {
+ Task<List<Post>> GetFriendsPosts(List<User> friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize);
+ Task<List<Post>> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs
new file mode 100644
index 0000000..af512c5
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface ILanguageRepository : IRepository<Language>
+ {
+ HashSet<Language> GetLanguages();
+ Task<Language> GetByNameAsync(string languageName);
+
+ Task<bool> DoesLanguageExistAsync(Guid id);
+ Task<bool> DoesLanguageNameExistAsync(string languageName);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IPostRepository.cs b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs
new file mode 100644
index 0000000..d40a487
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IPostRepository : IRepository<Post>
+ {
+ Task<bool> AddNewPostToCreator(Guid userId, Post post);
+
+ Task<Post> GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated);
+ Task<List<string>> GetFileUrls(Guid postId);
+
+ Task<bool> DoesPostExist(Guid postId);
+ Task<bool> DoesPostHaveFiles(Guid postId);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IProfilePictureRepository.cs b/src/Data/DevHive.Data/Interfaces/IProfilePictureRepository.cs
new file mode 100644
index 0000000..45beaa0
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IProfilePictureRepository.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IProfilePictureRepository : IRepository<ProfilePicture>
+ {
+ Task<ProfilePicture> GetByURLAsync(string picUrl);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs
new file mode 100644
index 0000000..4840c59
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+namespace DevHive.Data.Interfaces
+{
+ public interface IRatingRepository : IRepository<Rating>
+ {
+ Task<List<Rating>> GetRatingsByPostId(Guid postId);
+ Task<bool> UserRatedPost(Guid userId, Guid postId);
+ Task<Rating> GetRatingByUserAndPostId(Guid userId, Guid postId);
+
+ Task<bool> DoesRatingExist(Guid id);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IRepository.cs b/src/Data/DevHive.Data/Interfaces/IRepository.cs
new file mode 100644
index 0000000..7db8667
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IRepository.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Threading.Tasks;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IRepository<TEntity>
+ where TEntity : class
+ {
+ //Add Entity to database
+ Task<bool> AddAsync(TEntity entity);
+
+ //Find entity by id
+ Task<TEntity> GetByIdAsync(Guid id);
+
+ //Modify Entity from database
+ Task<bool> EditAsync(Guid id, TEntity newEntity);
+
+ //Delete Entity from database
+ Task<bool> DeleteAsync(TEntity entity);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs b/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs
new file mode 100644
index 0000000..b12772c
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IRoleRepository : IRepository<Role>
+ {
+ Task<Role> GetByNameAsync(string name);
+
+ Task<bool> DoesNameExist(string name);
+ Task<bool> DoesRoleExist(Guid id);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs
new file mode 100644
index 0000000..91c82ea
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface ITechnologyRepository : IRepository<Technology>
+ {
+ Task<Technology> GetByNameAsync(string technologyName);
+ HashSet<Technology> GetTechnologies();
+
+ Task<bool> DoesTechnologyExistAsync(Guid id);
+ Task<bool> DoesTechnologyNameExistAsync(string technologyName);
+ }
+}
diff --git a/src/Data/DevHive.Data/Interfaces/IUserRepository.cs b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs
new file mode 100644
index 0000000..494f540
--- /dev/null
+++ b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+
+namespace DevHive.Data.Interfaces
+{
+ public interface IUserRepository : IRepository<User>
+ {
+ Task<bool> AddRoleToUser(User user, string roleName);
+
+ Task<User> GetByUsernameAsync(string username);
+ Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl);
+
+ Task<bool> VerifyPassword(User user, string password);
+ Task<bool> IsInRoleAsync(User user, string roleName);
+ Task<bool> ValidateFriendsCollectionAsync(List<string> usernames);
+ Task<bool> DoesEmailExistAsync(string email);
+ Task<bool> DoesUserExistAsync(Guid id);
+ Task<bool> DoesUsernameExistAsync(string username);
+ Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username);
+ }
+}
diff --git a/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs
new file mode 100644
index 0000000..34c8300
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs
@@ -0,0 +1,600 @@
+// <auto-generated />
+using System;
+using DevHive.Data;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ [DbContext(typeof(DevHiveContext))]
+ [Migration("20210407161947_Initial_migration")]
+ partial class Initial_migration
+ {
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("Relational:MaxIdentifierLength", 63)
+ .HasAnnotation("ProductVersion", "5.0.4")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("Comments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Language", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Languages");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.ToTable("Posts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("PictureURL")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("ProfilePictures");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<bool>("IsLike")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("FileUrl")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("PostAttachments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Role", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedName")
+ .IsUnique()
+ .HasDatabaseName("RoleNameIndex");
+
+ b.ToTable("AspNetRoles");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Technology", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Technologies");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<int>("AccessFailedCount")
+ .HasColumnType("integer");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Email")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<bool>("EmailConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("FirstName")
+ .HasColumnType("text");
+
+ b.Property<string>("LastName")
+ .HasColumnType("text");
+
+ b.Property<bool>("LockoutEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<DateTimeOffset?>("LockoutEnd")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<string>("NormalizedEmail")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedUserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("PasswordHash")
+ .HasColumnType("text");
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnType("text");
+
+ b.Property<bool>("PhoneNumberConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("SecurityStamp")
+ .HasColumnType("text");
+
+ b.Property<bool>("TwoFactorEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("UserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedEmail")
+ .HasDatabaseName("EmailIndex");
+
+ b.HasIndex("NormalizedUserName")
+ .IsUnique()
+ .HasDatabaseName("UserNameIndex");
+
+ b.HasIndex("UserId");
+
+ b.HasIndex("UserName")
+ .IsUnique();
+
+ b.ToTable("AspNetUsers");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.Property<Guid>("LanguagesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LanguagesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("LanguageUser");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetRoleClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderKey")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderDisplayName")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LoginProvider", "ProviderKey");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserLogins");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetUserRoles");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.Property<string>("Value")
+ .HasColumnType("text");
+
+ b.HasKey("UserId", "LoginProvider", "Name");
+
+ b.ToTable("AspNetUserTokens");
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.Property<Guid>("RolesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("RolesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("RoleUser");
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.Property<Guid>("TechnologiesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("TechnologiesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("TechnologyUser");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Comments")
+ .HasForeignKey("CreatorId");
+
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Comments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Creator");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Posts")
+ .HasForeignKey("CreatorId");
+
+ b.Navigation("Creator");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithOne("ProfilePicture")
+ .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Ratings")
+ .HasForeignKey("PostId");
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany()
+ .HasForeignKey("UserId");
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Attachments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany("Friends")
+ .HasForeignKey("UserId");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Language", null)
+ .WithMany()
+ .HasForeignKey("LanguagesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RolesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Technology", null)
+ .WithMany()
+ .HasForeignKey("TechnologiesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Navigation("Attachments");
+
+ b.Navigation("Comments");
+
+ b.Navigation("Ratings");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Navigation("Comments");
+
+ b.Navigation("Friends");
+
+ b.Navigation("Posts");
+
+ b.Navigation("ProfilePicture");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs
new file mode 100644
index 0000000..841c94d
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs
@@ -0,0 +1,526 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ public partial class Initial_migration : Migration
+ {
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "AspNetRoles",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ ConcurrencyStamp = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetRoles", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUsers",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ FirstName = table.Column<string>(type: "text", nullable: true),
+ LastName = table.Column<string>(type: "text", nullable: true),
+ UserId = table.Column<Guid>(type: "uuid", nullable: true),
+ UserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedUserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ Email = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ EmailConfirmed = table.Column<bool>(type: "boolean", nullable: false),
+ PasswordHash = table.Column<string>(type: "text", nullable: true),
+ SecurityStamp = table.Column<string>(type: "text", nullable: true),
+ ConcurrencyStamp = table.Column<string>(type: "text", nullable: true),
+ PhoneNumber = table.Column<string>(type: "text", nullable: true),
+ PhoneNumberConfirmed = table.Column<bool>(type: "boolean", nullable: false),
+ TwoFactorEnabled = table.Column<bool>(type: "boolean", nullable: false),
+ LockoutEnd = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
+ LockoutEnabled = table.Column<bool>(type: "boolean", nullable: false),
+ AccessFailedCount = table.Column<int>(type: "integer", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUsers", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetUsers_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Languages",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Languages", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Technologies",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Technologies", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetRoleClaims",
+ columns: table => new
+ {
+ Id = table.Column<int>(type: "integer", nullable: false)
+ .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
+ RoleId = table.Column<Guid>(type: "uuid", nullable: false),
+ ClaimType = table.Column<string>(type: "text", nullable: true),
+ ClaimValue = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetRoleClaims_AspNetRoles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserClaims",
+ columns: table => new
+ {
+ Id = table.Column<int>(type: "integer", nullable: false)
+ .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ ClaimType = table.Column<string>(type: "text", nullable: true),
+ ClaimValue = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetUserClaims_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserLogins",
+ columns: table => new
+ {
+ LoginProvider = table.Column<string>(type: "text", nullable: false),
+ ProviderKey = table.Column<string>(type: "text", nullable: false),
+ ProviderDisplayName = table.Column<string>(type: "text", nullable: true),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey });
+ table.ForeignKey(
+ name: "FK_AspNetUserLogins_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserRoles",
+ columns: table => new
+ {
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ RoleId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
+ table.ForeignKey(
+ name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_AspNetUserRoles_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserTokens",
+ columns: table => new
+ {
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ LoginProvider = table.Column<string>(type: "text", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: false),
+ Value = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
+ table.ForeignKey(
+ name: "FK_AspNetUserTokens_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Posts",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
+ Message = table.Column<string>(type: "text", nullable: true),
+ TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Posts", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Posts_AspNetUsers_CreatorId",
+ column: x => x.CreatorId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "ProfilePictures",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ PictureURL = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_ProfilePictures", x => x.Id);
+ table.ForeignKey(
+ name: "FK_ProfilePictures_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "RoleUser",
+ columns: table => new
+ {
+ RolesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_RoleUser_AspNetRoles_RolesId",
+ column: x => x.RolesId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_RoleUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "LanguageUser",
+ columns: table => new
+ {
+ LanguagesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_LanguageUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_LanguageUser_Languages_LanguagesId",
+ column: x => x.LanguagesId,
+ principalTable: "Languages",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "TechnologyUser",
+ columns: table => new
+ {
+ TechnologiesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_TechnologyUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_TechnologyUser_Technologies_TechnologiesId",
+ column: x => x.TechnologiesId,
+ principalTable: "Technologies",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Comments",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true),
+ CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
+ Message = table.Column<string>(type: "text", nullable: true),
+ TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Comments", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Comments_AspNetUsers_CreatorId",
+ column: x => x.CreatorId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ table.ForeignKey(
+ name: "FK_Comments_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "PostAttachments",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true),
+ FileUrl = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_PostAttachments", x => x.Id);
+ table.ForeignKey(
+ name: "FK_PostAttachments_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Rating",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ UserId = table.Column<Guid>(type: "uuid", nullable: true),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true),
+ IsLike = table.Column<bool>(type: "boolean", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Rating", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Rating_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ table.ForeignKey(
+ name: "FK_Rating_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetRoleClaims_RoleId",
+ table: "AspNetRoleClaims",
+ column: "RoleId");
+
+ migrationBuilder.CreateIndex(
+ name: "RoleNameIndex",
+ table: "AspNetRoles",
+ column: "NormalizedName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserClaims_UserId",
+ table: "AspNetUserClaims",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserLogins_UserId",
+ table: "AspNetUserLogins",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserRoles_RoleId",
+ table: "AspNetUserRoles",
+ column: "RoleId");
+
+ migrationBuilder.CreateIndex(
+ name: "EmailIndex",
+ table: "AspNetUsers",
+ column: "NormalizedEmail");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUsers_UserId",
+ table: "AspNetUsers",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUsers_UserName",
+ table: "AspNetUsers",
+ column: "UserName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "UserNameIndex",
+ table: "AspNetUsers",
+ column: "NormalizedUserName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Comments_CreatorId",
+ table: "Comments",
+ column: "CreatorId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Comments_PostId",
+ table: "Comments",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_LanguageUser_UsersId",
+ table: "LanguageUser",
+ column: "UsersId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_PostAttachments_PostId",
+ table: "PostAttachments",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Posts_CreatorId",
+ table: "Posts",
+ column: "CreatorId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_ProfilePictures_UserId",
+ table: "ProfilePictures",
+ column: "UserId",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Rating_PostId",
+ table: "Rating",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Rating_UserId",
+ table: "Rating",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_RoleUser_UsersId",
+ table: "RoleUser",
+ column: "UsersId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_TechnologyUser_UsersId",
+ table: "TechnologyUser",
+ column: "UsersId");
+ }
+
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "AspNetRoleClaims");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserClaims");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserLogins");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserRoles");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserTokens");
+
+ migrationBuilder.DropTable(
+ name: "Comments");
+
+ migrationBuilder.DropTable(
+ name: "LanguageUser");
+
+ migrationBuilder.DropTable(
+ name: "PostAttachments");
+
+ migrationBuilder.DropTable(
+ name: "ProfilePictures");
+
+ migrationBuilder.DropTable(
+ name: "Rating");
+
+ migrationBuilder.DropTable(
+ name: "RoleUser");
+
+ migrationBuilder.DropTable(
+ name: "TechnologyUser");
+
+ migrationBuilder.DropTable(
+ name: "Languages");
+
+ migrationBuilder.DropTable(
+ name: "Posts");
+
+ migrationBuilder.DropTable(
+ name: "AspNetRoles");
+
+ migrationBuilder.DropTable(
+ name: "Technologies");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUsers");
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs
new file mode 100644
index 0000000..c451979
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs
@@ -0,0 +1,598 @@
+// <auto-generated />
+using System;
+using DevHive.Data;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ [DbContext(typeof(DevHiveContext))]
+ partial class DevHiveContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("Relational:MaxIdentifierLength", 63)
+ .HasAnnotation("ProductVersion", "5.0.4")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("Comments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Language", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Languages");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.ToTable("Posts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("PictureURL")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("ProfilePictures");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<bool>("IsLike")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("FileUrl")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("PostAttachments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Role", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedName")
+ .IsUnique()
+ .HasDatabaseName("RoleNameIndex");
+
+ b.ToTable("AspNetRoles");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Technology", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Technologies");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<int>("AccessFailedCount")
+ .HasColumnType("integer");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Email")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<bool>("EmailConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("FirstName")
+ .HasColumnType("text");
+
+ b.Property<string>("LastName")
+ .HasColumnType("text");
+
+ b.Property<bool>("LockoutEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<DateTimeOffset?>("LockoutEnd")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<string>("NormalizedEmail")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedUserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("PasswordHash")
+ .HasColumnType("text");
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnType("text");
+
+ b.Property<bool>("PhoneNumberConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("SecurityStamp")
+ .HasColumnType("text");
+
+ b.Property<bool>("TwoFactorEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("UserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedEmail")
+ .HasDatabaseName("EmailIndex");
+
+ b.HasIndex("NormalizedUserName")
+ .IsUnique()
+ .HasDatabaseName("UserNameIndex");
+
+ b.HasIndex("UserId");
+
+ b.HasIndex("UserName")
+ .IsUnique();
+
+ b.ToTable("AspNetUsers");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.Property<Guid>("LanguagesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LanguagesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("LanguageUser");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetRoleClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderKey")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderDisplayName")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LoginProvider", "ProviderKey");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserLogins");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetUserRoles");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.Property<string>("Value")
+ .HasColumnType("text");
+
+ b.HasKey("UserId", "LoginProvider", "Name");
+
+ b.ToTable("AspNetUserTokens");
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.Property<Guid>("RolesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("RolesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("RoleUser");
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.Property<Guid>("TechnologiesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("TechnologiesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("TechnologyUser");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Comments")
+ .HasForeignKey("CreatorId");
+
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Comments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Creator");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Posts")
+ .HasForeignKey("CreatorId");
+
+ b.Navigation("Creator");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithOne("ProfilePicture")
+ .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Ratings")
+ .HasForeignKey("PostId");
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany()
+ .HasForeignKey("UserId");
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Attachments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany("Friends")
+ .HasForeignKey("UserId");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Language", null)
+ .WithMany()
+ .HasForeignKey("LanguagesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RolesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Technology", null)
+ .WithMany()
+ .HasForeignKey("TechnologiesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Navigation("Attachments");
+
+ b.Navigation("Comments");
+
+ b.Navigation("Ratings");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Navigation("Comments");
+
+ b.Navigation("Friends");
+
+ b.Navigation("Posts");
+
+ b.Navigation("ProfilePicture");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Repositories/BaseRepository.cs b/src/Data/DevHive.Data/Repositories/BaseRepository.cs
new file mode 100644
index 0000000..239a3bc
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/BaseRepository.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.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/Data/DevHive.Data/Repositories/CommentRepository.cs b/src/Data/DevHive.Data/Repositories/CommentRepository.cs
new file mode 100644
index 0000000..9364776
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/CommentRepository.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+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/Data/DevHive.Data/Repositories/FeedRepository.cs b/src/Data/DevHive.Data/Repositories/FeedRepository.cs
new file mode 100644
index 0000000..d3312d7
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/FeedRepository.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using AutoMapper.Internal;
+using DevHive.Data.Interfaces;
+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/Data/DevHive.Data/Repositories/LanguageRepository.cs b/src/Data/DevHive.Data/Repositories/LanguageRepository.cs
new file mode 100644
index 0000000..3528ea8
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/LanguageRepository.cs
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+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/Data/DevHive.Data/Repositories/PostRepository.cs b/src/Data/DevHive.Data/Repositories/PostRepository.cs
new file mode 100644
index 0000000..b5228c2
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/PostRepository.cs
@@ -0,0 +1,102 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Data.Models.Relational;
+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.Ratings)
+ .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);
+
+ 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);
+
+ 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/Data/DevHive.Data/Repositories/ProfilePictureRepository.cs b/src/Data/DevHive.Data/Repositories/ProfilePictureRepository.cs
new file mode 100644
index 0000000..7284464
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/ProfilePictureRepository.cs
@@ -0,0 +1,25 @@
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data.Repositories
+{
+ public class ProfilePictureRepository : BaseRepository<ProfilePicture>, IProfilePictureRepository
+ {
+ private readonly DevHiveContext _context;
+
+ public ProfilePictureRepository(DevHiveContext context)
+ : base(context)
+ {
+ this._context = context;
+ }
+
+ public async Task<ProfilePicture> GetByURLAsync(string picUrl)
+ {
+ return await this._context
+ .ProfilePicture
+ .FirstOrDefaultAsync(x => x.PictureURL == picUrl);
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs
new file mode 100644
index 0000000..2048c3f
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs
@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data.Repositories
+{
+ public class RatingRepository : BaseRepository<Rating>, IRatingRepository
+ {
+ private readonly DevHiveContext _context;
+
+ public RatingRepository(DevHiveContext context)
+ : base(context)
+ {
+ this._context = context;
+ }
+
+ public override async Task<Rating> GetByIdAsync(Guid id)
+ {
+ return await this._context.Rating
+ .Include(x => x.User)
+ .Include(x => x.Post)
+ .FirstOrDefaultAsync(x => x.Id == id);
+ }
+ /// <summary>
+ /// Gets all the ratings for a post.
+ /// </summary>
+ /// <param name="postId">Id of the post.</param>
+ /// <returns></returns>
+ public async Task<List<Rating>> GetRatingsByPostId(Guid postId)
+ {
+ return await this._context.Rating
+ .Include(x => x.User)
+ .Include(x => x.Post)
+ .Where(x => x.Post.Id == postId).ToListAsync();
+ }
+ /// <summary>
+ /// Checks if a user rated a given post. In DevHive every user has one or no rating for every post.
+ /// </summary>
+ /// <param name="userId">Id of the user.</param>
+ /// <param name="postId">Id of the post.</param>
+ /// <returns>True if the user has already rated the post and false if he hasn't.</returns>
+ public async Task<bool> UserRatedPost(Guid userId, Guid postId)
+ {
+ return await this._context.Rating
+ .Where(x => x.Post.Id == postId)
+ .AnyAsync(x => x.User.Id == userId);
+ }
+ /// <summary>
+ /// Gets a rating by the post to which the rating corresponds and the user who created it.
+ /// </summary>
+ /// <param name="userId">Id of the user.</param>
+ /// <param name="postId">Id of the post.</param>
+ /// <returns>Rating for the given post by the given user.</returns>
+ public async Task<Rating> GetRatingByUserAndPostId(Guid userId, Guid postId)
+ {
+ return await this._context.Rating
+ .Include(x => x.User)
+ .Include(x => x.Post)
+ .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId);
+ }
+
+ /// <summary>
+ /// Checks if a given rating already exist
+ /// </summary>
+ /// <param name="id">Id of the rating</param>
+ /// <returns>True if the rating exists and false if it does not.</returns>
+ public async Task<bool> DoesRatingExist(Guid id)
+ {
+ return await this._context.Rating
+ .AsNoTracking()
+ .AnyAsync(r => r.Id == id);
+ }
+ }
+}
+
diff --git a/src/Data/DevHive.Data/Repositories/RoleRepository.cs b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
new file mode 100644
index 0000000..99e0634
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data.Repositories
+{
+ public class RoleRepository : BaseRepository<Role>, IRoleRepository
+ {
+ private readonly RoleManager<Role> _roleManager;
+
+ public RoleRepository(DevHiveContext context, RoleManager<Role> roleManager)
+ : base(context)
+ {
+ this._roleManager = roleManager;
+ }
+
+ #region Create
+ public override async Task<bool> AddAsync(Role entity)
+ {
+ IdentityResult result = await this._roleManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
+ #region Read
+ public async Task<Role> GetByNameAsync(string name)
+ {
+ return await this._roleManager.FindByNameAsync(name);
+ }
+ #endregion
+
+ public override async Task<bool> EditAsync(Guid id, Role newEntity)
+ {
+ newEntity.Id = id;
+ IdentityResult result = await this._roleManager.UpdateAsync(newEntity);
+
+ return result.Succeeded;
+ }
+
+ #region Validations
+ public async Task<bool> DoesNameExist(string name)
+ {
+ return await this._roleManager.RoleExistsAsync(name);
+ }
+
+ public async Task<bool> DoesRoleExist(Guid id)
+ {
+ return await this._roleManager.Roles.AnyAsync(r => r.Id == id);
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data/Repositories/TechnologyRepository.cs b/src/Data/DevHive.Data/Repositories/TechnologyRepository.cs
new file mode 100644
index 0000000..d0d1f3f
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/TechnologyRepository.cs
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+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/Data/DevHive.Data/Repositories/UserRepository.cs b/src/Data/DevHive.Data/Repositories/UserRepository.cs
new file mode 100644
index 0000000..d570480
--- /dev/null
+++ b/src/Data/DevHive.Data/Repositories/UserRepository.cs
@@ -0,0 +1,152 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data.Repositories
+{
+ public class UserRepository : BaseRepository<User>, IUserRepository
+ {
+ private readonly UserManager<User> _userManager;
+ private readonly RoleManager<Role> _roleManager;
+
+ public UserRepository(DevHiveContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
+ : base(context)
+ {
+ this._userManager = userManager;
+ this._roleManager = roleManager;
+ }
+
+ #region Create
+ public override async Task<bool> AddAsync(User entity)
+ {
+ entity.PasswordHash = this._userManager.PasswordHasher.HashPassword(entity, entity.PasswordHash).ToString();
+ IdentityResult result = await this._userManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+
+ public async Task<bool> AddRoleToUser(User user, string roleName)
+ {
+ bool succeeded = (await this._userManager.AddToRoleAsync(user, roleName)).Succeeded;
+ if (succeeded)
+ {
+ user.Roles.Add(await this._roleManager.FindByNameAsync(roleName));
+ succeeded = await this.SaveChangesAsync();
+ }
+
+ return succeeded;
+ }
+ #endregion
+
+ #region Read
+ public override async Task<User> GetByIdAsync(Guid id)
+ {
+ return await this._userManager.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._userManager.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 override async Task<bool> EditAsync(Guid id, User newEntity)
+ {
+ newEntity.Id = id;
+ IdentityResult result = await this._userManager.UpdateAsync(newEntity);
+
+ return result.Succeeded;
+ }
+
+ 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 Delete
+ public override async Task<bool> DeleteAsync(User entity)
+ {
+ IdentityResult result = await this._userManager.DeleteAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
+ #region Validations
+ public async Task<bool> VerifyPassword(User user, string password)
+ {
+ return await this._userManager.CheckPasswordAsync(user, password);
+ }
+
+ public async Task<bool> IsInRoleAsync(User user, string roleName)
+ {
+ return await this._userManager.IsInRoleAsync(user, roleName);
+ }
+
+ public async Task<bool> DoesUserExistAsync(Guid id)
+ {
+ return await this._userManager.Users.AnyAsync(x => x.Id == id);
+ }
+
+ public async Task<bool> DoesUsernameExistAsync(string username)
+ {
+ return await this._userManager.Users
+ .AsNoTracking()
+ .AnyAsync(u => u.UserName == username);
+ }
+
+ public async Task<bool> DoesEmailExistAsync(string email)
+ {
+ return await this._userManager.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.DoesUsernameExistAsync(username))
+ {
+ valid = false;
+ break;
+ }
+ }
+ return valid;
+ }
+
+ public async Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username)
+ {
+ return await this._userManager.Users
+ .AnyAsync(x => x.Id == id &&
+ x.UserName == username);
+ }
+ #endregion
+ }
+}