Exemplo n.º 1
0
        public IHttpResponse Create(ChannelCreateInputModel model)
        {
            var name        = model.Name;
            var description = model.Description;
            var tags        = model.Tags;

            if (!Enum.TryParse <ChannelType>(model.Type, out var type))
            {
                return(this.BadRequestErrorWithView("Invalid channel type"));
            }

            var channel = new Channel
            {
                Name        = name,
                Description = description,
                Tags        = tags,
                Type        = type
            };

            this.Context.Channels.Add(channel);

            try
            {
                this.Context.SaveChanges();
            }
            catch (Exception e)
            {
                return(this.BadRequestErrorWithView(e.Message));
            }

            return(this.Redirect("/channels/details?id=" + channel.Id));
        }
        public async Task <IActionResult> Create(ChannelCreateInputModel input)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (user.ChannelId != null)
            {
                return(this.RedirectToAction("Details", new { Id = user.ChannelId }));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            if (!this.channelsService.IsValidChannel(input.Title))
            {
                input.ExistTitle = "This channel title already exist!";
                return(this.View());
            }

            var channelId = await this.channelsService.CreateChannelAsync(input.Title, input.Description, user);

            return(this.RedirectToAction("Details", new { Id = channelId }));
        }