diff --git a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs index 3e8575cc042c03c47236e69f02a24c9b39011b68..c0e99fbbcffdbea35382eda2355c5ce83d4ce6f6 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs @@ -2,8 +2,11 @@ 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 @@ -18,5 +21,47 @@ namespace Tsi1.Api.Controllers { _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(); + } } } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs b/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs new file mode 100644 index 0000000000000000000000000000000000000000..eb1c19e5a54fc654f061ea3fea103b473a04ec17 --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs @@ -0,0 +1,71 @@ +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(); + } + } +} diff --git a/Tsi1.Api/Tsi1.Api/Controllers/PostMessageController.cs b/Tsi1.Api/Tsi1.Api/Controllers/PostMessageController.cs new file mode 100644 index 0000000000000000000000000000000000000000..0a9245696204bf380622fe3862d25797d5cfdd4c --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/PostMessageController.cs @@ -0,0 +1,71 @@ +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> GetGetPostMessagesPosts(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(); + } + } +} diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index 75f0c2ee47cd0d3547f62e94e29bbf3aaa31c62e..0c5c2155f2cabb390a140c0d999df314e08957b1 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -43,6 +43,8 @@ namespace Tsi1.Api services.AddScoped<IUserTypeService, UserTypeService>(); services.AddScoped<ICourseService, CourseService>(); services.AddScoped<IForumService, ForumService>(); + services.AddScoped<IPostService, PostService>(); + services.AddScoped<IPostMessageService, PostMessageService>(); services.AddCors(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs index f9506ddd0750378c205e74dd87db361a93b618fe..2756562ff74849602f0f89d682c40384ea6d219f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tsi1.BusinessLayer.Dtos { - class ForumCreateDto + public class ForumCreateDto { public string Name { get; set; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumPreviewDto.cs index ae1ca4b9cdcb312a2f2c4fbe5c1610568374be1f..efe56b67319500b2cc0014de56d445304fe488fc 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumPreviewDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumPreviewDto.cs @@ -4,7 +4,7 @@ using System.Text; namespace Tsi1.BusinessLayer.Dtos { - class ForumPreviewDto + public class ForumPreviewDto { public int Id { get; set; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostCreateDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..b3bbe807c6a6b143d26911a86041054578795b94 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostCreateDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class PostCreateDto + { + public string Title { get; set; } + + public int ForumId { get; set; } + + public int UserId { get; set; } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessageCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessageCreateDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..b42cf311baaf3433c13095f34511bc3b32a5ba27 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessageCreateDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class PostMessageCreateDto + { + public string Content { get; set; } + + public int UserId { get; set; } + + public int PostId { get; set; } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessagePreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessagePreviewDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..cad57d7938a6e616a89e7d82e5a9d85909364296 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostMessagePreviewDto.cs @@ -0,0 +1,20 @@ +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; } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostPreviewDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..526ca193b0a22f3474086050ffa6c428a4035f46 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/PostPreviewDto.cs @@ -0,0 +1,20 @@ +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; } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 1343433604817d83c0fea7a340789d7a34c66e70..95ed2d6e8f2f1d2e382d4b27728a71c05f72d58a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -1,6 +1,7 @@ using AutoMapper; using System; using System.Collections.Generic; +using System.Security.Cryptography.X509Certificates; using System.Text; using Tsi1.BusinessLayer.Dtos; using Tsi1.DataLayer.Entities; @@ -13,9 +14,17 @@ namespace Tsi1.BusinessLayer.Helpers { 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>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostMessageService.cs new file mode 100644 index 0000000000000000000000000000000000000000..a1b34f403dc64bc82c1609eaf85f4a7656640308 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostMessageService.cs @@ -0,0 +1,19 @@ +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); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostService.cs index 6c31e433187592fbd017feccc0bbf308bd0251d3..40dbc56c173fcd990aa3c43ae8c6c5d8dac8d5ea 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IPostService.cs @@ -1,10 +1,19 @@ 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 { - interface IPostService + public interface IPostService { + Task<ServiceResult<List<PostPreviewDto>>> GetPosts(int forumId); + + Task<ServiceResult<Post>> Create(PostCreateDto newPost); + + Task<ServiceResult<Post>> Delete(int postId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs new file mode 100644 index 0000000000000000000000000000000000000000..067a3317b0b92812470a91b85d613ef5266e440a --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs @@ -0,0 +1,79 @@ +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; + } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostService.cs new file mode 100644 index 0000000000000000000000000000000000000000..0d625f4d02207dfae95d3c44aac8555516f83b40 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostService.cs @@ -0,0 +1,79 @@ +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; + } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj b/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj index 71bca384a32afadf9f0d5acbb9ad681f05bc0cfa..bf03c351a4e0b3702f4c73b7b986ddf95efa3291 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj +++ b/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj @@ -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>