Example #1
0
        public async Task UpdateSeason(SeasonEditModel season)
        {
            if (season == null)
            {
                throw new ArgumentNullException(nameof(season));
            }

            // Find record to update
            var seasonToUpdate = _context.Season.Single(s => s.Id == season.Id);

            // Update own props
            seasonToUpdate.Label       = season.Label;
            seasonToUpdate.WinningTeam = season.WinningTeamId;
            _context.Update(seasonToUpdate);
            await _context.SaveChangesAsync();

            // Update winners
            using (var transactionalQueryExecutor = _queryExecutor.BeginTransaction()) {
                try {
                    await transactionalQueryExecutor
                    .NewQuery("DELETE FROM [dbo].[SeasonWinner] WHERE [Season]=@Season")
                    .WithCommandType(CommandType.Text)
                    .WithParameters(new { Season = season.Id })
                    .ExecuteAsync();

                    var winnersToAdd = season.WinningParticipantIds.Select(
                        wp => new SeasonWinner {
                        Season      = season.Id,
                        Participant = wp
                    });
                    foreach (var winner in winnersToAdd)
                    {
                        await transactionalQueryExecutor
                        .NewQuery(
                            @"
              INSERT INTO [dbo].[SeasonWinner]
                         ([Season]
                         ,[Participant])
                   VALUES
                         (@Season
                         ,@Participant)")
                        .WithCommandType(CommandType.Text)
                        .WithParameters(new { Season = winner.Season, Participant = winner.Participant })
                        .ExecuteAsync();
                    }

                    transactionalQueryExecutor.Commit();
                } catch {
                    transactionalQueryExecutor.Rollback();
                    throw;
                }
            }
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Sport,WinningTeamId,WinningParticipantIds[]")][ModelBinder(typeof(SeasonEditModel.SeasonEditModelBinder))] SeasonEditModel season)
        {
            if (id != season.Id)
            {
                return(NotFound());
            }

            var seasonForValidation = await _seasonService.LoadDataRecord(id);

            seasonForValidation.Label = season.Label;
            await _seasonModelStatePopulator.ValidateAndPopulateForUpdate(ModelState, id, seasonForValidation);

            if (ModelState.IsValid)
            {
                await _seasonService.UpdateSeason(season);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(await _seasonService.LoadDisplayModel(id)));
        }