Skip to content
Snippets Groups Projects
Commit 0e77295d authored by Lucca Santangelo's avatar Lucca Santangelo
Browse files

merge from master

parents 811d1e3a 518b6541
No related branches found
No related tags found
1 merge request!6remove https and docker
Showing
with 591 additions and 0 deletions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ForumController : ControllerBase
{
private readonly IForumService _forumService;
public ForumController(IForumService forumService)
{
_forumService = forumService;
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetForums/{courseId}")]
public async Task<IActionResult> GetForums(int courseId)
{
var result = await _forumService.GetForums(courseId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(UserTypes.Professor)]
[HttpPost("Create")]
public async Task<IActionResult> Create(ForumCreateDto newForum)
{
var result = await _forumService.Create(newForum);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
[Authorize(UserTypes.Professor)]
[HttpDelete("Delete/{forumId}")]
public async Task<IActionResult> Delete(int forumId)
{
var result = await _forumService.Delete(forumId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
[Authorize(Roles = UserTypes.Student)]
[HttpGet("Subscribe/{forumId}")]
public async Task<IActionResult> Subscribe(int forumId)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
var result = await _forumService.Subscribe(forumId, userId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MessageController : ControllerBase
{
private readonly IMessageService _messageService;
public MessageController(IMessageService messageService)
{
_messageService = messageService;
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetMessages/{receiverId}")]
public async Task<ActionResult> GetMessages(int receiverId)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
var result = await _messageService.GetMessages(userId, receiverId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpPost("Send")]
public async Task<ActionResult> Send(MessageCreateDto newMessage)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
newMessage.SenderId = userId;
var result = await _messageService.Send(newMessage);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PostController : ControllerBase
{
private readonly IPostService _postService;
private readonly IForumService _forumService;
private readonly IEmailService _emailService;
public PostController(IPostService postService, IForumService forumService,
IEmailService emailService)
{
_postService = postService;
_forumService = forumService;
_emailService = emailService;
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetPosts/{forumId}")]
public async Task<IActionResult> GetPosts(int forumId)
{
var result = await _postService.GetPosts(forumId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpPost("Create")]
public async Task<IActionResult> Create(PostCreateDto newPost)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
newPost.UserId = userId;
var result = await _postService.Create(newPost);
if (result.HasError)
{
return BadRequest(result.Message);
}
var userEmails = await _forumService.GetSubscribedUsers(newPost.ForumId);
await _emailService.NotifyNewPostOrMessage(newPost, userEmails.Data);
return Ok();
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpDelete("Delete/{postId}")]
public async Task<IActionResult> Delete(int postId)
{
var result = await _postService.Delete(postId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PostMessageController : ControllerBase
{
private readonly IPostMessageService _postMessageService;
public PostMessageController(IPostMessageService postMessageService)
{
_postMessageService = postMessageService;
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetPostMessages/{postId}")]
public async Task<IActionResult> GetPostMessagesPosts(int postId)
{
var result = await _postMessageService.GetPostMessages(postId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpPost("Create")]
public async Task<IActionResult> Create(PostMessageCreateDto newPostMessage)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
newPostMessage.UserId = userId;
var result = await _postMessageService.Create(newPostMessage);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpDelete("Delete/{postMessageId}")]
public async Task<IActionResult> Delete(int postMessageId)
{
var result = await _postMessageService.Delete(postMessageId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
}
}
...@@ -80,5 +80,20 @@ namespace Tsi1.Api.Controllers ...@@ -80,5 +80,20 @@ namespace Tsi1.Api.Controllers
return Ok(); return Ok();
} }
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetAll")]
public async Task<IActionResult> GetAll()
{
var result = await _userService.GetAll();
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
} }
} }
...@@ -3,6 +3,7 @@ using System.Collections.Generic; ...@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
...@@ -13,12 +14,15 @@ using Microsoft.Extensions.Configuration; ...@@ -13,12 +14,15 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Tsi1.Api.Infrastructure; using Tsi1.Api.Infrastructure;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces; using Tsi1.BusinessLayer.Interfaces;
using Tsi1.BusinessLayer.Services; using Tsi1.BusinessLayer.Services;
using Tsi1.DataLayer; using Tsi1.DataLayer;
using Tsi1.DataLayer.MongoDbConfiguration;
namespace Tsi1.Api namespace Tsi1.Api
{ {
...@@ -37,9 +41,24 @@ namespace Tsi1.Api ...@@ -37,9 +41,24 @@ namespace Tsi1.Api
services.AddControllers(); services.AddControllers();
services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql"))); services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql")));
services.Configure<Tsi1DatabaseSettings>(
Configuration.GetSection(nameof(Tsi1DatabaseSettings)));
services.AddSingleton<ITsi1DatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<Tsi1DatabaseSettings>>().Value);
services.AddSingleton<IMessageService, MessageService>();
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserTypeService, UserTypeService>(); services.AddScoped<IUserTypeService, UserTypeService>();
services.AddScoped<ICourseService, CourseService>(); services.AddScoped<ICourseService, CourseService>();
services.AddScoped<IForumService, ForumService>();
services.AddScoped<IPostService, PostService>();
services.AddScoped<IPostMessageService, PostMessageService>();
services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
services.AddScoped<IEmailService, EmailService>();
services.AddCors(); services.AddCors();
...@@ -94,6 +113,14 @@ namespace Tsi1.Api ...@@ -94,6 +113,14 @@ namespace Tsi1.Api
{securityScheme, new string[] { }} {securityScheme, new string[] { }}
}); });
}); });
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc => {
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
"ConnectionStrings": { "ConnectionStrings": {
"PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111" "PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111"
}, },
"Tsi1DatabaseSettings": {
"Tsi1CollectionName": "Messages",
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Tsi1Db"
},
"jwtTokenConfig": { "jwtTokenConfig": {
"secret": "1234567890123456789", "secret": "1234567890123456789",
"issuer": "https://localhost:44363", "issuer": "https://localhost:44363",
...@@ -9,6 +14,13 @@ ...@@ -9,6 +14,13 @@
"accessTokenExpiration": 20, "accessTokenExpiration": 20,
"refreshTokenExpiration": 60 "refreshTokenExpiration": 60
}, },
"MailSettings": {
"Mail": "tsi1.grupo2.2020@gmail.com",
"DisplayName": "tsi1 grupo 2 2020",
"Password": "VamoTSI.2020",
"Host": "smtp.gmail.com",
"Port": 587
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
......
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class ForumCreateDto
{
public string Name { get; set; }
public int CourseId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class ForumPreviewDto
{
public int Id { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class MessageCreateDto
{
[JsonIgnore]
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public string Content { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class MessagePreviewDto
{
public string Id { get; set; }
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public string Content { get; set; }
public string Date { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class PostCreateDto
{
public string Title { get; set; }
public int ForumId { get; set; }
[JsonIgnore]
public int UserId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class PostMessageCreateDto
{
public string Content { get; set; }
[JsonIgnore]
public int UserId { get; set; }
public int PostId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class PostMessagePreviewDto
{
public int Id { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
public int UserId { get; set; }
public int PostId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class PostPreviewDto
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public int ForumId { get; set; }
public int UserId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class UserPreviewDto
{
public int Id { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
...@@ -9,5 +9,13 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -9,5 +9,13 @@ namespace Tsi1.BusinessLayer.Helpers
public const string UserDoesNotExist = "El usuario '{0}' no existe"; public const string UserDoesNotExist = "El usuario '{0}' no existe";
public const string IncorrectPassword = "Contraseña incorrecta"; public const string IncorrectPassword = "Contraseña incorrecta";
public const string UserTypeDoesNotExist = "El tipo de usuario con id '{0}' no existe"; public const string UserTypeDoesNotExist = "El tipo de usuario con id '{0}' no existe";
public const string ForumDoesNotExist = "El foro con id '{0}' no existe";
public const string PostDoesNotExist = "El post con id '{0}' no existe";
public const string PostMessageDoesNotExist = "El mensage con id '{0}' no existe";
public const string CannotConnectToSmtpServer = "No se pudo conectar al servidor SMTP";
public const string CannotAuthenticateToSmtpServer = "No se pudo autenticar en el servidor SMTP";
public const string CannotSendEmail = "No se pudo mandar el mail con asunto {0}";
} }
} }
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Helpers
{
public class MailSettings
{
public string Mail { get; set; }
public string DisplayName { get; set; }
public string Password { get; set; }
public string Host { get; set; }
public int Port { get; set; }
}
}
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Helpers
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Forum, ForumCreateDto>();
CreateMap<Forum, ForumPreviewDto>();
CreateMap<Post, PostCreateDto>();
CreateMap<Post, PostPreviewDto>();
CreateMap<PostMessage, PostMessageCreateDto>();
CreateMap<PostMessage, PostMessagePreviewDto>();
CreateMap<Message, MessagePreviewDto>();
CreateMap<Message, MessageCreateDto>();
CreateMap<User, UserPreviewDto>();
CreateMap<User, UserRegisterDto>();
CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>();
CreateMap<PostCreateDto, Post>();
CreateMap<PostPreviewDto, Post>();
CreateMap<PostMessageCreateDto, PostMessage>();
CreateMap<PostMessagePreviewDto, PostMessage>();
CreateMap<MessagePreviewDto, Message>();
CreateMap<MessageCreateDto, Message>();
CreateMap<UserPreviewDto, User>();
CreateMap<UserRegisterDto, User>();
}
}
}
using MimeKit;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Interfaces
{
public interface IEmailService
{
Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message);
Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment