Exemplo n.º 1
0
        public async Task <IActionResult> Upsert(AddChannelViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var user = await TryGetUser(User);

                model.Owners.AddRange(await GetLegitUsers(user));

                return(View("Add", model));
            }


            if (string.IsNullOrEmpty(model.Channel.Name))
            {
                ModelState.AddModelError("Name", "Channel must have a name");
                var user = await TryGetUser(User);

                model.Owners.AddRange(await GetLegitUsers(user));
                return(View("Add", model));
            }


            if (string.IsNullOrEmpty(model.Channel.WebHookAddress))
            {
                ModelState.AddModelError("Name", "Channel must have an endpoint or an address.");
                var user = await TryGetUser(User);

                model.Owners.AddRange(await GetLegitUsers(user));
                return(View("Add", model));
            }

            var existingChannel = _channelManager.GetChannelByName(model.OwnerUserId, model.Channel.Name);

            if (existingChannel != null)
            {
                ModelState.AddModelError("Name", "This user already owns a channel with the same name.");
                var user = await TryGetUser(User);

                model.Owners.AddRange(await GetLegitUsers(user));
                return(View("Add", model));
            }

            model.Channel.OwnerUserId = model.OwnerUserId;

            if (model.Channel.Id == 0)
            {
                await _channelManager.AddChannel(model.Channel);
            }
            else
            {
                await _channelManager.UpdateChannel(model.Channel);
            }

            return(RedirectToAction("Index"));
        }
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var character   = request.Session.Character;
                var channelName = request.Data.GetOrDefault <string>(k.channel);

                _channelManager.GetChannelByName(channelName).ThrowIfNotNull(ErrorCodes.ChannelAlreadyExists);
                _channelManager.CreateChannel(ChannelType.Public, channelName);
                _channelManager.JoinChannel(channelName, character, PresetChannelRoles.ROLE_GOD, null);

                Message.Builder.FromRequest(request).WithOk().Send();

                scope.Complete();
            }
        }