Beispiel #1
0
        public async Task UpdateNotExistingReferee()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var refereesList = new List <Referee>();

            var mockCountryRepo = new Mock <IRepository <Country> >();
            var mockRefereeRepo = new Mock <IRepository <Referee> >();

            mockRefereeRepo.Setup(r => r.All()).Returns(refereesList.AsQueryable());

            var refereeService = new RefereeService(mockRefereeRepo.Object, mockCountryRepo.Object);

            var updatedViewModel = new RefereeViewModel
            {
                Id        = 1,
                Name      = "Mike Dean",
                CountryId = 1
            };

            await Assert.ThrowsAsync <Exception>(() => refereeService.UpdateAsync(updatedViewModel));
        }
Beispiel #2
0
        public IHttpActionResult Get(int id, int unionId)
        {
            var refServ    = new RefereeSevice();
            var seasonServ = new SeasonsRepo();
            int?seasonId   = seasonServ.GetLasSeasonByUnionId(unionId);
            var gamesList  = refServ.GetRefereeGames(id, seasonId);

            var vm = new RefereeViewModel();

            if (gamesList.Count > 0)
            {
                vm.LiveGame = gamesList.FirstOrDefault(t => t.Status == GameStatus.Started);

                vm.ClosedGames = gamesList.Where(t => t.Status != GameStatus.Started && t.Status != GameStatus.Next)
                                 .OrderBy(t => t.StartDate)
                                 .ToList();

                if (vm.ClosedGames != null && vm.ClosedGames.Count > 0)
                {
                    vm.CloseGame = vm.ClosedGames.Last();
                    vm.NextGames = gamesList.Where(t => t.StartDate > vm.CloseGame.StartDate && t.Status != GameStatus.Started)
                                   .OrderBy(t => t.StartDate)
                                   .ToList();
                }
                else
                {
                    vm.NextGames = gamesList.Where(t => t.Status != GameStatus.Started)
                                   .OrderBy(t => t.StartDate)
                                   .ToList();
                }
            }


            return(Ok(vm));
        }
        public async Task <IActionResult> Edit(RefereeViewModel refereeViewModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Data is not valid");
                refereeViewModel.CountriesItems = countryService.GetAllAsKeyValuePairs();

                return(View(refereeViewModel));
            }

            try
            {
                await refereeService.UpdateAsync(refereeViewModel);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException?.Message ?? ex.Message);
                refereeViewModel.CountriesItems = countryService.GetAllAsKeyValuePairs();

                return(View(refereeViewModel));
            }

            TempData["SuccessMessage"] = $"Referee {refereeViewModel.Name} updated successfully.";

            return(RedirectToAction("Index"));
        }
        public IActionResult Create()
        {
            var refereeViewModel = new RefereeViewModel
            {
                BirthDate      = DateTime.Now,
                CountriesItems = countryService.GetAllAsKeyValuePairs()
            };

            return(View(refereeViewModel));
        }
        public ActionResult Edit([Bind(Include = "Referee")] RefereeViewModel refereeEditViewModel)
        {
            if (ModelState.IsValid)
            {
                refereeService.Update(refereeEditViewModel.Referee);
                return(RedirectToAction("Details", "Tournaments", new { id = refereeEditViewModel.Referee.TournamentId, active = "Referees" }));
            }
            Tournament tournament = tournamentService.GetById(refereeEditViewModel.Referee.TournamentId);

            refereeEditViewModel.PositionList = GetPositionList(tournament);
            return(View(refereeEditViewModel));
        }
