diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Data/DevHive.Data/ConnectionString.json | 6 | ||||
| -rw-r--r-- | src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs (renamed from src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.Designer.cs) | 6 | ||||
| -rw-r--r-- | src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs (renamed from src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.cs) | 2 | ||||
| -rw-r--r-- | src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs | 2 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs | 4 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs | 3 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Interfaces/IFriendsService.cs | 11 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Services/FriendsService.cs | 45 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs | 1 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/FriendsController.cs | 54 |
10 files changed, 125 insertions, 9 deletions
diff --git a/src/Data/DevHive.Data/ConnectionString.json b/src/Data/DevHive.Data/ConnectionString.json index 1281a1c..ec065d1 100644 --- a/src/Data/DevHive.Data/ConnectionString.json +++ b/src/Data/DevHive.Data/ConnectionString.json @@ -1,5 +1,5 @@ { - "ConnectionStrings": { - "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=password;" - } + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=;" + } } diff --git a/src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.Designer.cs b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs index a3fcb2d..34c8300 100644 --- a/src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.Designer.cs +++ b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.Designer.cs @@ -10,15 +10,15 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { [DbContext(typeof(DevHiveContext))] - [Migration("20210328161726_Profile_Pic_Property_Alter")] - partial class Profile_Pic_Property_Alter + [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.3") + .HasAnnotation("ProductVersion", "5.0.4") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); modelBuilder.Entity("DevHive.Data.Models.Comment", b => diff --git a/src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.cs b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs index 5aca664..841c94d 100644 --- a/src/Data/DevHive.Data/Migrations/20210328161726_Profile_Pic_Property_Alter.cs +++ b/src/Data/DevHive.Data/Migrations/20210407161947_Initial_migration.cs @@ -4,7 +4,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { - public partial class Profile_Pic_Property_Alter : Migration + public partial class Initial_migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git a/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 242473b..c451979 100644 --- a/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -16,7 +16,7 @@ namespace DevHive.Data.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("Relational:MaxIdentifierLength", 63) - .HasAnnotation("ProductVersion", "5.0.3") + .HasAnnotation("ProductVersion", "5.0.4") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); modelBuilder.Entity("DevHive.Data.Models.Comment", b => diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 37f259a..e870ab1 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,5 +1,5 @@ -using DevHive.Data.Models; using AutoMapper; +using DevHive.Data.Models; using DevHive.Services.Models.Role; namespace DevHive.Services.Configurations.Mapping @@ -14,6 +14,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap<Role, RoleServiceModel>(); CreateMap<Role, UpdateRoleServiceModel>(); + + CreateMap<RoleServiceModel, UpdateRoleServiceModel>(); } } } diff --git a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs index 99b41a8..5a39f73 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -23,6 +23,9 @@ namespace DevHive.Services.Configurations.Mapping CreateMap<User, UpdateUserServiceModel>() .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); CreateMap<User, FriendServiceModel>(); + + CreateMap<UserServiceModel, UpdateUserServiceModel>(); + CreateMap<UserServiceModel, UpdateFriendServiceModel>(); } } } diff --git a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs new file mode 100644 index 0000000..e337793 --- /dev/null +++ b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs @@ -0,0 +1,11 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Services.Interfaces +{ + public interface IFriendsService + { + Task<bool> AddFriend(Guid userId, Guid friendId); + Task<bool> RemoveFriend(Guid userId, Guid friendId); + } +} diff --git a/src/Services/DevHive.Services/Services/FriendsService.cs b/src/Services/DevHive.Services/Services/FriendsService.cs new file mode 100644 index 0000000..4e3e355 --- /dev/null +++ b/src/Services/DevHive.Services/Services/FriendsService.cs @@ -0,0 +1,45 @@ +using System; +using System.Threading.Tasks; +using DevHive.Common.Constants; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; + +namespace DevHive.Services.Services +{ + public class FriendsService : IFriendsService + { + private readonly IUserRepository _friendRepository; + + public FriendsService(IUserRepository friendRepository) + { + this._friendRepository = friendRepository; + } + + public async Task<bool> AddFriend(Guid userId, Guid friendId) + { + User user = await this._friendRepository.GetByIdAsync(userId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); + + User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); + + bool addedToUser = user.Friends.Add(friend) && await this._friendRepository.EditAsync(userId, user); + bool addedToFriend = friend.Friends.Add(user) && await this._friendRepository.EditAsync(friendId, friend); + return addedToUser && addedToFriend; + } + + public async Task<bool> RemoveFriend(Guid userId, Guid friendId) + { + User user = await this._friendRepository.GetByIdAsync(userId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); + + User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); + + bool addedToUser = user.Friends.Remove(friend) && await this._friendRepository.EditAsync(userId, user); + bool addedToFriend = friend.Friends.Remove(user) && await this._friendRepository.EditAsync(friendId, friend); + return addedToUser && addedToFriend; + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 20b66e1..f49a335 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -33,6 +33,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient<IRatingService, RatingService>(); services.AddTransient<IProfilePictureService, ProfilePictureService>(); services.AddTransient<IUserService, UserService>(); + services.AddTransient<IFriendsService, FriendsService>(); services.AddTransient<ICloudService, CloudinaryService>(options => new CloudinaryService( diff --git a/src/Web/DevHive.Web/Controllers/FriendsController.cs b/src/Web/DevHive.Web/Controllers/FriendsController.cs new file mode 100644 index 0000000..9f3ec13 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/FriendsController.cs @@ -0,0 +1,54 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Common.Models.Identity; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.User; +using DevHive.Web.Models.User; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NSwag.Annotations; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class FriendsController + { + private readonly IFriendsService _friendsService; + private readonly IMapper _mapper; + private readonly IJwtService _jwtService; + + public FriendsController(IFriendsService friendsService, IMapper mapper, IJwtService jwtService) + { + this._friendsService = friendsService; + this._mapper = mapper; + this._jwtService = jwtService; + } + + [HttpPost] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> AddFriend(Guid userId, Guid friendId, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return (await this._friendsService.AddFriend(userId, friendId)) ? + new OkResult() : + new BadRequestResult(); + } + + [HttpDelete] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> RemoveFriend(Guid userId, Guid friendId, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return (await this._friendsService.RemoveFriend(userId, friendId)) ? + new OkResult() : + new BadRequestResult(); + } + } +} |
