public async Task <IActionResult> Create(PlayerDetailedViewModel model) { if (ModelState.IsValid) { var result = await this.playersService.CreateAsync(model); if (!result.Succeeded) { return(RedirectToAction( ActionConstants.Error, PagesConstants.Home, new { area = "", errorMessage = result.Error })); } return(RedirectToAction( ActionConstants.Details, PagesConstants.Teams, new { id = model.TeamId })); } var allTeams = this.teamsService.All <TeamViewModel>(); List <SelectListItem> allTeamsList = new List <SelectListItem>(); foreach (var team in allTeams) { allTeamsList.Add(new SelectListItem { Value = team.Id.ToString(), Text = team.Name }); } this.ViewData[PagesConstants.Teams] = allTeamsList; return(View(model)); }
private bool ValidPlayerDetailedViewModel(PlayerDetailedViewModel model) { return(model.TeamId != Guid.Empty && model.Price > 0 && model.Price <= GlobalConstants.MaxPlayerPrice && !string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Nationality) && !string.IsNullOrWhiteSpace(model.Name) && !string.IsNullOrWhiteSpace(model.Nationality)); }
public async Task <IServiceResult> CreateAsync(PlayerDetailedViewModel model) { var result = new ServiceResult { Succeeded = false }; if (model == null) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } if (!this.ValidPlayerDetailedViewModel(model)) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } var team = await this.teamRepository .GetByIdAsync(model.TeamId); if (team == null) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } Player player = this.CreatePlayer(model); if (model.Image != null) { var uploadResult = this.imagesService.Upload( model.Image, GlobalConstants.PlayerName); if (uploadResult == null) { result.Error = ExceptionConstants.FailedUploadException; return(result); } var image = await this.imagesService.CreateAsync( GlobalConstants.PlayerName, uploadResult.PublicId, uploadResult.Url); player.Image = image; } this.playerRepository.Add(player); await this.playerRepository.SaveChangesAsync(); result.Succeeded = true; return(result); }
private Player CreatePlayer(PlayerDetailedViewModel model) { return(new Player { Name = model.Name, TeamId = model.TeamId, Price = model.Price, Nationality = model.Nationality, Position = model.Position }); }
public IActionResult Create(Guid teamId) { var model = new PlayerDetailedViewModel { TeamId = teamId }; var allTeams = this.teamsService.All <TeamViewModel>(); List <SelectListItem> allTeamsList = new List <SelectListItem>(); foreach (var team in allTeams) { allTeamsList.Add(new SelectListItem { Value = team.Id.ToString(), Text = team.Name }); } this.ViewData[PagesConstants.Teams] = allTeamsList; return(View(model)); }
public void CreateAsync_WithEmptyTeamId_ShouldReturnFalse() { FantasyLeagueDbContextSeeder.Seed(Context, Provider); var model = new PlayerDetailedViewModel { Name = "SomeName", Price = 20, Image = null, Nationality = "SomeNation", TeamId = Guid.Empty }; var result = this.playersService.CreateAsync(model) .GetAwaiter().GetResult(); result.Succeeded.ShouldBeFalse(); this.TearDown(); }
public void CreateAsync_WithVaildData_ShouldReturnTrue() { FantasyLeagueDbContextSeeder.Seed(Context, Provider); var team = this.Context.Teams.First(); var model = new PlayerDetailedViewModel { Name = "SomeName", Price = 20, Image = null, Nationality = "SomeNation", TeamId = team.Id }; var result = this.playersService.CreateAsync(model) .GetAwaiter().GetResult(); result.Succeeded.ShouldBeTrue(); this.TearDown(); }
public void EditAsync_WithInvalidPlayerId_ShouldReturnFalse() { FantasyLeagueDbContextSeeder.Seed(Context, Provider); var team = this.Context.Teams.First(); var model = new PlayerDetailedViewModel { Id = Guid.NewGuid(), Name = "SomeName", Price = 20, Image = null, Nationality = "SomeNation", TeamId = team.Id }; var result = this.playersService.EditAsync(model) .GetAwaiter().GetResult(); result.Succeeded.ShouldBeFalse(); this.TearDown(); }
public void CreateAsync_WithInvaildData_ShouldReturnFalse( string name, double price, string nationality) { FantasyLeagueDbContextSeeder.Seed(Context, Provider); var team = this.Context.Teams.First(); var model = new PlayerDetailedViewModel { Name = name, Price = price, Image = null, Nationality = nationality, TeamId = team.Id }; var result = this.playersService.CreateAsync(model) .GetAwaiter().GetResult(); result.Succeeded.ShouldBeFalse(); this.TearDown(); }
public async Task <IServiceResult> EditAsync(PlayerDetailedViewModel model) { var result = new ServiceResult { Succeeded = false }; if (model == null) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } if (model.Id == Guid.Empty || !ValidPlayerDetailedViewModel(model)) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } var team = await this.teamRepository .GetByIdAsync(model.TeamId); if (team == null) { result.Error = string.Format(ExceptionConstants.InvalidInputException); return(result); } var player = await this.playerRepository .GetByIdAsync(model.Id); if (player == null) { result.Error = string.Format( ExceptionConstants.NotFoundException, GlobalConstants.PlayerName); return(result); } player.Name = model.Name; player.Nationality = model.Nationality; player.TeamId = model.TeamId; player.Price = model.Price; if (model.Image != null) { if (player.Image != null) { this.imagesService.Delete(player.Image); } var uploadResult = this.imagesService.Upload( model.Image, GlobalConstants.PlayerName); if (uploadResult == null) { result.Error = ExceptionConstants.FailedUploadException; return(result); } var image = await this.imagesService.CreateAsync( GlobalConstants.PlayerName, uploadResult.PublicId, uploadResult.Url); player.Image = image; } await this.playerRepository.SaveChangesAsync(); result.Succeeded = true; return(result); }