コード例 #1
0
        /// <summary>
        /// This method will return the instance of a server based on the provided user and channel id.
        /// </summary>
        /// <param name="userId">The id of the target user</param>
        /// <param name="channelId">the id of the target channel</param>
        /// <returns>An instance of a Server or null</returns>
        private Either <Server, Error> GetServer(long userId, int channelId)
        {
            try
            {
                var server = _burstChatContext
                             .Servers
                             .Include(s => s.Channels)
                             .Include(s => s.Subscriptions)
                             .AsQueryable()
                             .FirstOrDefault(s => s.Subscriptions.Any(sub => sub.UserId == userId) &&
                                             s.Channels.Any(c => c.Id == channelId));

                if (server is null)
                {
                    return(new Failure <Server, Error>(ChannelErrors.ChannelNotFound()));
                }
                else
                {
                    return(new Success <Server, Error>(server));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(new Failure <Server, Error>(SystemErrors.Exception()));
            }
        }
コード例 #2
0
        /// <summary>
        /// This method will return the data of a channel based on the provided channel id.
        /// </summary>
        /// <param name="userId">The id of the requesting user</param>
        /// <param name="channelId">The id of the target channel</param>
        /// <returns>An either monad</returns>
        public Either <Channel, Error> Get(long userId, int channelId)
        {
            try
            {
                return(GetServer(userId, channelId).Bind <Channel>(_ =>
                {
                    var channel = _burstChatContext
                                  .Channels
                                  .FirstOrDefault(c => c.Id == channelId);

                    if (channel is not null && channel.IsPublic)
                    {
                        return new Success <Channel, Error>(channel);
                    }
                    else
                    {
                        return new Failure <Channel, Error>(ChannelErrors.ChannelNotFound());
                    }
                }));