aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Data/DevHiveContext.cs6
-rw-r--r--src/DevHive.Data/Repositories/FriendsRepository.cs38
-rw-r--r--src/DevHive.Data/Repositories/LanguageRepository.cs4
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs4
-rw-r--r--src/DevHive.Data/Repositories/TechnologyRepository.cs4
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs20
-rw-r--r--src/DevHive.Services/Services/FriendsService.cs76
-rw-r--r--src/DevHive.Services/Services/UserService.cs73
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs3
-rw-r--r--src/DevHive.Web/Controllers/FriendsController.cs59
-rw-r--r--src/DevHive.Web/Controllers/UserController.cs28
-rw-r--r--src/DevHive.Web/Startup.cs2
12 files changed, 117 insertions, 200 deletions
diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs
index e8eb5fb..39d39d3 100644
--- a/src/DevHive.Data/DevHiveContext.cs
+++ b/src/DevHive.Data/DevHiveContext.cs
@@ -7,7 +7,7 @@ namespace DevHive.Data
{
public class DevHiveContext : IdentityDbContext<User, Role, Guid>
{
- public DevHiveContext(DbContextOptions options)
+ public DevHiveContext(DbContextOptions<DevHiveContext> options)
: base(options) { }
public DbSet<Technology> Technologies { get; set; }
@@ -25,10 +25,6 @@ namespace DevHive.Data
builder.Entity<User>()
.HasMany(x => x.Friends);
- builder.Entity<Role>()
- .HasIndex(x => x.Id)
- .IsUnique();
-
base.OnModelCreating(builder);
}
}
diff --git a/src/DevHive.Data/Repositories/FriendsRepository.cs b/src/DevHive.Data/Repositories/FriendsRepository.cs
deleted file mode 100644
index c65ba5e..0000000
--- a/src/DevHive.Data/Repositories/FriendsRepository.cs
+++ /dev/null
@@ -1,38 +0,0 @@
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-using DevHive.Common.Models.Data;
-using DevHive.Data.Models;
-using Microsoft.EntityFrameworkCore;
-
-namespace DevHive.Data.Repositories
-{
- public class FriendsRepository : UserRepository
- {
- private readonly DbContext _context;
-
- public FriendsRepository(DbContext context)
- : base(context)
- {
- this._context = context;
- }
-
- //Create
- public async Task<bool> AddFriendAsync(User user, User friend)
- {
- this._context.Update(user);
- user.Friends.Add(friend);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- //Delete
- public async Task<bool> RemoveFriendAsync(User user, User friend)
- {
- this._context.Update(user);
- user.Friends.Remove(friend);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- }
-} \ No newline at end of file
diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs
index a33bd5b..6e3a2e3 100644
--- a/src/DevHive.Data/Repositories/LanguageRepository.cs
+++ b/src/DevHive.Data/Repositories/LanguageRepository.cs
@@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories
{
public class LanguageRepository : IRepository<Language>
{
- private readonly DbContext _context;
+ private readonly DevHiveContext _context;
- public LanguageRepository(DbContext context)
+ public LanguageRepository(DevHiveContext context)
{
this._context = context;
}
diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs
index 21c29db..1d6f2da 100644
--- a/src/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/DevHive.Data/Repositories/RoleRepository.cs
@@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories
{
public class RoleRepository : IRepository<Role>
{
- private readonly DbContext _context;
+ private readonly DevHiveContext _context;
- public RoleRepository(DbContext context)
+ public RoleRepository(DevHiveContext context)
{
this._context = context;
}
diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs
index debfd3e..e2e4257 100644
--- a/src/DevHive.Data/Repositories/TechnologyRepository.cs
+++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs
@@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories
{
public class TechnologyRepository : IRepository<Technology>
{
- private readonly DbContext _context;
+ private readonly DevHiveContext _context;
- public TechnologyRepository(DbContext context)
+ public TechnologyRepository(DevHiveContext context)
{
this._context = context;
}
diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs
index 5cd4264..d2a8830 100644
--- a/src/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/DevHive.Data/Repositories/UserRepository.cs
@@ -11,9 +11,9 @@ namespace DevHive.Data.Repositories
{
public class UserRepository : IRepository<User>
{
- private readonly DbContext _context;
+ private readonly DevHiveContext _context;
- public UserRepository(DbContext context)
+ public UserRepository(DevHiveContext context)
{
this._context = context;
}
@@ -27,6 +27,14 @@ namespace DevHive.Data.Repositories
return await RepositoryMethods.SaveChangesAsync(this._context);
}
+
+ public async Task<bool> AddFriendAsync(User user, User friend)
+ {
+ this._context.Update(user);
+ user.Friends.Add(friend);
+
+ return await RepositoryMethods.SaveChangesAsync(this._context);
+ }
//Read
public IEnumerable<User> QueryAll()
@@ -77,6 +85,14 @@ namespace DevHive.Data.Repositories
return await RepositoryMethods.SaveChangesAsync(this._context);
}
+
+ public async Task<bool> RemoveFriendAsync(User user, User friend)
+ {
+ this._context.Update(user);
+ user.Friends.Remove(friend);
+
+ return await RepositoryMethods.SaveChangesAsync(this._context);
+ }
//Validations
public bool DoesUserExist(Guid id)
diff --git a/src/DevHive.Services/Services/FriendsService.cs b/src/DevHive.Services/Services/FriendsService.cs
deleted file mode 100644
index 119c955..0000000
--- a/src/DevHive.Services/Services/FriendsService.cs
+++ /dev/null
@@ -1,76 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using AutoMapper;
-using DevHive.Data.Models;
-using DevHive.Data.Repositories;
-using DevHive.Services.Models.Identity.User;
-using DevHive.Services.Options;
-
-namespace DevHive.Services.Services
-{
- public class FriendsService
- {
- private readonly FriendsRepository _friendsRepository;
- private readonly IMapper _friendsMapper;
-
- public FriendsService(FriendsRepository friendsRepository, IMapper mapper)
- {
- this._friendsRepository = friendsRepository;
- this._friendsMapper = mapper;
- }
-
- //Create
- public async Task<bool> AddFriend(Guid userId, Guid friendId)
- {
- User user = await this._friendsRepository.GetByIdAsync(userId);
- User friend = await this._friendsRepository.GetByIdAsync(friendId);
-
- if (DoesUserHaveThisFriend(user, friend))
- throw new ArgumentException("Friend already exists in your friends list.");
-
- return user != default(User) && friend != default(User) ?
- await this._friendsRepository.AddFriendAsync(user, friend) :
- throw new ArgumentException("Invalid user!");
- }
-
- //Read
- public async Task<UserServiceModel> GetFriendById(Guid friendId)
- {
- if(!_friendsRepository.DoesUserExist(friendId))
- throw new ArgumentException("User does not exist!");
-
- User friend = await this._friendsRepository.GetByIdAsync(friendId);
-
- return this._friendsMapper.Map<UserServiceModel>(friend);
- }
-
- public async Task<bool> RemoveFriend(Guid userId, Guid friendId)
- {
- if(!this._friendsRepository.DoesUserExist(userId) &&
- !this._friendsRepository.DoesUserExist(friendId))
- throw new ArgumentException("Invalid user!");
-
- User user = await this._friendsRepository.GetByIdAsync(userId);
- User friend = await this._friendsRepository.GetByIdAsync(friendId);
-
- if(!this.DoesUserHaveFriends(user))
- throw new ArgumentException("User does not have any friends.");
-
- if (!DoesUserHaveThisFriend(user, friend))
- throw new ArgumentException("This ain't your friend, amigo.");
-
- return await this.RemoveFriend(user.Id, friendId);
- }
-
- //Validation
- private bool DoesUserHaveThisFriend(User user, User friend)
- {
- return user.Friends.Contains(friend);
- }
-
- private bool DoesUserHaveFriends(User user)
- {
- return user.Friends.Count >= 1;
- }
- }
-} \ No newline at end of file
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index 99a8d9f..7fd0682 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -67,6 +67,19 @@ namespace DevHive.Services.Services
return new TokenModel(WriteJWTSecurityToken(user.UserName, user.Roles));
}
+ public async Task<bool> AddFriend(Guid userId, Guid friendId)
+ {
+ User user = await this._userRepository.GetByIdAsync(userId);
+ User friend = await this._userRepository.GetByIdAsync(friendId);
+
+ if (DoesUserHaveThisFriend(user, friend))
+ throw new ArgumentException("Friend already exists in your friends list.");
+
+ return user != default(User) && friend != default(User) ?
+ await this._userRepository.AddFriendAsync(user, friend) :
+ throw new ArgumentException("Invalid user!");
+ }
+
public async Task<UserServiceModel> GetUserById(Guid id)
{
User user = await this._userRepository.GetByIdAsync(id)
@@ -75,6 +88,16 @@ namespace DevHive.Services.Services
return this._userMapper.Map<UserServiceModel>(user);
}
+ public async Task<UserServiceModel> GetFriendById(Guid friendId)
+ {
+ if(!_userRepository.DoesUserExist(friendId))
+ throw new ArgumentException("User does not exist!");
+
+ User friend = await this._userRepository.GetByIdAsync(friendId);
+
+ return this._userMapper.Map<UserServiceModel>(friend);
+ }
+
public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel)
{
if (!this._userRepository.DoesUserExist(updateModel.Id))
@@ -105,6 +128,24 @@ namespace DevHive.Services.Services
throw new InvalidOperationException("Unable to delete user!");
}
+ public async Task<bool> RemoveFriend(Guid userId, Guid friendId)
+ {
+ if(!this._userRepository.DoesUserExist(userId) &&
+ !this._userRepository.DoesUserExist(friendId))
+ throw new ArgumentException("Invalid user!");
+
+ User user = await this._userRepository.GetByIdAsync(userId);
+ User friend = await this._userRepository.GetByIdAsync(friendId);
+
+ if(!this.DoesUserHaveFriends(user))
+ throw new ArgumentException("User does not have any friends.");
+
+ if (!DoesUserHaveThisFriend(user, friend))
+ throw new ArgumentException("This ain't your friend, amigo.");
+
+ return await this.RemoveFriend(user.Id, friendId);
+ }
+
public async Task<bool> ValidJWT(Guid id, string rawTokenData)
{
// There is authorization name in the beginning, i.e. "Bearer eyJh..."
@@ -143,6 +184,27 @@ namespace DevHive.Services.Services
return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password)));
}
+ private bool DoesUserHaveThisFriend(User user, User friend)
+ {
+ return user.Friends.Contains(friend);
+ }
+
+ private bool DoesUserHaveFriends(User user)
+ {
+ return user.Friends.Count >= 1;
+ }
+
+ private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
+ {
+ List<string> toReturn = new();
+
+ foreach(var claim in claims)
+ if (claim.Type == type)
+ toReturn.Add(claim.Value);
+
+ return toReturn;
+ }
+
private string WriteJWTSecurityToken(string userName, IList<Role> roles)
{
byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
@@ -170,16 +232,5 @@ namespace DevHive.Services.Services
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
-
- private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
- {
- List<string> toReturn = new();
-
- foreach(var claim in claims)
- if (claim.Type == type)
- toReturn.Add(claim.Value);
-
- return toReturn;
- }
}
}
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
index 7ef144c..b42ae05 100644
--- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
+++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
@@ -16,8 +16,7 @@ namespace DevHive.Web.Configurations.Extensions
public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<DevHiveContext>(options =>
- options.UseNpgsql(configuration.GetConnectionString("DEV"),
- x => x.MigrationsAssembly("DevHive.Web")));
+ options.UseNpgsql(configuration.GetConnectionString("DEV")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<DevHiveContext>();
diff --git a/src/DevHive.Web/Controllers/FriendsController.cs b/src/DevHive.Web/Controllers/FriendsController.cs
deleted file mode 100644
index 1b9b9c5..0000000
--- a/src/DevHive.Web/Controllers/FriendsController.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using AutoMapper;
-using DevHive.Common.Models;
-using DevHive.Data.Repositories;
-using DevHive.Services.Models.Identity.User;
-using DevHive.Services.Options;
-using DevHive.Services.Services;
-using DevHive.Web.Models.Identity.User;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-
-namespace DevHive.Web.Controllers
-{
- [ApiController]
- [Route("/api")]
- [Authorize(Roles = "User")]
- public class FriendsController
- {
- private readonly FriendsService _friendsService;
- private readonly IMapper _friendsMapper;
-
- public FriendsController(FriendsService friendsService, IMapper mapper)
- {
- this._friendsService = friendsService;
- this._friendsMapper = mapper;
- }
-
- //Create
- [HttpPost]
- [Route("AddAFriend")]
- public async Task<IActionResult> AddAFriend(Guid userId, [FromBody] IdModel friendIdModel)
- {
- return await this._friendsService.AddFriend(userId, friendIdModel.Id) ?
- new OkResult() :
- new BadRequestResult();
- }
-
- //Read
- [HttpGet]
- [Route("GetAFriend")]
- public async Task<IActionResult> GetAFriend(Guid friendId)
- {
- UserServiceModel friendServiceModel = await this._friendsService.GetFriendById(friendId);
- UserWebModel friend = this._friendsMapper.Map<UserWebModel>(friendServiceModel);
-
- return new OkObjectResult(friend);
- }
-
- //Delete
- [HttpDelete]
- [Route("RemoveAFriend")]
- public async Task<IActionResult> RemoveAFriend(Guid userId, Guid friendId)
- {
- await this._friendsService.RemoveFriend(userId, friendId);
- return new OkResult();
- }
- }
-} \ No newline at end of file
diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs
index b23fdee..ad2656c 100644
--- a/src/DevHive.Web/Controllers/UserController.cs
+++ b/src/DevHive.Web/Controllers/UserController.cs
@@ -9,6 +9,7 @@ using DevHive.Web.Models.Identity.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using DevHive.Common.Models.Identity;
+using DevHive.Common.Models;
namespace DevHive.Web.Controllers
{
@@ -53,6 +54,15 @@ namespace DevHive.Web.Controllers
return new CreatedResult("Register", tokenWebModel);
}
+ [HttpPost]
+ [Route("AddAFriend")]
+ public async Task<IActionResult> AddAFriend(Guid userId, [FromBody] IdModel friendIdModel)
+ {
+ return await this._userService.AddFriend(userId, friendIdModel.Id) ?
+ new OkResult() :
+ new BadRequestResult();
+ }
+
//Read
[HttpGet]
public async Task<IActionResult> GetById(Guid id, [FromHeader] string authorization)
@@ -66,6 +76,16 @@ namespace DevHive.Web.Controllers
return new OkObjectResult(userWebModel);
}
+ [HttpGet]
+ [Route("GetAFriend")]
+ public async Task<IActionResult> GetAFriend(Guid friendId)
+ {
+ UserServiceModel friendServiceModel = await this._userService.GetFriendById(friendId);
+ UserWebModel friend = this._userMapper.Map<UserWebModel>(friendServiceModel);
+
+ return new OkObjectResult(friend);
+ }
+
//Update
[HttpPut]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization)
@@ -93,5 +113,13 @@ namespace DevHive.Web.Controllers
await this._userService.DeleteUser(id);
return new OkResult();
}
+
+ [HttpDelete]
+ [Route("RemoveAFriend")]
+ public async Task<IActionResult> RemoveAFriend(Guid userId, Guid friendId)
+ {
+ await this._userService.RemoveFriend(userId, friendId);
+ return new OkResult();
+ }
}
}
diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs
index 66fde9e..12f72f7 100644
--- a/src/DevHive.Web/Startup.cs
+++ b/src/DevHive.Web/Startup.cs
@@ -45,7 +45,7 @@ namespace DevHive.Web
}
else
{
- app.UseExceptionHandler("/api/HttpError");
+ app.UseExceptionHandler("/api/Error");
app.UseHsts();
}