diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-19 23:05:24 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-19 23:05:24 +0200 |
| commit | d6d70dc432cf2b6523ce9f06575e178cbd208414 (patch) | |
| tree | 23bf9315473067167f67aa8afd9e32299d1f037e | |
| parent | ce6471578364310de31ed02e21ba5d7ae6529234 (diff) | |
| download | DevHive-chat_system.tar DevHive-chat_system.tar.gz DevHive-chat_system.zip | |
Adding Message layerchat_system
7 files changed, 94 insertions, 9 deletions
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<Message> { - + Task<Message> 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<Chat> + { + 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<Message> 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<Guid> CreateMessage(CreateMessageServiceModel createMessageServiceModel) + public async Task<Guid> CreateMessage(CreateMessageServiceModel createMessageServiceModel) { - throw new NotImplementedException(); + //if you think of any validations add them here + + Message message = this._messageMapper.Map<Message>(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<ReadMessageServiceModel> GetMessageById(Guid id) + public async Task<ReadMessageServiceModel> 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<ReadMessageServiceModel>(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<IActionResult> 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<CreateMessageServiceModel>(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<IActionResult> 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<ReadMessageWebModel>(readMessageServiceModel); + + return new OkObjectResult(readMessageWebModel); } [HttpPut] |
