/// <summary> /// This method will fetch all information about direct messaging entry. /// </summary> /// <param name="userId">The id of the requesting user</param> /// <param name="firstParticipantId">The user id of the first participant</param> /// <param name="secondParticipantId">The user id of the second participant</param> /// <returns>An either monad</returns> public Either <DirectMessaging, Error> Get(long userId, long firstParticipantId, long secondParticipantId) { try { if (userId != firstParticipantId && userId != secondParticipantId) { return(new Failure <DirectMessaging, Error>(DirectMessagingErrors.DirectMessagingNotFound())); } var directMessaging = _burstChatContext .DirectMessaging .Include(dm => dm.FirstParticipantUser) .Include(dm => dm.SecondParticipantUser) .FirstOrDefault(dm => (dm.FirstParticipantUserId == firstParticipantId && dm.SecondParticipantUserId == secondParticipantId) || (dm.FirstParticipantUserId == secondParticipantId && dm.SecondParticipantUserId == firstParticipantId)); return(directMessaging is not null ? new Success <DirectMessaging, Error>(directMessaging) : new Failure <DirectMessaging, Error>(DirectMessagingErrors.DirectMessagesNotFound())); } catch (Exception e) { _logger.LogError(e.Message); return(new Failure <DirectMessaging, Error>(SystemErrors.Exception())); } }
/// <summary> /// This method will delete a message from the direct messaging entry. /// </summary> /// <param name="userId">The id of the requesting user</param> /// <param name="directMessagingId">The id of the direct messaging entry</param> /// <param name="messageId">The id of the message to be deleted</param> /// <returns>An either monad</returns> public Either <Message, Error> DeleteMessage(long userId, long directMessagingId, long messageId) { try { return(Get(userId, directMessagingId).Bind <Message>(_ => { var directMessaging = _burstChatContext .DirectMessaging .Include(dm => dm.Messages.Where(m => m.Id == messageId)) .First(dm => dm.Id == directMessagingId); if (!directMessaging.Messages.Any()) { return new Failure <Message, Error>(DirectMessagingErrors.DirectMessagesNotFound()); } var message = directMessaging.Messages.First() !; directMessaging.Messages.Remove(message); _burstChatContext.SaveChanges(); return new Success <Message, Error>(message); })); } catch (Exception e) { _logger.LogError(e.Message); return(new Failure <Message, Error>(SystemErrors.Exception())); } }
/// <summary> /// This method will add a new message to a direct messaging entry. /// </summary> /// <param name="userId">The id of the requesting user</param> /// <param name="directMessagingId">The id of the target direct messaging entry</param> /// <param name="message">The message instance that will be used for the insertion</param> /// <returns>An either monad</returns> public Either <Message, Error> InsertMessage(long userId, long directMessagingId, Message message) { try { return(Get(userId, directMessagingId).Bind <Message>(directMessaging => { var user = _burstChatContext .Users .FirstOrDefault(u => u.Id == userId); if (user is null || message is null) { return new Failure <Message, Error>(DirectMessagingErrors.DirectMessagesNotFound()); } var newMessage = new Message { User = user, Links = message.GetLinksFromContent(), Content = message.RemoveLinksFromContent(), Edited = false, DatePosted = message.DatePosted }; directMessaging.Messages.Add(newMessage); _burstChatContext.SaveChanges(); return new Success <Message, Error>(newMessage); })); } catch (Exception e) { _logger.LogError(e.Message); return(new Failure <Message, Error>(SystemErrors.Exception())); } }
/// <summary> /// This method will fetch all information about direct messaging entry. /// </summary> /// <param name="userId">The id of the requesting user</param> /// <param name="directMessagingId">The id of the direct messages</param> /// <returns>An either monad</returns> public Either <DirectMessaging, Error> Get(long userId, long directMessagingId) { try { var directMessaging = _burstChatContext .DirectMessaging .FirstOrDefault(dm => dm.Id == directMessagingId && (dm.FirstParticipantUserId == userId || dm.SecondParticipantUserId == userId)); return(directMessaging is not null ? new Success <DirectMessaging, Error>(directMessaging) : new Failure <DirectMessaging, Error>(DirectMessagingErrors.DirectMessagesNotFound())); } catch (Exception e) { _logger.LogError(e.Message); return(new Failure <DirectMessaging, Error>(SystemErrors.Exception())); } }