Beispiel #6
0
        public async Task SaveAndUpdateRefereeWithNameOfAnotherdExistingReferee()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var refereesList = new List <Referee>();
            var id           = 1;

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockRefereeRepo = new Mock <IRepository <Referee> >();

            mockRefereeRepo.Setup(r => r.All()).Returns(refereesList.AsQueryable());
            mockRefereeRepo.Setup(r => r.AddAsync(It.IsAny <Referee>())).Callback <Referee>(referee => refereesList.Add(new Referee
            {
                Id      = id++,
                Name    = referee.Name,
                Country = referee.Country
            }));

            var refereeService = new RefereeService(mockRefereeRepo.Object, mockCountryRepo.Object);

            var firstRefereeViewModel = new RefereeViewModel
            {
                Name      = "Michael Oliver",
                CountryId = 1
            };

            var secondRefereeViewModel = new RefereeViewModel
            {
                Name      = "Mike Dean",
                CountryId = 1
            };

            await refereeService.CreateAsync(firstRefereeViewModel);

            await refereeService.CreateAsync(secondRefereeViewModel);

            var secondUpdatedViewModel = new RefereeViewModel
            {
                Id        = 2,
                Name      = "Michael Oliver",
                CountryId = 1
            };

            await Assert.ThrowsAsync <Exception>(() => refereeService.UpdateAsync(secondUpdatedViewModel));
        }
        public IActionResult Edit(int id)
        {
            var referee          = refereeService.Get(id);
            var refereeViewModel = new RefereeViewModel
            {
                Id             = id,
                Name           = referee.Name,
                BirthDate      = referee.BirthDate,
                CountryId      = referee.Country.Id,
                CountryName    = referee.Country.Name,
                CountriesItems = countryService.GetAllAsKeyValuePairs()
            };

            return(View(refereeViewModel));
        }
Beispiel #8
0
        public async Task GetAllRefereesAsKeyValuePairs()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var refereesList = new List <Referee>();

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockRefereeRepo = new Mock <IRepository <Referee> >();

            mockRefereeRepo.Setup(r => r.All()).Returns(refereesList.AsQueryable());
            mockRefereeRepo.Setup(r => r.AddAsync(It.IsAny <Referee>())).Callback <Referee>(referee => refereesList.Add(new Referee
            {
                Id      = 1,
                Name    = referee.Name,
                Country = referee.Country
            }));

            var refereeService = new RefereeService(mockRefereeRepo.Object, mockCountryRepo.Object);

            var firstRefereeViewModel = new RefereeViewModel
            {
                Name           = "Michael Oliver",
                CountryId      = 1,
                CountriesItems = new CountryService(mockCountryRepo.Object).GetAllAsKeyValuePairs()
            };

            var secondRefereeViewModel = new RefereeViewModel
            {
                Name      = "Mike Dean",
                CountryId = 1
            };

            await refereeService.CreateAsync(firstRefereeViewModel);

            await refereeService.CreateAsync(secondRefereeViewModel);

            var keyValuePairs = refereeService.GetAllAsKeyValuePairs().ToList();

            Assert.True(keyValuePairs.Count == 2);
            Assert.True(firstRefereeViewModel.CountriesItems.Count() == 1);
        }
        public IActionResult Delete(int id)
        {
            var referee          = refereeService.Get(id);
            var refereeViewModel = new RefereeViewModel
            {
                Id          = id,
                Name        = referee.Name,
                BirthDate   = referee.BirthDate,
                CountryId   = referee.Country.Id,
                CountryName = referee.Country.Name,
            };

            ViewData["EntityName"] = "Referee";

            return(View("../Shared/_Delete", refereeViewModel));
        }
Beispiel #10
0
        public async Task SaveAndUpdateReferee()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var refereesList = new List <Referee>();

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockRefereeRepo = new Mock <IRepository <Referee> >();

            mockRefereeRepo.Setup(r => r.All()).Returns(refereesList.AsQueryable());
            mockRefereeRepo.Setup(r => r.AddAsync(It.IsAny <Referee>())).Callback <Referee>(referee => refereesList.Add(new Referee
            {
                Id      = 1,
                Name    = referee.Name,
                Country = referee.Country
            }));

            var refereeService = new RefereeService(mockRefereeRepo.Object, mockCountryRepo.Object);

            var refereeViewModel = new RefereeViewModel
            {
                Name      = "Mike Dean",
                CountryId = 1
            };

            await refereeService.CreateAsync(refereeViewModel);

            var updatedViewModel = new RefereeViewModel
            {
                Id        = 1,
                Name      = "Michael Oliver",
                CountryId = 1
            };

            await refereeService.UpdateAsync(updatedViewModel);

            var savedReferee = refereeService.Get(1);

            Assert.Equal(1, savedReferee.Id);
            Assert.Equal("Michael Oliver", savedReferee.Name);
        }
        public async Task <IActionResult> Delete(RefereeViewModel refereeViewModel)
        {
            try
            {
                await refereeService.DeleteAsync(refereeViewModel.Id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException?.Message ?? ex.Message);
                ViewData["EntityName"] = "Referee";

                return(View("../Shared/_Delete", refereeViewModel));
            }

            TempData["SuccessMessage"] = "Referee deleted successfully.";

            return(RedirectToAction("Index"));
        }
