From d6d70dc432cf2b6523ce9f06575e178cbd208414 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 19 Feb 2021 23:05:24 +0200 Subject: Adding Message layer --- src/Data/DevHive.Data.Models/Message.cs | 2 ++ .../DevHive.Data/Interfaces/IMessageRepository.cs | 4 ++- .../DevHive.Data/Repositories/ChatRepository.cs | 20 +++++++++++ .../DevHive.Data/Repositories/MessageRepository.cs | 10 ++++++ .../DevHive.Services/Services/CommentService.cs | 1 + .../DevHive.Services/Services/MessageService.cs | 40 +++++++++++++++++++--- .../DevHive.Web/Controllers/MessageController.cs | 26 ++++++++++++-- 7 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 src/Data/DevHive.Data/Repositories/ChatRepository.cs diff --git a/src/Data/DevHive.Data.Models/Message.cs b/src/Data/DevHive.Data.Models/Message.cs index 89592f8..e9e0e64 100644 --- a/src/Data/DevHive.Data.Models/Message.cs +++ b/src/Data/DevHive.Data.Models/Message.cs @@ -4,6 +4,8 @@ namespace DevHive.Data.Models { public class Message { + public Guid Id { get; set; } + public User Creator { get; set; } public string Content { get; set; } diff --git a/src/Data/DevHive.Data/Interfaces/IMessageRepository.cs b/src/Data/DevHive.Data/Interfaces/IMessageRepository.cs index f694bc0..57a5220 100644 --- a/src/Data/DevHive.Data/Interfaces/IMessageRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IMessageRepository.cs @@ -1,3 +1,5 @@ +using System; +using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories.Interfaces; @@ -5,6 +7,6 @@ namespace DevHive.Data.Interfaces { public interface IMessageRepository : IRepository { - + Task GetMessageByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated); } } diff --git a/src/Data/DevHive.Data/Repositories/ChatRepository.cs b/src/Data/DevHive.Data/Repositories/ChatRepository.cs new file mode 100644 index 0000000..5cda676 --- /dev/null +++ b/src/Data/DevHive.Data/Repositories/ChatRepository.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DevHive.Data.Models; + +namespace DevHive.Data.Repositories +{ + public class ChatRepository : BaseRepository + { + private readonly DevHiveContext _context; + + public ChatRepository(DevHiveContext context) + : base(context) + { + this._context = context; + } + } +} diff --git a/src/Data/DevHive.Data/Repositories/MessageRepository.cs b/src/Data/DevHive.Data/Repositories/MessageRepository.cs index 9697c97..902cabf 100644 --- a/src/Data/DevHive.Data/Repositories/MessageRepository.cs +++ b/src/Data/DevHive.Data/Repositories/MessageRepository.cs @@ -1,5 +1,8 @@ +using System; +using System.Threading.Tasks; using DevHive.Data.Interfaces; using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { @@ -12,5 +15,12 @@ namespace DevHive.Data.Repositories { this._context = context; } + + public async Task GetMessageByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) + { + return await this._context.Message + .FirstOrDefaultAsync(p => p.Creator.Id == creatorId && + p.TimeCreated == timeCreated); + } } } diff --git a/src/Services/DevHive.Services/Services/CommentService.cs b/src/Services/DevHive.Services/Services/CommentService.cs index b48bac8..d4b6846 100644 --- a/src/Services/DevHive.Services/Services/CommentService.cs +++ b/src/Services/DevHive.Services/Services/CommentService.cs @@ -40,6 +40,7 @@ namespace DevHive.Services.Services comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); bool success = await this._commentRepository.AddAsync(comment); + if (success) { Comment newComment = await this._commentRepository diff --git a/src/Services/DevHive.Services/Services/MessageService.cs b/src/Services/DevHive.Services/Services/MessageService.cs index 3e07fab..8b03d08 100644 --- a/src/Services/DevHive.Services/Services/MessageService.cs +++ b/src/Services/DevHive.Services/Services/MessageService.cs @@ -4,6 +4,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using AutoMapper; using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Interfaces; @@ -15,21 +16,50 @@ namespace DevHive.Services.Services { private readonly MessageRepository _messageRepository; private readonly UserRepository _userRepository; + private readonly ChatRepository _chatRepository; + private readonly IMapper _messageMapper; - public MessageService(MessageRepository messageRepository, UserRepository userRepository) + public MessageService(MessageRepository messageRepository, UserRepository userRepository, ChatRepository chatRepository, IMapper messageMapper) { this._messageRepository = messageRepository; this._userRepository = userRepository; + this._chatRepository = chatRepository; + this._messageMapper = messageMapper; } - public Task CreateMessage(CreateMessageServiceModel createMessageServiceModel) + public async Task CreateMessage(CreateMessageServiceModel createMessageServiceModel) { - throw new NotImplementedException(); + //if you think of any validations add them here + + Message message = this._messageMapper.Map(createMessageServiceModel); + + message.TimeCreated = DateTime.Now; + message.Chat = await this._chatRepository.GetByIdAsync(createMessageServiceModel.ChatId); + message.Creator = await this._userRepository.GetByIdAsync(createMessageServiceModel.CreatorId); + + bool result = await this._messageRepository.AddAsync(message); + + if (result) + { + Message newMessage = await this._messageRepository.GetMessageByCreatorAndTimeCreatedAsync(createMessageServiceModel.CreatorId, message.TimeCreated); + + return newMessage.Id; + } + + return Guid.Empty; } - public Task GetMessageById(Guid id) + public async Task GetMessageById(Guid id) { - throw new NotImplementedException(); + Message message = await this._messageRepository.GetByIdAsync(id) ?? + throw new ArgumentException("The message does not exist"); + + User user = await this._userRepository.GetByIdAsync(message.Creator.Id) ?? + throw new ArgumentException("The user does not exist"); + + ReadMessageServiceModel readMessageServiceModel = this._messageMapper.Map(message); + + return readMessageServiceModel; } #region Validations diff --git a/src/Web/DevHive.Web/Controllers/MessageController.cs b/src/Web/DevHive.Web/Controllers/MessageController.cs index 092c7fa..48cfe2e 100644 --- a/src/Web/DevHive.Web/Controllers/MessageController.cs +++ b/src/Web/DevHive.Web/Controllers/MessageController.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Models.Message; using DevHive.Services.Services; using DevHive.Web.Models.Message; using Microsoft.AspNetCore.Authorization; @@ -13,24 +15,42 @@ namespace DevHive.Web.Controllers public class MessageController : ControllerBase { private readonly MessageService _messageService; + private readonly IMapper _messageMapper; - public MessageController(MessageService messageService) + public MessageController(MessageService messageService, IMapper messageMapper) { this._messageService = messageService; + this._messageMapper = messageMapper; } [HttpPost] [Authorize(Roles = "User,Admin")] public async Task Create(Guid userId, [FromBody] CreateMessageWebModel createMessageWebModel, [FromHeader] string authorization) { - throw new NotImplementedException(); + if (!await this._messageService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateMessageServiceModel createMessageServiceModel = this._messageMapper.Map(createMessageWebModel); + + Guid messageId = await this._messageService.CreateMessage(createMessageServiceModel); + + if (messageId == Guid.Empty) + return BadRequest(); + + return new OkObjectResult(messageId); } [HttpGet] [Authorize(Roles = "User,Admin")] public async Task Read(Guid id) { - throw new NotImplementedException(); + ReadMessageServiceModel readMessageServiceModel = await this._messageService.GetMessageById(id); + if (readMessageServiceModel == null) + return BadRequest("Message does not exist!"); + + ReadMessageWebModel readMessageWebModel = this._messageMapper.Map(readMessageServiceModel); + + return new OkObjectResult(readMessageWebModel); } [HttpPut] -- cgit v1.2.3