public async Task <IActionResult> Edit(string id, FootballPlayerEditBindingModel footballPlayerEditBindingModel) { if (!this.ModelState.IsValid) { var allPlayerPosition = await this.footballPlayerService.GetAllPlayerPosition().ToListAsync(); this.ViewData["positions"] = allPlayerPosition .Select(position => new FootballPlayerEditPlayerPositionViewModel { Name = position.Name, }) .ToList(); return(this.View(footballPlayerEditBindingModel)); } string pictureUrl = await this.cloudinaryService .UploadPictureAync(footballPlayerEditBindingModel.Picture, footballPlayerEditBindingModel.Name); FootballPlayerServiceModel footballPlayerServiceModel = footballPlayerEditBindingModel.To <FootballPlayerServiceModel>(); footballPlayerServiceModel.Picture = pictureUrl; await this.footballPlayerService.Edit(id, footballPlayerServiceModel); return(this.Redirect($"/Player/Details/{id}")); }
public async Task <bool> Edit(string id, FootballPlayerServiceModel footballPlayerServiceModel) { PlayerPosition playerPositionFromDb = await this.context.PlayerPositions .SingleOrDefaultAsync(playerPosition => playerPosition.Name == footballPlayerServiceModel.PlayerPosition.Name); if (playerPositionFromDb == null) { throw new ArgumentNullException(nameof(playerPositionFromDb)); } FootballPlayer footballPlayerFromDb = await this.context.FootballPlayers.SingleOrDefaultAsync(player => player.Id == id); if (footballPlayerFromDb == null) { throw new ArgumentNullException(nameof(footballPlayerFromDb)); } footballPlayerFromDb.Name = footballPlayerServiceModel.Name; footballPlayerFromDb.Country = footballPlayerServiceModel.Country; footballPlayerFromDb.Picture = footballPlayerServiceModel.Picture; footballPlayerFromDb.Age = footballPlayerServiceModel.Age; footballPlayerFromDb.Height = footballPlayerServiceModel.Height; footballPlayerFromDb.PlayerNumber = footballPlayerServiceModel.PlayerNumber; footballPlayerFromDb.PlayerPosition = playerPositionFromDb; this.context.Update(footballPlayerFromDb); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Create(FootballPlayerCreateBindingModel footballPlayerCreateBindingModel) { if (!this.ModelState.IsValid) { var allPlayerPositions = await this.footballPlayerService.GetAllPlayerPosition().ToListAsync(); this.ViewData["positions"] = allPlayerPositions .Select(position => new FootballPlayerCreatePlayerPositionViewModel { Name = position.Name, }) .ToList(); } string pictureUrl = await this.cloudinaryService .UploadPictureAync(footballPlayerCreateBindingModel.Picture, footballPlayerCreateBindingModel.Name); FootballPlayerServiceModel footballPlayerServiceModel = footballPlayerCreateBindingModel.To <FootballPlayerServiceModel>(); footballPlayerServiceModel.Picture = pictureUrl; await this.footballPlayerService.CreatePlayer(footballPlayerServiceModel); return(this.Redirect("/")); }
public async Task <bool> CreatePlayer(FootballPlayerServiceModel footballPlayerServiceModel) { PlayerPosition playerPositionFromDb = this.context.PlayerPositions .SingleOrDefault(playerPosition => playerPosition.Name == footballPlayerServiceModel.PlayerPosition.Name); if (playerPositionFromDb == null) { throw new ArgumentNullException(nameof(playerPositionFromDb)); } FootballPlayer footballPlayer = footballPlayerServiceModel.To <FootballPlayer>(); footballPlayer.PlayerPosition = playerPositionFromDb; this.context.FootballPlayers.Add(footballPlayer); int result = await this.context.SaveChangesAsync(); return(result > 0); }