diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Web/DevHive.Web | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-0.2.tar DevHive-0.2.tar.gz DevHive-0.2.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Web/DevHive.Web')
31 files changed, 1831 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs new file mode 100644 index 0000000..cd5679f --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs @@ -0,0 +1,26 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using AutoMapper; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureAutoMapper + { + public static void AutoMapperConfiguration(this IServiceCollection services) + { + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + } + + public static void UseAutoMapperConfiguration(this IApplicationBuilder app) + { + _ = new MapperConfiguration(cfg => + { + cfg.AllowNullCollections = true; + }); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs new file mode 100644 index 0000000..b4c49b4 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -0,0 +1,98 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Builder; +using System; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using DevHive.Data; +using System.Linq; +using System.Threading.Tasks; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class DatabaseExtensions + { + public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext<DevHiveContext>(options => + { + // options.EnableSensitiveDataLogging(true); + options.UseNpgsql(configuration.GetConnectionString("DEV"), options => + { + options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); + }); + }); + + services.AddIdentity<User, Role>() + .AddRoles<Role>() + .AddEntityFrameworkStores<DevHiveContext>(); + + services.Configure<IdentityOptions>(options => + { + options.User.RequireUniqueEmail = true; + + options.Password.RequireDigit = true; + options.Password.RequiredLength = 5; + options.Password.RequiredUniqueChars = 0; + options.Password.RequireLowercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + + options.Lockout.AllowedForNewUsers = true; + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); + options.Lockout.MaxFailedAccessAttempts = 5; + }); + + services.AddAuthorization(options => + { + options.AddPolicy("User", options => + { + options.RequireAuthenticatedUser(); + options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + options.RequireRole("User"); + }); + + options.AddPolicy("Administrator", options => + { + options.RequireAuthenticatedUser(); + options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + options.RequireRole("Admin"); + }); + }); + } + + public static void UseDatabaseConfiguration(this IApplicationBuilder app) + { + app.UseHttpsRedirection(); + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + + using var serviceScope = app.ApplicationServices.CreateScope(); + using var dbContext = serviceScope.ServiceProvider.GetRequiredService<DevHiveContext>(); + + dbContext.Database.Migrate(); + + var roleManager = (RoleManager<Role>)serviceScope.ServiceProvider.GetService(typeof(RoleManager<Role>)); + + if (!dbContext.Roles.Any(x => x.Name == Role.DefaultRole)) + { + Role defaultRole = new() { Name = Role.DefaultRole }; + + roleManager.CreateAsync(defaultRole).Wait(); + } + + if (!dbContext.Roles.Any(x => x.Name == Role.AdminRole)) + { + Role adminRole = new() { Name = Role.AdminRole }; + + roleManager.CreateAsync(adminRole).Wait(); + } + + dbContext.SaveChanges(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs new file mode 100644 index 0000000..f49a335 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -0,0 +1,51 @@ +using System.Text; +using DevHive.Common.Jwt; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Data.Interfaces; +using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Services.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureDependencyInjection + { + public static void DependencyInjectionConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddTransient<ILanguageRepository, LanguageRepository>(); + services.AddTransient<IRoleRepository, RoleRepository>(); + services.AddTransient<ITechnologyRepository, TechnologyRepository>(); + services.AddTransient<IPostRepository, PostRepository>(); + services.AddTransient<ICommentRepository, CommentRepository>(); + services.AddTransient<IFeedRepository, FeedRepository>(); + services.AddTransient<IRatingRepository, RatingRepository>(); + services.AddTransient<IProfilePictureRepository, ProfilePictureRepository>(); + services.AddTransient<IUserRepository, UserRepository>(); + + services.AddTransient<ILanguageService, LanguageService>(); + services.AddTransient<IRoleService, RoleService>(); + services.AddTransient<ITechnologyService, TechnologyService>(); + services.AddTransient<IPostService, PostService>(); + services.AddTransient<ICommentService, CommentService>(); + services.AddTransient<IFeedService, FeedService>(); + services.AddTransient<IRatingService, RatingService>(); + services.AddTransient<IProfilePictureService, ProfilePictureService>(); + services.AddTransient<IUserService, UserService>(); + services.AddTransient<IFriendsService, FriendsService>(); + + services.AddTransient<ICloudService, CloudinaryService>(options => + new CloudinaryService( + cloudName: configuration.GetSection("Cloud").GetSection("cloudName").Value, + apiKey: configuration.GetSection("Cloud").GetSection("apiKey").Value, + apiSecret: configuration.GetSection("Cloud").GetSection("apiSecret").Value)); + + services.AddSingleton<IJwtService, JwtService>(options => + new JwtService( + signingKey: Encoding.ASCII.GetBytes(configuration.GetSection("Jwt").GetSection("signingKey").Value), + validationIssuer: configuration.GetSection("Jwt").GetSection("validationIssuer").Value, + audience: configuration.GetSection("Jwt").GetSection("audience").Value)); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs new file mode 100644 index 0000000..6885f84 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -0,0 +1,36 @@ +using DevHive.Web.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureExceptionHandlerMiddleware + { + public static void ConfigureExceptionHandler(this IServiceCollection services, IConfiguration configuration) + { + services.Configure<ApiBehaviorOptions>(o => + { + o.InvalidModelStateResponseFactory = actionContext => + { + var problemDetails = new ValidationProblemDetails(actionContext.ModelState) + { + Status = StatusCodes.Status422UnprocessableEntity + }; + + return new UnprocessableEntityObjectResult(problemDetails) + { + ContentTypes = { "application/problem+json" } + }; + }; + }); + } + + public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) + { + app.UseMiddleware<ExceptionMiddleware>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs new file mode 100644 index 0000000..18127bc --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs @@ -0,0 +1,46 @@ +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureJwt + { + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) + { + // Get key from appsettings.json + var signingKey = Encoding.ASCII.GetBytes(configuration + .GetSection("Jwt") + .GetSection("signingKey") + .Value); + + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + IssuerSigningKey = new SymmetricSecurityKey(signingKey), + ValidateIssuer = false, + ValidateAudience = false + }; + }); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs new file mode 100644 index 0000000..1b7182c --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs @@ -0,0 +1,65 @@ +using System.Linq; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using NSwag; +using NSwag.Generation.Processors.Security; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class SwaggerExtensions + { +#pragma warning disable S1075 + private const string LicenseName = "GPL-3.0 License"; + private const string LicenseUri = "https://github.com/Team-Kaleidoscope/DevHive/blob/main/LICENSE"; + private const string TermsOfServiceUri = "https://example.com/terms"; +#pragma warning restore S1075 + + public static void SwaggerConfiguration(this IServiceCollection services) + { + services.AddOpenApiDocument(c => + { + c.GenerateXmlObjects = true; + c.UseControllerSummaryAsTagDescription = true; + + c.AllowNullableBodyParameters = false; + c.Description = "DevHive Social Media's API Endpoints"; + + c.PostProcess = doc => + { + doc.Info.Version = "v0.1"; + doc.Info.Title = "API"; + doc.Info.Description = "DevHive Social Media's first official API release"; + doc.Info.TermsOfService = TermsOfServiceUri; + doc.Info.License = new() + { + Name = LicenseName, + Url = LicenseUri + }; + }; + + c.AddSecurity("Bearer", Enumerable.Empty<string>(), new() + { + Type = OpenApiSecuritySchemeType.ApiKey, + Name = "Authorization", + In = OpenApiSecurityApiKeyLocation.Header, + Description = "Type into the textbox: Bearer {your JWT token}." + }); + c.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("Bearer")); + }); + } + + public static void UseSwaggerConfiguration(this IApplicationBuilder app) + { + app.UseOpenApi(c => + { + c.DocumentName = "v0.1"; + }); + app.UseSwaggerUi3(c => + { + c.DocumentTitle = "DevHive API"; + c.EnableTryItOut = false; + c.DocExpansion = "list"; + }); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs new file mode 100644 index 0000000..b8d6829 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using DevHive.Services.Models.Comment; +using DevHive.Web.Models.Comment; + +namespace DevHive.Web.Configurations.Mapping +{ + public class CommentMappings : Profile + { + public CommentMappings() + { + CreateMap<CreateCommentWebModel, CreateCommentServiceModel>(); + CreateMap<UpdateCommentWebModel, UpdateCommentServiceModel>(); + + CreateMap<ReadCommentServiceModel, ReadCommentWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs new file mode 100644 index 0000000..0909f6d --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/FeedMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Services.Models; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; + +namespace DevHive.Web.Configurations.Mapping +{ + public class FeedMappings : Profile + { + public FeedMappings() + { + CreateMap<GetPageWebModel, GetPageServiceModel>() + .ForMember(dest => dest.FirstRequestIssued, src => src.MapFrom(p => p.FirstPageTimeIssued)); + + CreateMap<ReadPageServiceModel, ReadPageWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs new file mode 100644 index 0000000..eca0d1a --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DevHive.Web.Models.Language; +using DevHive.Services.Models.Language; + +namespace DevHive.Web.Configurations.Mapping +{ + public class LanguageMappings : Profile + { + public LanguageMappings() + { + CreateMap<CreateLanguageWebModel, CreateLanguageServiceModel>(); + CreateMap<ReadLanguageWebModel, ReadLanguageServiceModel>(); + CreateMap<UpdateLanguageWebModel, UpdateLanguageServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<LanguageWebModel, LanguageServiceModel>(); + + CreateMap<LanguageServiceModel, LanguageWebModel>(); + CreateMap<ReadLanguageServiceModel, ReadLanguageWebModel>(); + CreateMap<CreateLanguageServiceModel, CreateLanguageWebModel>(); + CreateMap<UpdateLanguageServiceModel, UpdateLanguageWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs new file mode 100644 index 0000000..a5b46ee --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using DevHive.Services.Models.Post; +using DevHive.Web.Models.Post; + +namespace DevHive.Web.Configurations.Mapping +{ + public class PostMappings : Profile + { + public PostMappings() + { + CreateMap<CreatePostWebModel, CreatePostServiceModel>(); + CreateMap<UpdatePostWebModel, UpdatePostServiceModel>(); + + CreateMap<ReadPostServiceModel, ReadPostWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs new file mode 100644 index 0000000..8c12a20 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/ProfilePictureMappings.cs @@ -0,0 +1,16 @@ +using System; +using AutoMapper; +using DevHive.Web.Models.ProfilePicture; +using DevHive.Services.Models.ProfilePicture; + +namespace DevHive.Web.Configurations.Mapping +{ + public class ProfilePictureMappings : Profile + { + public ProfilePictureMappings() + { + CreateMap<ProfilePictureWebModel, ProfilePictureServiceModel>() + .ForMember(dest => dest.ProfilePictureFormFile, src => src.MapFrom(p => p.Picture)); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs new file mode 100644 index 0000000..1d731d8 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Services.Models.Rating; +using DevHive.Web.Models.Rating; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RatingMappings : Profile + { + public RatingMappings() + { + CreateMap<CreateRatingWebModel, CreateRatingServiceModel>(); + + CreateMap<ReadRatingServiceModel, ReadRatingWebModel>(); + + CreateMap<UpdateRatingWebModel, UpdateRatingServiceModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs new file mode 100644 index 0000000..60f8503 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using DevHive.Web.Models.Role; +using DevHive.Services.Models.Role; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RoleMappings : Profile + { + public RoleMappings() + { + CreateMap<CreateRoleWebModel, CreateRoleServiceModel>(); + CreateMap<UpdateRoleWebModel, UpdateRoleServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<RoleWebModel, RoleServiceModel>(); + + CreateMap<CreateRoleServiceModel, CreateRoleWebModel>(); + CreateMap<UpdateRoleServiceModel, UpdateRoleWebModel>(); + CreateMap<RoleServiceModel, RoleWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs new file mode 100644 index 0000000..708b6ac --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DevHive.Web.Models.Technology; +using DevHive.Services.Models.Technology; + +namespace DevHive.Web.Configurations.Mapping +{ + public class TechnologyMappings : Profile + { + public TechnologyMappings() + { + CreateMap<CreateTechnologyWebModel, CreateTechnologyServiceModel>(); + CreateMap<ReadTechnologyWebModel, ReadTechnologyServiceModel>(); + CreateMap<UpdateTechnologyWebModel, UpdateTechnologyServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<TechnologyWebModel, TechnologyServiceModel>(); + + CreateMap<CreateTechnologyServiceModel, CreateTechnologyWebModel>(); + CreateMap<ReadTechnologyServiceModel, ReadTechnologyWebModel>(); + CreateMap<UpdateTechnologyServiceModel, UpdateTechnologyWebModel>(); + CreateMap<TechnologyServiceModel, TechnologyWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs new file mode 100644 index 0000000..dabec93 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -0,0 +1,30 @@ +using AutoMapper; +using DevHive.Services.Models.User; +using DevHive.Web.Models.User; +using DevHive.Common.Models.Identity; + +namespace DevHive.Web.Configurations.Mapping +{ + public class UserMappings : Profile + { + public UserMappings() + { + CreateMap<LoginWebModel, LoginServiceModel>(); + CreateMap<RegisterWebModel, RegisterServiceModel>(); + CreateMap<UserWebModel, UserServiceModel>(); + CreateMap<UpdateUserWebModel, UpdateUserServiceModel>(); + + CreateMap<UserServiceModel, UserWebModel>(); + + CreateMap<TokenModel, TokenWebModel>(); + + //Update + CreateMap<UpdateUserWebModel, UpdateUserServiceModel>(); + CreateMap<UsernameWebModel, FriendServiceModel>(); + CreateMap<UsernameWebModel, UpdateFriendServiceModel>(); + + CreateMap<UpdateUserServiceModel, UpdateUserWebModel>(); + CreateMap<FriendServiceModel, UsernameWebModel>(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..8fa3577 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,116 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the comments layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class CommentController + { + private readonly ICommentService _commentService; + private readonly IMapper _commentMapper; + private readonly IJwtService _jwtService; + + public CommentController(ICommentService commentService, IMapper commentMapper, IJwtService jwtService) + { + this._commentService = commentService; + this._commentMapper = commentMapper; + this._jwtService = jwtService; + } + + /// <summary> + /// Create a comment and attach it to a post + /// </summary> + /// <param name="userId">The useer's Id</param> + /// <param name="createCommentWebModel">The new comment's parametars</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>The comment's Id</returns> + [HttpPost] + public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + if (!await this._commentService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateCommentServiceModel createCommentServiceModel = + this._commentMapper.Map<CreateCommentServiceModel>(createCommentWebModel); + createCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.AddComment(createCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create comment!") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Query comment's data by it's Id + /// </summary> + /// <param name="commentId">The comment's Id</param> + /// <returns>Full data model of the comment</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetCommentById(Guid commentId) + { + ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(commentId); + ReadCommentWebModel readCommentWebModel = this._commentMapper.Map<ReadCommentWebModel>(readCommentServiceModel); + + return new OkObjectResult(readCommentWebModel); + } + + /// <summary> + /// Update comment's parametars. Comment creator only! + /// </summary> + /// <param name="userId">The comment creator's Id</param> + /// <param name="updateCommentWebModel">New comment's parametars</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpPut] + public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + UpdateCommentServiceModel updateCommentServiceModel = + this._commentMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.UpdateComment(updateCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Delete a comment. Comment creator only! + /// </summary> + /// <param name="commentId">Comment's Id</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpDelete] + public async Task<IActionResult> DeleteComment(Guid commentId, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(commentId, authorization)) + return new UnauthorizedResult(); + + return await this._commentService.DeleteComment(commentId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); + } + } +} + diff --git a/src/Web/DevHive.Web/Controllers/FeedController.cs b/src/Web/DevHive.Web/Controllers/FeedController.cs new file mode 100644 index 0000000..37532a9 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/FeedController.cs @@ -0,0 +1,69 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the feed layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class FeedController + { + private readonly IFeedService _feedService; + private readonly IMapper _mapper; + + public FeedController(IFeedService feedService, IMapper mapper) + { + this._feedService = feedService; + this._mapper = mapper; + } + + /// <summary> + /// Query posts for user's feed + /// </summary> + /// <param name="userId">The user's Id, whose feed is begin queried</param> + /// <param name="getPageWebModel">Page parametars</param> + /// <returns>A page of the feed</returns> + [HttpPost] + [Route("GetPosts")] + public async Task<IActionResult> GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map<GetPageServiceModel>(getPageWebModel); + getPageServiceModel.UserId = userId; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map<ReadPageWebModel>(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + + /// <summary> + /// Query a user profile's posts + /// </summary> + /// <param name="username">The user's username, whose posts are being queried</param> + /// <param name="getPageWebModel">Page parametars</param> + /// <returns>A page of the user's posts</returns> + [HttpPost] + [Route("GetUserPosts")] + [AllowAnonymous] + public async Task<IActionResult> GetUserPosts(string username, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map<GetPageServiceModel>(getPageWebModel); + getPageServiceModel.Username = username; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetUserPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map<ReadPageWebModel>(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/FriendsController.cs b/src/Web/DevHive.Web/Controllers/FriendsController.cs new file mode 100644 index 0000000..318ae64 --- /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, string friendUsername, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return (await this._friendsService.AddFriend(userId, friendUsername)) ? + new OkResult() : + new BadRequestResult(); + } + + [HttpDelete] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> RemoveFriend(Guid userId, string friendUsername, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return (await this._friendsService.RemoveFriend(userId, friendUsername)) ? + new OkResult() : + new BadRequestResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/LanguageController.cs b/src/Web/DevHive.Web/Controllers/LanguageController.cs new file mode 100644 index 0000000..665fb66 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/LanguageController.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Language; +using DevHive.Web.Models.Language; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the language layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class LanguageController + { + private readonly ILanguageService _languageService; + private readonly IMapper _languageMapper; + + public LanguageController(ILanguageService languageService, IMapper mapper) + { + this._languageService = languageService; + this._languageMapper = mapper; + } + + /// <summary> + /// Create a new language, so users can have a choice. Admin only! + /// </summary> + /// <param name="createLanguageWebModel">The new language's parametars</param> + /// <returns>The new language's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateLanguageWebModel createLanguageWebModel) + { + CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map<CreateLanguageServiceModel>(createLanguageWebModel); + + Guid id = await this._languageService.CreateLanguage(languageServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create language {createLanguageWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Query full language data by Id + /// </summary> + /// <param name="id">The language's Id</param> + /// <returns>Full language data</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + ReadLanguageWebModel languageWebModel = this._languageMapper.Map<ReadLanguageWebModel>(languageServiceModel); + + return new OkObjectResult(languageWebModel); + } + + /// <summary> + /// Query all languages in the database + /// </summary> + /// <returns>All languages in the database</returns> + [HttpGet] + [Route("GetLanguages")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingLanguages() + { + HashSet<ReadLanguageServiceModel> languageServiceModels = this._languageService.GetLanguages(); + HashSet<ReadLanguageWebModel> languageWebModels = this._languageMapper.Map<HashSet<ReadLanguageWebModel>>(languageServiceModels); + + return new OkObjectResult(languageWebModels); + } + + /// <summary> + /// Alter language's properties. Admin only! + /// </summary> + /// <param name="id">The language's Id</param> + /// <param name="updateModel">The langauge's new parametars</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) + { + UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map<UpdateLanguageServiceModel>(updateModel); + updatelanguageServiceModel.Id = id; + + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Language"); + + return new OkResult(); + } + + /// <summary> + /// Delete a language. Admin only! + /// </summary> + /// <param name="langaugeId">The language's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid langaugeId) + { + bool result = await this._languageService.DeleteLanguage(langaugeId); + + if (!result) + return new BadRequestObjectResult("Could not delete Language"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/PostController.cs b/src/Web/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..44b291d --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,123 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post; +using DevHive.Services.Models.Post; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the post layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class PostController + { + private readonly IPostService _postService; + private readonly IMapper _postMapper; + private readonly IJwtService _jwtService; + + public PostController(IPostService postService, IMapper postMapper, IJwtService jwtService) + { + this._postService = postService; + this._postMapper = postMapper; + this._jwtService = jwtService; + } + + #region Create + /// <summary> + /// Create a new post + /// </summary> + /// <param name="userId">The user's Id</param> + /// <param name="createPostWebModel">The new post's data</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>New post's Id</returns> + [HttpPost] + public async Task<IActionResult> Create(Guid userId, [FromForm] CreatePostWebModel createPostWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + CreatePostServiceModel createPostServiceModel = + this._postMapper.Map<CreatePostServiceModel>(createPostWebModel); + createPostServiceModel.CreatorId = userId; + + Guid id = await this._postService.CreatePost(createPostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Read + /// <summary> + /// Query full post's data by it's Id + /// </summary> + /// <param name="id">The post's Id</param> + /// <returns>Full data model of the post</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); + ReadPostWebModel postWebModel = this._postMapper.Map<ReadPostWebModel>(postServiceModel); + + return new OkObjectResult(postWebModel); + } + #endregion + + #region Update + /// <summary> + /// Update post's data. Creator only! + /// </summary> + /// <param name="userId">The post creator's Id</param> + /// <param name="updatePostWebModel">The new params of the post</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>The post's Id</returns> + [HttpPut] + public async Task<IActionResult> Update(Guid userId, [FromForm] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) + return new UnauthorizedResult(); + + UpdatePostServiceModel updatePostServiceModel = + this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel); + updatePostServiceModel.CreatorId = userId; + + Guid id = await this._postService.UpdatePost(updatePostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not update post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Delete + /// <summary> + /// Delete a post. Creator only! + /// </summary> + /// <param name="postId">Post's Id</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpDelete] + public async Task<IActionResult> Delete(Guid postId, [FromHeader] string authorization) + { + if (!await this._postService.ValidateJwtForPost(postId, authorization)) + return new UnauthorizedResult(); + + return await this._postService.DeletePost(postId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Post"); + } + #endregion + } +} diff --git a/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs new file mode 100644 index 0000000..8474df5 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs @@ -0,0 +1,66 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.ProfilePicture; +using DevHive.Common.Jwt.Interfaces; +using AutoMapper; +using DevHive.Web.Models.ProfilePicture; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the profile picture layer + /// </summary> + [ApiController] + [Route("api/[controller]")] + public class ProfilePictureController + { + private readonly IProfilePictureService _profilePictureService; + private readonly IJwtService _jwtService; + private readonly IMapper _profilePictureMapper; + + public ProfilePictureController(IProfilePictureService profilePictureService, IJwtService jwtService, IMapper profilePictureMapper) + { + this._profilePictureService = profilePictureService; + this._jwtService = jwtService; + this._profilePictureMapper = profilePictureMapper; + } + + /// <summary> + /// Get the URL of user's profile picture + /// </summary> + /// <param name="profilePictureId">The profile picture's Id</param> + /// <param name="authorization">JWT Bearer Token</param> + /// <returns>The URL of the profile picture</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> ReadProfilePicture(Guid profilePictureId) + { + string profilePicURL = await this._profilePictureService.GetProfilePictureById(profilePictureId); + return new OkObjectResult(new { ProfilePictureURL = profilePicURL} ); + } + + /// <summary> + /// Alter the profile picture of a user + /// </summary> + /// <param name="userId">The user's Id</param> + /// <param name="profilePictureWebModel">The new profile picture</param> + /// <param name="authorization">JWT Bearer Token</param> + /// <returns>The URL of the new profile picture</returns> + [HttpPut] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] ProfilePictureWebModel profilePictureWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + ProfilePictureServiceModel profilePictureServiceModel = this._profilePictureMapper.Map<ProfilePictureServiceModel>(profilePictureWebModel); + profilePictureServiceModel.UserId = userId; + + string url = await this._profilePictureService.UpdateProfilePicture(profilePictureServiceModel); + return new OkObjectResult(new { URL = url }); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs new file mode 100644 index 0000000..7d21795 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Rating; +using DevHive.Web.Models.Rating; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Authorize(Roles = "Admin,User")] + [Route("api/[controller]")] + public class RatingController + { + private readonly IRatingService _rateService; + private readonly IMapper _mapper; + private readonly IJwtService _jwtService; + + public RatingController(IRatingService rateService, IMapper mapper, IJwtService jwtService) + { + this._rateService = rateService; + this._mapper = mapper; + this._jwtService = jwtService; + } + + [HttpPost] + public async Task<IActionResult> RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + CreateRatingServiceModel ratePostServiceModel = this._mapper.Map<CreateRatingServiceModel>(createRatingWebModel); + ratePostServiceModel.UserId = userId; + + Guid id = await this._rateService.RatePost(ratePostServiceModel); + + if (Guid.Empty == id) + return new BadRequestResult(); + + return new OkObjectResult(new { Id = id }); + } + + [HttpGet] + public async Task<IActionResult> GetRatingById(Guid id) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingById(id); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + + [HttpGet] + [Route("GetByUserAndPost")] + public async Task<IActionResult> GetRatingByUserAndPost(Guid userId, Guid postId) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingByPostAndUser(userId, postId); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + + [HttpPut] + public async Task<IActionResult> UpdateRating(Guid userId, Guid postId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + UpdateRatingServiceModel updateRatingServiceModel = + this._mapper.Map<UpdateRatingServiceModel>(updateRatingWebModel); + updateRatingServiceModel.UserId = userId; + updateRatingServiceModel.PostId = postId; + + ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); + + if (readRatingServiceModel == null) + return new BadRequestResult(); + else + { + ReadRatingWebModel readRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + return new OkObjectResult(readRatingWebModel); + } + } + + [HttpDelete] + public async Task<IActionResult> DeleteRating(Guid userId, Guid ratingId, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return await this._rateService.DeleteRating(ratingId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Rating"); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/RoleController.cs b/src/Web/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..ebb305e --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RoleController.cs @@ -0,0 +1,101 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using DevHive.Web.Models.Role; +using AutoMapper; +using System; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Role; +using Microsoft.AspNetCore.Authorization; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the roles layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class RoleController + { + private readonly IRoleService _roleService; + private readonly IMapper _roleMapper; + + public RoleController(IRoleService roleService, IMapper mapper) + { + this._roleService = roleService; + this._roleMapper = mapper; + } + + /// <summary> + /// Create a new role for the roles hierarchy. Admin only! + /// </summary> + /// <param name="createRoleWebModel">The new role's parametars</param> + /// <returns>The new role's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel) + { + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map<CreateRoleServiceModel>(createRoleWebModel); + + Guid id = await this._roleService.CreateRole(roleServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Get a role's full data, querying it by it's Id + /// </summary> + /// <param name="id">The role's Id</param> + /// <returns>Full info of the role</returns> + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> GetById(Guid id) + { + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map<RoleWebModel>(roleServiceModel); + + return new OkObjectResult(roleWebModel); + } + + /// <summary> + /// Update a role's parametars. Admin only! + /// </summary> + /// <param name="id">The role's Id</param> + /// <param name="updateRoleWebModel">The new parametrats for that role</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + { + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map<UpdateRoleServiceModel>(updateRoleWebModel); + updateRoleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); + } + + /// <summary> + /// Delete a role. Admin only! + /// </summary> + /// <param name="id">The role's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could not delete role!"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/TechnologyController.cs b/src/Web/DevHive.Web/Controllers/TechnologyController.cs new file mode 100644 index 0000000..ecf2bd7 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/TechnologyController.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Technology; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the technology layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class TechnologyController + { + private readonly ITechnologyService _technologyService; + private readonly IMapper _technologyMapper; + + public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper) + { + this._technologyService = technologyService; + this._technologyMapper = technologyMapper; + } + + /// <summary> + /// Create a new technology, so users can have a choice. Admin only! + /// </summary> + /// <param name="createTechnologyWebModel">Data for the new technology</param> + /// <returns>The new technology's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel) + { + CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map<CreateTechnologyServiceModel>(createTechnologyWebModel); + + Guid id = await this._technologyService.CreateTechnology(technologyServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Get technology's data by it's Id + /// </summary> + /// <param name="id">The technology's Id</param> + /// <returns>The technology's full data</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadTechnologyServiceModel readTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); + ReadTechnologyWebModel readTechnologyWebModel = this._technologyMapper.Map<ReadTechnologyWebModel>(readTechnologyServiceModel); + + return new OkObjectResult(readTechnologyWebModel); + } + + /// <summary> + /// Get all technologies from our database + /// </summary> + /// <returns>All technologies</returns> + [HttpGet] + [Route("GetTechnologies")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingTechnologies() + { + HashSet<ReadTechnologyServiceModel> technologyServiceModels = this._technologyService.GetTechnologies(); + HashSet<ReadTechnologyWebModel> languageWebModels = this._technologyMapper.Map<HashSet<ReadTechnologyWebModel>>(technologyServiceModels); + + return new OkObjectResult(languageWebModels); + } + + /// <summary> + /// Alter a technology's parameters. Admin only! + /// </summary> + /// <param name="id">Technology's Id</param> + /// <param name="updateModel">The new parametars</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) + { + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map<UpdateTechnologyServiceModel>(updateModel); + updateTechnologyServiceModel.Id = id; + + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Technology"); + + return new OkResult(); + } + + /// <summary> + /// Delete a etchnology from the database. Admin only! + /// </summary> + /// <param name="id">The technology's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._technologyService.DeleteTechnology(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Technology"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs new file mode 100644 index 0000000..4d01447 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/UserController.cs @@ -0,0 +1,172 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Models.User; +using DevHive.Web.Models.User; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using DevHive.Common.Models.Identity; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; +using NSwag.Annotations; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for integration with the User + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [OpenApiController("User Controller")] + public class UserController : ControllerBase + { + private readonly IUserService _userService; + private readonly IMapper _userMapper; + private readonly IJwtService _jwtService; + + public UserController(IUserService userService, IMapper mapper, IJwtService jwtService) + { + this._userService = userService; + this._userMapper = mapper; + this._jwtService = jwtService; + } + + #region Authentication + /// <summary> + /// Login endpoint for the DevHive Social Platform + /// </summary> + /// <param name="loginModel">Login model with username and password</param> + /// <returns>A JWT Token for further validation</returns> + [HttpPost] + [AllowAnonymous] + [Route("Login")] + [OpenApiTags("Authorization")] + public async Task<IActionResult> Login([FromBody] LoginWebModel loginModel) + { + LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel); + + TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel); + + return new OkObjectResult(tokenWebModel); + } + + /// <summary> + /// Register a new User in the DevHive Social Platform + /// </summary> + /// <param name="registerModel">Register model with the new data to provide</param> + /// <returns>A JWT Token for further validation</returns> + [HttpPost] + [AllowAnonymous] + [Route("Register")] + [OpenApiTag("Authorization")] + public async Task<IActionResult> Register([FromBody] RegisterWebModel registerModel) + { + RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel); + + TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel); + + return new CreatedResult("Register", tokenWebModel); + } + #endregion + + #region Read + /// <summary> + /// Get a User's information using the Guid + /// </summary> + /// <param name="id">User's Id</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>A full User's read model</returns> + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> GetById(Guid id, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + UserServiceModel userServiceModel = await this._userService.GetUserById(id); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new OkObjectResult(userWebModel); + } + + /// <summary> + /// Get a User's profile using his username. Does NOT require authorization + /// </summary> + /// <param name="username">User's username</param> + /// <returns>A trimmed version of the full User's read model</returns> + [HttpGet] + [Route("GetUser")] + [AllowAnonymous] + public async Task<IActionResult> GetUser(string username) + { + UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); + UserWebModel friend = this._userMapper.Map<UserWebModel>(friendServiceModel); + + return new OkObjectResult(friend); + } + #endregion + + #region Update + /// <summary> + /// Full update on User's data. A PUSTINQK can only edit his account + /// </summary> + /// <param name="id">The User's Id</param> + /// <param name="updateUserWebModel">A full User update model</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>A full User's read model</returns> + [HttpPut] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateUserWebModel); + updateUserServiceModel.Id = id; + + UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new AcceptedResult("UpdateUser", userWebModel); + } + #endregion + + #region Delete + /// <summary> + /// Delete a User with his Id. A PUSTINQK can only delete his account. An Admin can delete all accounts + /// </summary> + /// <param name="id">The User's Id</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>Ok, BadRequest or Unauthorized</returns> + [HttpDelete] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + bool result = await this._userService.DeleteUser(id); + if (!result) + return new BadRequestObjectResult("Could not delete User"); + + return new OkResult(); + } + #endregion + + /// <summary> + /// We don't talk about that, NIGGA! + /// </summary> + /// <param name="userId"></param> + /// <returns></returns> + [HttpPost] + [OpenApiIgnore] + [Authorize(Roles = "User,Admin")] + [Route("SuperSecretPromotionToAdmin")] + public async Task<IActionResult> SuperSecretPromotionToAdmin(Guid userId) + { + return new OkObjectResult(await this._userService.SuperSecretPromotionToAdmin(userId)); + } + } +} diff --git a/src/Web/DevHive.Web/DevHive.Web.csproj b/src/Web/DevHive.Web/DevHive.Web.csproj new file mode 100644 index 0000000..5b3a920 --- /dev/null +++ b/src/Web/DevHive.Web/DevHive.Web.csproj @@ -0,0 +1,43 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + <PropertyGroup> + <TargetFramework>net5.0</TargetFramework> + </PropertyGroup> + <PropertyGroup> + <EnableNETAnalyzers>true</EnableNETAnalyzers> + <AnalysisLevel>latest</AnalysisLevel> + <GenerateDocumentationFile>true</GenerateDocumentationFile> + <AllowUntrustedCertificate>true</AllowUntrustedCertificate> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.4" NoWarn="NU1605"/> + <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" NoWarn="NU1605"/> + <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> + <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2"/> + <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1"/> + <PackageReference Include="AutoMapper" Version="10.1.1"/> + <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/> + <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4"/> + <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/> + <PackageReference Include="NSwag.AspNetCore" Version="13.10.8"/> + <PackageReference Include="NSwag.Generation.AspNetCore" Version="13.10.8"/> + <PackageReference Include="NSwag.Annotations" Version="13.10.8"/> + <PackageReference Include="NSwag.Core" Version="13.10.8"/> + <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.1.1"/> + <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.1.1"/> + <PackageReference Include="NSwag.SwaggerGeneration.WebApi" Version="12.3.1"/> + <PackageReference Include="Serilog.AspNetCore" Version="4.1.0"/> + <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0"/> + <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3"/> + <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0"/> + <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1"/> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\DevHive.Web.Models\DevHive.Web.Models.csproj"/> + <ProjectReference Include="..\..\Services\DevHive.Services\DevHive.Services.csproj"/> + <ProjectReference Include="..\..\Common\DevHive.Common.Models\DevHive.Common.Models.csproj"/> + <ProjectReference Include="..\..\Common\DevHive.Common\DevHive.Common.csproj"/> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..680d824 --- /dev/null +++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -0,0 +1,44 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +namespace DevHive.Web.Middleware +{ + public class ExceptionMiddleware + { + private readonly RequestDelegate _next; + + public ExceptionMiddleware(RequestDelegate next) + { + this._next = next; + } + + public async Task InvokeAsync(HttpContext httpContext) + { + try + { + await this._next(httpContext); + } + catch (Exception ex) + { + await HandleExceptionAsync(httpContext, ex); + } + } + + private static Task HandleExceptionAsync(HttpContext context, Exception exception) + { + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + + var problems = new + { + errors = new { Exception = new String[] { exception.Message } } + }; + + string message = JsonConvert.SerializeObject(problems); + return context.Response.WriteAsync(message); + } + } +} diff --git a/src/Web/DevHive.Web/Program.cs b/src/Web/DevHive.Web/Program.cs new file mode 100644 index 0000000..e7c47a9 --- /dev/null +++ b/src/Web/DevHive.Web/Program.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; +using Serilog; + +namespace DevHive.Web +{ + #pragma warning disable IDE0055, S1118 + + public class Program + { + private const int HTTP_PORT = 5000; + + public static void Main(string[] args) + { + var config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); + + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(config) + .CreateLogger(); + + try + { + Log.Information("Application Starting Up"); + CreateHostBuilder(args).Build().Run(); + } + catch (Exception ex) + { + Log.Fatal(ex, "The application failed to start correctly."); + } + finally + { + Log.CloseAndFlush(); + } + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseSerilog() + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTP_PORT)); + webBuilder.UseStartup<Startup>(); + }); + } +} diff --git a/src/Web/DevHive.Web/Properties/launchSettings.json b/src/Web/DevHive.Web/Properties/launchSettings.json new file mode 100644 index 0000000..2b65d0b --- /dev/null +++ b/src/Web/DevHive.Web/Properties/launchSettings.json @@ -0,0 +1,36 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:1955", + "sslPort": 44326 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web Production": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Production" + } + } + } +} diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs new file mode 100644 index 0000000..49fa408 --- /dev/null +++ b/src/Web/DevHive.Web/Startup.cs @@ -0,0 +1,75 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DevHive.Web.Configurations.Extensions; +using Newtonsoft.Json; +using Serilog; + +namespace DevHive.Web +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddCors(); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + }); + + services.DependencyInjectionConfiguration(this.Configuration); + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + services.AutoMapperConfiguration(); + services.ConfigureExceptionHandler(this.Configuration); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed(origin => true) // allow any origin + .AllowCredentials()); // allow credentials + + if (env.IsDevelopment()) + { + app.UseExceptionHandlerMiddlewareConfiguration(); + // app.UseDeveloperExceptionPage(); + } + else + { + app.UseHsts(); + app.UseExceptionHandlerMiddlewareConfiguration(); + } + + app.UseSwaggerConfiguration(); + app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); + + app.UseSerilogRequestLogging(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } +} diff --git a/src/Web/DevHive.Web/appsettings.json b/src/Web/DevHive.Web/appsettings.json new file mode 100644 index 0000000..84d534d --- /dev/null +++ b/src/Web/DevHive.Web/appsettings.json @@ -0,0 +1,50 @@ +{ + "Jwt": { + "signingKey": "", + "validationIssuer": "", + "audience": "" + }, + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=;" + }, + "Cloud": { + "cloudName": "devhive", + "apiKey": "488664116365813", + "apiSecret": "" + }, + "Serilog": { + "Using": [], + "LevelSwitches": { + "$consoleSwitch": "Verbose", + "$fileSwitch": "Error" + }, + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithProcessId", + "WithThreadId" + ], + "WriteTo": [ + { + "Name": "Console", + "Args": { + "levelSwitch": "$consoleSwitch" + } + }, + { + "Name": "File", + "Args": { + "path": "./Logs/errors.log", + "levelSwitch": "$fileSwitch" + } + } + ] + } +} |
