public async Task <IActionResult> CreateChannel(CreateChannelInputModel input) { var user = await this.userManager.GetUserAsync(User); if (!ModelState.IsValid) { return(View(input)); } if (!this.channelService.IsChannelNameAvailable(input.Name)) { ModelState.AddModelError("Name", "Channel name is already taken."); return(View(input)); } var channel = new Channel() { CreatorId = user.Id, Name = input.Name, IsDeleted = false, MaxUsers = input.MaxPlayers, Security = input.Security }; await this.channelService.CreateAsync(channel); await this.channelService.AddUserToChannelAsync(user.Id, channel.Id); return(RedirectToAction("Index")); }
public async Task <IActionResult> Index(CreateChannelInputModel input) { if (!ModelState.IsValid) { return(View(input)); } var user = await this.userManager.GetUserAsync(User); var userChannel = this.userService.GetUsersChannel(user.Id); if (userChannel.Name != input.Name) { if (!this.channelService.IsChannelNameAvailable(input.Name)) { ModelState.AddModelError("Name", "Channel name is taken"); return(View(input)); } } if (input.MaxPlayers < userChannel.CurrentPlayers) { ModelState.AddModelError("MaxPlayers", "Can't set max players below current players count"); return(View(input)); } await this.channelService.UpdateChannelAsync(input, user.Id); return(Redirect("/Channel/Index")); }
public IActionResult Create(CreateChannelInputModel createChannelInputModel) { if (!this.ModelState.IsValid) { return(this.RedirectToAction(nameof(Create))); } string pictureUrl = this.cloudinaryService .UploadPicture(createChannelInputModel.ChannelPicUrl, createChannelInputModel.Name); var username = this.User.Identity.Name; this.channelService.Create(createChannelInputModel.Name, createChannelInputModel.Description, pictureUrl, username); return(this.RedirectToAction(nameof(My))); }
public async Task UpdateChannelAsync(CreateChannelInputModel input, string userId) { var userChannel = this.context.Channels .FirstOrDefault(x => x.CreatorId == userId); if (userChannel == null) { return; } userChannel.Name = input.Name; userChannel.MaxUsers = input.MaxPlayers; userChannel.Security = input.Security; await this.context.SaveChangesAsync(); }
public IHttpResponse Create(CreateChannelInputModel model) { var tagsToSplit = model.Tags.Split(", ", StringSplitOptions.RemoveEmptyEntries).ToList(); var typeAsEnum = Enum.TryParse(model.Type, out ChannelType result); if (!typeAsEnum) { return(BadRequestError("No such type.")); } var channel = new Channel() { Description = model.Description, Name = model.Name, Type = result }; this.Db.Channels.Add(channel); foreach (var tag in tagsToSplit) { var dbTag = this.Db.Tags.FirstOrDefault(x => x.Name == tag.Trim()); if (dbTag == null) { dbTag = new Tag { Name = tag.Trim() }; this.Db.Tags.Add(dbTag); this.Db.SaveChanges(); } channel.Tags.Add(new ChannelTag { TagId = dbTag.Id, }); } this.Db.SaveChanges(); return(this.Redirect("/")); }