Пример #1
0
        public IActionResult JoinChannel(string id)
        {
            var channel = _service.GetChannel(id).Result;

            if (channel == null)
            {
                return(NotFound());
            }
            var user = _userManager.GetUserAsync(User).Result;

            _service.AddMember(channel, user);
            return(RedirectToAction("Main", "Channel", new { id = channel.Title }));
        }
        public bool TryAddMemberToChannel(Guid user, Guid channelId, Guid member, out string message)
        {
            message = string.Empty;
            IChannel channel = _db.Channels.FirstOrDefault(c => c.ID == channelId);

            if (channel == null)
            {
                message = $"Channel({channelId}) does not exist.";
                return(false);
            }

            if (channel.CurrentMembers.Any(c => c.Member == user) == false)
            {
                message = $"User {user} is not a member of channel {channelId}. Adding member to this channel is not allowed.";
                return(false);
            }

            if (channel.CurrentMembers.Any(c => c.Member == member) == true)
            {
                message = $"User {member} is already a member of channel {channelId}.";
                return(false);
            }

            channel.AddMember(member);
            _dataFileService.SaveDB(_db);
            return(true);
        }
        public bool TryAddConverserToConversation(Guid user, Guid conversationid, Guid converser, out string message)
        {
            message = string.Empty;
            Conversation conv = _db.Conversations?.FirstOrDefault(c => c.ID == conversationid);

            if (conv == null)
            {
                message = $"Conversation {conversationid} not found.";
                return(false);
            }
            if (conv.TerminatedOn != DateTime.MinValue)
            {
                message = $"Conversation {conversationid} is closed.";
            }
            IChannel channel = _db.Channels.FirstOrDefault(c => c.ID == conv.Channel);

            if (channel == null)
            {
                message = $"Channel({conv.Channel}) does not exist or is not associated with conversation {conversationid}.";
                return(false);
            }

            if (channel.CurrentMembers.Any(c => c.Member == user) == false)
            {
                message = $"User {user} is not a member of channel {conv.Channel} of conversations {conversationid}. Adding member {converser} is not allowed.";
                return(false);
            }

            if (channel.CurrentMembers.Any(c => c.Member == converser) == true)
            {
                message = $"User {converser} is already a member of channel {conv.Channel} of conversations {conversationid}.";
                return(false);
            }

            channel.AddMember(converser);
            _dataFileService.SaveDB(_db);
            return(true);
        }