Skip to content
Snippets Groups Projects
Commit 171024d9 authored by Lucca Santangelo Dodera's avatar Lucca Santangelo Dodera
Browse files

Merge branch 'forum-post-postmessage' into 'master'

Forum post postmessage

See merge request !2
parents 62fb48a0 d43f0ca9
No related branches found
No related tags found
1 merge request!2Forum post postmessage
Showing
with 657 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(Roles = UserTypes.Student + ", " + 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(Roles = UserTypes.Student + ", " + 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();
}
}
}
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;
public PostController(IPostService postService)
{
_postService = postService;
}
[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);
}
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();
}
}
}
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
......@@ -16,6 +17,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Tsi1.Api.Infrastructure;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.BusinessLayer.Services;
using Tsi1.DataLayer;
......@@ -40,6 +42,9 @@ namespace Tsi1.Api
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserTypeService, UserTypeService>();
services.AddScoped<ICourseService, CourseService>();
services.AddScoped<IForumService, ForumService>();
services.AddScoped<IPostService, PostService>();
services.AddScoped<IPostMessageService, PostMessageService>();
services.AddCors();
......@@ -94,6 +99,14 @@ namespace Tsi1.Api
{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.
......
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 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; }
}
}
......@@ -9,5 +9,9 @@ namespace Tsi1.BusinessLayer.Helpers
public const string UserDoesNotExist = "El usuario '{0}' no existe";
public const string IncorrectPassword = "Contraseña incorrecta";
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";
}
}
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<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>();
CreateMap<PostCreateDto, Post>();
CreateMap<PostPreviewDto, Post>();
CreateMap<PostMessageCreateDto, PostMessage>();
CreateMap<PostMessagePreviewDto, PostMessage>();
}
}
}
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 IForumService
{
Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int courseId);
Task<ServiceResult<Forum>> Create(ForumCreateDto newForum);
Task<ServiceResult<Forum>> Delete(int forumId);
}
}
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 IPostMessageService
{
Task<ServiceResult<List<PostMessagePreviewDto>>> GetPostMessages(int postId);
Task<ServiceResult<PostMessage>> Create(PostMessageCreateDto newPostMessage);
Task<ServiceResult<PostMessage>> Delete(int postMessageId);
}
}
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 IPostService
{
Task<ServiceResult<List<PostPreviewDto>>> GetPosts(int forumId);
Task<ServiceResult<Post>> Create(PostCreateDto newPost);
Task<ServiceResult<Post>> Delete(int postId);
}
}
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class ForumService : IForumService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public ForumService(Tsi1Context context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResult<Forum>> Create(ForumCreateDto newForum)
{
var result = new ServiceResult<Forum>();
var forum = _mapper.Map<Forum>(newForum);
_context.Forums.Add(forum);
await _context.SaveChangesAsync();
result.Data = forum;
return result;
}
public async Task<ServiceResult<Forum>> Delete(int forumId)
{
var result = new ServiceResult<Forum>();
var forum = await _context.Forums.FirstOrDefaultAsync(x => x.Id == forumId);
if (forum == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ForumDoesNotExist, forumId);
return result;
}
_context.Forums.Remove(forum);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int courseId)
{
var result = new ServiceResult<List<ForumPreviewDto>>();
var forums = await _context.Forums
.Where(x => x.CourseId == courseId)
.ToListAsync();
var forumDtos = _mapper.Map<List<ForumPreviewDto>>(forums);
result.Data = forumDtos;
return result;
}
}
}
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class PostMessageService : IPostMessageService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public PostMessageService(Tsi1Context context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResult<PostMessage>> Create(PostMessageCreateDto newPostMessage)
{
var result = new ServiceResult<PostMessage>();
var postMessage = _mapper.Map<PostMessage>(newPostMessage);
postMessage.Date = DateTime.Now;
_context.PostMessages.Add(postMessage);
await _context.SaveChangesAsync();
result.Data = postMessage;
return result;
}
public async Task<ServiceResult<PostMessage>> Delete(int postMessageId)
{
var result = new ServiceResult<PostMessage>();
var postMessage = await _context.PostMessages.FirstOrDefaultAsync(x => x.Id == postMessageId);
if (postMessage == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.PostMessageDoesNotExist, postMessageId);
return result;
}
_context.PostMessages.Remove(postMessage);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<List<PostMessagePreviewDto>>> GetPostMessages(int postId)
{
var result = new ServiceResult<List<PostMessagePreviewDto>>();
var postMessages = await _context.PostMessages
.Where(x => x.PostId == postId)
.ToListAsync();
var postMessageDtos = _mapper.Map<List<PostMessagePreviewDto>>(postMessages);
result.Data = postMessageDtos;
return result;
}
}
}
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class PostService : IPostService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public PostService(Tsi1Context context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResult<Post>> Create(PostCreateDto newPost)
{
var result = new ServiceResult<Post>();
var post = _mapper.Map<Post>(newPost);
post.Date = DateTime.Now;
_context.Posts.Add(post);
await _context.SaveChangesAsync();
result.Data = post;
return result;
}
public async Task<ServiceResult<Post>> Delete(int postId)
{
var result = new ServiceResult<Post>();
var post = await _context.Posts.FirstOrDefaultAsync(x => x.Id == postId);
if (post == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.PostDoesNotExist, postId);
return result;
}
_context.Posts.Remove(post);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<List<PostPreviewDto>>> GetPosts(int forumId)
{
var result = new ServiceResult<List<PostPreviewDto>>();
var posts = await _context.Posts
.Where(x => x.ForumId == forumId)
.ToListAsync();
var postDtos = _mapper.Map<List<PostPreviewDto>>(posts);
result.Data = postDtos;
return result;
}
}
}
......@@ -4,6 +4,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tsi1.DataLayer\Tsi1.DataLayer.csproj" />
</ItemGroup>
......
......@@ -10,6 +10,7 @@ namespace Tsi1.DataLayer.Entities
{
StudentCourses = new HashSet<StudentCourse>();
ProfessorCourses = new HashSet<ProfessorCourse>();
Forums = new HashSet<Forum>();
}
public int Id { get; set; }
......@@ -17,5 +18,6 @@ namespace Tsi1.DataLayer.Entities
public ICollection<StudentCourse> StudentCourses { get; set; }
public ICollection<ProfessorCourse> ProfessorCourses { get; set; }
public ICollection<Forum> Forums { get; set; }
}
}
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