예제 #1
0
 public ActionResult <object> Select()
 {
     try
     {
         var test  = _seasonService.DoNothing();
         var items = _seasonService.GetAll();
         if (items.ToList().Count > 0)
         {
             var toSerialize = new MessageHelpers <SeasonViewModel>()
             {
                 Status = 200,
                 Data   = new List <SeasonViewModel>(items)
             };
             return(JsonConvert.SerializeObject(toSerialize));
         }
         else
         {
             var toSerialize = new MessageHelpers <SeasonViewModel>()
             {
                 Status = 404,
                 Data   = null
             };
             return(JsonConvert.SerializeObject(toSerialize));
         }
     }
     catch
     {
         var toSerialize = new MessageHelpers <SeasonViewModel>()
         {
             Status = 502,
             Data   = null
         };
         return(JsonConvert.SerializeObject(toSerialize));
     }
 }
        public async Task SaveAndLoadSeason()
        {
            var championshipsList = new List <Championship> {
                new Championship {
                    Id = 1, Name = "Premier League"
                }
            };
            var seasonsList = new List <Season>();

            var mockChampionshipRepo = new Mock <IRepository <Championship> >();

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

            var mockSeasonRepo = new Mock <IRepository <Season> >();

            mockSeasonRepo.Setup(r => r.All()).Returns(seasonsList.AsQueryable());
            mockSeasonRepo.Setup(r => r.AddAsync(It.IsAny <Season>())).Callback <Season>(season => seasonsList.Add(season));
            mockSeasonRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => seasonsList.FirstOrDefault(c => c.Id == id));

            var seasonService = new SeasonService(mockSeasonRepo.Object, mockChampionshipRepo.Object);

            var seasonViewModel = new SeasonViewModel
            {
                Name             = "2020/21",
                ChampionshipId   = 1,
                ChampionshipName = "Premier League",
                Description      = "Premier League, Season 2020/21"
            };

            await seasonService.CreateAsync(seasonViewModel);

            var savedSeason     = seasonService.Get(10, false);
            var lastSavedSeason = seasonService.GetAll().LastOrDefault();

            Assert.Null(savedSeason);
            Assert.Equal("2020/21", lastSavedSeason.Name);
            Assert.Equal("Premier League, Season 2020/21", seasonViewModel.Description);
            Assert.Equal("Premier League", seasonViewModel.ChampionshipName);
            Assert.NotNull(lastSavedSeason.Championship);
        }
        public async Task SaveAndDeleteSeason()
        {
            var championshipsList = new List <Championship> {
                new Championship {
                    Id = 1, Name = "Premier League"
                }
            };
            var seasonsList = new List <Season>();

            var mockChampionshipRepo = new Mock <IRepository <Championship> >();

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

            var mockSeasonRepo = new Mock <IRepository <Season> >();

            mockSeasonRepo.Setup(r => r.All()).Returns(seasonsList.AsQueryable());
            mockSeasonRepo.Setup(r => r.AddAsync(It.IsAny <Season>())).Callback <Season>(season => seasonsList.Add(new Season
            {
                Id           = 1,
                Name         = season.Name,
                Championship = season.Championship
            }));
            mockSeasonRepo.Setup(r => r.Delete(It.IsAny <Season>())).Callback <Season>(season => seasonsList.Remove(season));

            var seasonService = new SeasonService(mockSeasonRepo.Object, mockChampionshipRepo.Object);

            var seasonViewModel = new SeasonViewModel
            {
                Name           = "2020/21",
                ChampionshipId = 1
            };

            await seasonService.CreateAsync(seasonViewModel);

            await seasonService.DeleteAsync(1);

            Assert.Empty(seasonService.GetAll(false));
        }
예제 #4
0
        public async Task <IActionResult> GetAll()
        {
            var model = await _seasonService.GetAll();

            return(new OkObjectResult(model));
        }
예제 #5
0
 public List <Season> GetSeasons()
 {
     return(seasonService.GetAll().ToList());
 }