diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-14 23:29:14 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-14 23:29:14 +0200 |
| commit | dee2e37a4a8759108390c664e06bf147b8385cbf (patch) | |
| tree | bd65fe5649731a55aa6f1d8b48d53d89032fb8be /src/DevHive.Data | |
| parent | 1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff) | |
| download | DevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.tar DevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.tar.gz DevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.zip | |
Stabalized project for compilation. Next step after init architecture
Diffstat (limited to 'src/DevHive.Data')
| -rw-r--r-- | src/DevHive.Data/DevHive.Data.csproj | 7 | ||||
| -rw-r--r-- | src/DevHive.Data/Models/Language.cs | 9 | ||||
| -rw-r--r-- | src/DevHive.Data/Models/Technology.cs | 9 | ||||
| -rw-r--r-- | src/DevHive.Data/Models/User.cs | 67 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/DevHiveContext.cs | 26 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/IRepository.cs | 24 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/UserRepository.cs | 69 |
7 files changed, 211 insertions, 0 deletions
diff --git a/src/DevHive.Data/DevHive.Data.csproj b/src/DevHive.Data/DevHive.Data.csproj index 563e6f9..0becbe2 100644 --- a/src/DevHive.Data/DevHive.Data.csproj +++ b/src/DevHive.Data/DevHive.Data.csproj @@ -4,4 +4,11 @@ <TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="AutoMapper" Version="10.1.1" />
+ <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
+
+ <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
+ </ItemGroup>
+
</Project>
diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs new file mode 100644 index 0000000..a455796 --- /dev/null +++ b/src/DevHive.Data/Models/Language.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Data.Models +{ + public class Language + { + public Guid Id { get; set; } + } +} diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs new file mode 100644 index 0000000..6f2afeb --- /dev/null +++ b/src/DevHive.Data/Models/Technology.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Data.Models +{ + public class Technology + { + public Guid Id { get; set; } + } +} diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs new file mode 100644 index 0000000..fb1fb43 --- /dev/null +++ b/src/DevHive.Data/Models/User.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.AspNetCore.Identity; + +namespace DevHive.Data.Models +{ + [Table("Users")] + public class User : IdentityUser<Guid> + { + private string _firstName; + private string _lastName; + + [Required] + [Range(3, 50)] + [Display(Name = "Username")] + public override string UserName + { + get => base.UserName; + set + { + ValidateString("Username", 3, 50, value, true); + base.UserName = value; + } + } + + [Required] + [Range(3, 30)] + public string FirstName + { + get => this._firstName; + set + { + ValidateString("FirstName", 3, 30, value, false); + this._firstName = value; + } + } + + [Required] + [Range(3, 30)] + public string LastName + { + get => this._lastName; + set + { + ValidateString("LastName", 3, 30, value, false); + this._lastName = value; + } + } + + public string ProfilePicture { get; set; } + + public string Role { get; set; } + + //public List<User> Friends { get; set; } + + private static void ValidateString(string propertyName, int minLength, int maxLength, string value, bool canBeDigit) + { + if (value.Length < minLength || value.Length > maxLength) + throw new ArgumentException($"{propertyName} length cannot be less than {minLength} and more than {maxLength}."); + + foreach (char ch in value) + if (!Char.IsLetter(ch) && !(Char.IsDigit(ch) && canBeDigit)) + throw new ArgumentException($"{propertyName} contains invalid characters."); + } + } +} diff --git a/src/DevHive.Data/Repositories/DevHiveContext.cs b/src/DevHive.Data/Repositories/DevHiveContext.cs new file mode 100644 index 0000000..be5cc56 --- /dev/null +++ b/src/DevHive.Data/Repositories/DevHiveContext.cs @@ -0,0 +1,26 @@ +using System; +using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class DevHiveContext : IdentityDbContext<User, IdentityRole<Guid>, Guid> + { + public DevHiveContext(DbContextOptions options) + : base(options) { } + + public DbSet<Technology> Technologies { get; set; } + public DbSet<Language> Languages { get; set; } + + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity<User>() + .HasIndex(x => x.UserName) + .IsUnique(); + + base.OnModelCreating(builder); + } + } +} diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs new file mode 100644 index 0000000..81a1a35 --- /dev/null +++ b/src/DevHive.Data/Repositories/IRepository.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Data.Models.Interfaces.Database +{ + public interface IRepository<TEntity> + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Return *count* instances of Entity from the database + IEnumerable<TEntity> Query(int count); + + //Find entity by id + Task<TEntity> FindByIdAsync(object id); + + //Modify Entity from database + Task EditAsync(object id, TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(object id); + } +}
\ No newline at end of file diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs new file mode 100644 index 0000000..5bd9d97 --- /dev/null +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Data.Models; + +namespace DevHive.Data.Repositories +{ + public class UserRepository : IRepository<User> + { + /* private readonly UserRepository<User> _dbRepository; + + public UserRepository(DbContext context) + : base (context) + { + this._dbRepository = new DbRepository<User>(context); + } + + public User FindByUsername(string username) + { + return this._dbRepository.DbSet + .FirstOrDefault(usr => usr.UserName == username); + } + + public bool DoesUsernameExist(string username) + { + return this._dbRepository.DbSet + .Any(x => x.UserName == username); + } + + public bool DoesUserExist(Guid id) + { + return this._dbRepository.DbSet + .Any(x => x.Id == id); + } + + public bool HasThisUsername(Guid id, string username) + { + return this._dbRepository.DbSet + .Any(x => x.Id == id && + x.UserName == username); + } */ + + public Task AddAsync(User entity) + { + throw new System.NotImplementedException(); + } + + public IEnumerable<User> Query(int count) + { + throw new System.NotImplementedException(); + } + + public Task<User> FindByIdAsync(object id) + { + throw new System.NotImplementedException(); + } + + public Task EditAsync(object id, User newEntity) + { + throw new System.NotImplementedException(); + } + + public Task DeleteAsync(object id) + { + throw new System.NotImplementedException(); + } + } +} |