Beispiel #12
0
        public async Task CreateAsync(RefereeViewModel refereeViewModel)
        {
            var doesRefereeExist = refereeRepository.All().Any(c => c.Name == refereeViewModel.Name);

            if (doesRefereeExist)
            {
                throw new Exception($"Referee with a name {refereeViewModel.Name} already exists.");
            }

            var referee = new Referee
            {
                Name      = refereeViewModel.Name,
                BirthDate = refereeViewModel.BirthDate,
                Country   = countryRepository.Get(refereeViewModel.CountryId)
            };

            await refereeRepository.AddAsync(referee);

            await refereeRepository.SaveChangesAsync();
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Referee referee = refereeService.GetById((int)id);

            if (referee == null)
            {
                return(HttpNotFound());
            }

            RefereeViewModel refereeEditViewModel = new RefereeViewModel()
            {
                Referee      = referee,
                PositionList = GetPositionList(referee.Tournament)
            };

            return(View(refereeEditViewModel));
        }
Beispiel #14
0
        public async Task SaveAndLoadReferee()
        {
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var refereesList = new List <Referee>();

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockRefereeRepo = new Mock <IRepository <Referee> >();

            mockRefereeRepo.Setup(r => r.All()).Returns(refereesList.AsQueryable());
            mockRefereeRepo.Setup(r => r.AddAsync(It.IsAny <Referee>())).Callback <Referee>(referee => refereesList.Add(referee));
            mockRefereeRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => refereesList.FirstOrDefault(c => c.Id == id));

            var refereeService = new RefereeService(mockRefereeRepo.Object, mockCountryRepo.Object);

            var refereeViewModel = new RefereeViewModel
            {
                Name        = "Michael Oliver",
                CountryId   = 1,
                CountryName = "England",
                BirthDate   = new DateTime(1990, 1, 1)
            };

            await refereeService.CreateAsync(refereeViewModel);

            var savedReferee     = refereeService.Get(10, false);
            var lastSavedReferee = refereeService.GetAll().LastOrDefault();

            Assert.Null(savedReferee);
            Assert.Equal("Michael Oliver", lastSavedReferee.Name);
            Assert.Equal("England", refereeViewModel.CountryName);
            Assert.Equal(new DateTime(1990, 1, 1), refereeViewModel.BirthDate);
            Assert.NotNull(lastSavedReferee.Country);
        }
Beispiel #15
0
        public async Task UpdateAsync(RefereeViewModel refereeViewModel)
        {
            var allReferees = refereeRepository.All();
            var referee     = allReferees.FirstOrDefault(c => c.Id == refereeViewModel.Id);

            if (referee is null)
            {
                throw new Exception($"Referee not found");
            }

            var doesRefereeExist = allReferees.Any(c => c.Id != refereeViewModel.Id && c.Name == refereeViewModel.Name);

            if (doesRefereeExist)
            {
                throw new Exception($"Referee with a name {refereeViewModel.Name} already exists.");
            }

            referee.Name      = refereeViewModel.Name;
            referee.BirthDate = refereeViewModel.BirthDate;
            referee.Country   = countryRepository.Get(refereeViewModel.CountryId);

            await refereeRepository.SaveChangesAsync();
        }
        public ActionResult Create(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var tournament = tournamentService.GetById((int)id);

            if (tournament == null)
            {
                return(HttpNotFound());
            }
            RefereeViewModel refereeViewModel = new RefereeViewModel
            {
                Referee = new Referee()
                {
                    TournamentId = (int)id
                },
                PositionList = GetPositionList(tournament)
            };

            return(View(refereeViewModel));
        }