public async Task <IActionResult> EditContestantInEvent(string userId, int competitionInstanceId, int competitionId, int eventId) { var user = await _adminService.GetUserByIdAsync(userId); var competitionInstance = await _competitionService.GetCompetitionInstanceByIdAsync(competitionInstanceId); var competiton = await _competitionService.GetCompetitionByIdAsync(competitionId); var _event = await _eventService.GetEventByIdAsync(eventId); var heats = _heatService.GetHeatsForEvent(eventId); var dto = _competitionService.GetEditContestantChipHeatResultDtoFor(userId, eventId, competitionInstanceId); var nationName = _adminService.GetNationalityById((int)user.Nationality); var model = new EditContestantInEventDto { CompetitionName = competiton.Name, CompetitionInstanceName = competitionInstance.Name, EventName = _event.Name, UserName = user.Username, FirstName = user.FirstName, MiddleName = user.MiddleName, LastName = user.LastName, DateOfBirth = user.DateOfBirth, NationId = user.Nationality, Nationality = nationName, Phone = user.Phone, HeatNumber = dto.HeatNumber, Bib = dto.Bib, ChipNumber = dto.ChipNumber, HeatId = dto.HeatId, OldHeatId = dto.HeatId, ContestantInHeatModified = dto.ContestantInHeatModified, Notes = dto.Notes, Status = dto.Status, Team = dto.Team, HeatsInEvent = heats, OldChipCode = dto.ChipCode }; return(View(model)); }
public async Task <IActionResult> EditContestantInEvent(EditContestantInEventDto model, string userId, int competitionInstanceId, int competitionId, int eventId) { if (ModelState.IsValid) { // Get The Chip Contestants in About To Be Connected To var chip = await _chipService.GetChipByNumberAsync(model.ChipNumber); if (chip == null && model.ChipNumber != 0) { return(Json("Chip with this Number does not exist")); } // Get oldChipInHeat To Remove var oldChipInHeat = await _chipService.GetChipInHeatByCodeUserIdAndHeatId(model.OldChipCode, userId, model.OldHeatId); if (oldChipInHeat != null) { var chipSuccess = await _chipService.RemoveChipInHeatAsync(oldChipInHeat); if (!chipSuccess) { return(Json("chipInHeatToRemove not successfully removed")); } } if (model.ChipNumber != 0) { // Edit Fields in Contestants New Chip chip.LastCompetitionInstanceId = competitionInstanceId; chip.LastUserId = userId; chip.LastSeen = DateTime.Now; var chipEdit = await _chipService.EditChipAsync(chip); if (chipEdit != true) { return(Json("Edit Chip Failed")); } // Create New ChipInHeat var newChipInHeat = new ChipInHeat { UserId = userId, ChipCode = chip.Code, HeatId = model.HeatId, Valid = true }; // Assigning New ChipInHeat To User try { await _chipService.AssignChipToUserInHeatAsync(newChipInHeat); } catch (Exception e) { return(Json(e.Message)); } } // Get ContestantInHeat To Remove var contestantInHeat = await _heatService.GetContestantsInHeatByUserIdAndHeatIdAsync(userId, model.OldHeatId); if (contestantInHeat == null) { return(Json("contestantInHeat does not match this userId and heatId")); } // Get Old Results to keep track of data before it is deleted var oldResult = await _resultService.GetResultAsync(userId, model.OldHeatId); if (oldResult == null) { return(Json("Result does not match this userId and heatId")); } // Removes ContestantInHeat and Result await _heatService.RemoveAsyncContestantInHeat(contestantInHeat); // Create New ContestantIn Heat To Replace The Old One // New Result Will Be Created Automatically var newContestantInHeat = new ContestantInHeat { HeatId = model.HeatId, UserId = userId, Bib = model.Bib, Team = model.Team, Modified = DateTime.Now }; // Save newContestantInHeat In Database await _heatService.AddAsyncContestantInHeat(newContestantInHeat); // Get The New Result To Update Its Data var newResult = await _resultService.GetResultAsync(userId, newContestantInHeat.HeatId); if (newResult == null) { return(Json("newResult was not created which means newContestantInHeat was not created")); } // Edit Field That Came From The Model newResult.Modified = DateTime.Now; newResult.HeatId = model.HeatId; newResult.Status = model.Status; newResult.Notes = model.Notes; newResult.UserId = userId; // Edit Fields That Came From The Old Result newResult.Name = oldResult.Name; newResult.Club = oldResult.Club; newResult.Country = oldResult.Country; newResult.Created = oldResult.Created; newResult.Gender = oldResult.Gender; newResult.FinalTime = oldResult.FinalTime; newResult.Nationality = oldResult.Nationality; // Save newResult In Database await _resultService.EditAsync(newResult); return(RedirectToAction("Contestants", "CompetitionInstance", new { competitionId, competitionInstanceId })); } return(View(model)); }