Пример #1
0
        public EliteMedalsViewModel(
            int season,
            Dictionary <string, Player> playersDict,
            EliteMedals eliteMedals,
            SeasonResults seasonResults)
        {
            this.season = season;
            var players = new List <PlayerInfo>();

            foreach (string playerId in playersDict.Keys)
            {
                Player player = playersDict[playerId];
                EliteMedals.EliteMedal existingMedal = eliteMedals.GetExistingMedal(playerId);
                HashSet <Tuple <SeasonResults.PlayerResult, bool> > topThreeResults = seasonResults.GetTopThreeResults(playerId, existingMedal.Value);
                FormattedMedal formattedExistingMedal = eliteMedals.GetFormattedExistingMedal(playerId);
                FormattedMedal nextMedal  = eliteMedals.GetNextMedal(playerId);
                var            playerInfo = new PlayerInfo(
                    playerId,
                    player.Name,
                    player.PersonalNumber,
                    topThreeResults,
                    existingMedal.Value,
                    formattedExistingMedal,
                    nextMedal);
                players.Add(playerInfo);
            }

            Players = players.ToArray();
        }
Пример #2
0
    public ActionResult EliteMedals(int?season)
    {
        if (season.HasValue == false)
        {
            season = CompositionRoot.DocumentSession.LatestSeasonOrDefault(SystemTime.UtcNow.Year);
        }

        Dictionary <string, Player> playersDict = CompositionRoot.DocumentSession.Query <Player, PlayerSearch>()
                                                  .Where(p => p.PlayerStatus == Player.Status.Active)
                                                  .ToDictionary(x => x.Id);
        EliteMedals eliteMedals = CompositionRoot.DocumentSession.Load <EliteMedals>(Domain.EliteMedals.TheId);

        if (eliteMedals == null)
        {
            eliteMedals = new EliteMedals();
            CompositionRoot.DocumentSession.Store(eliteMedals);
        }

        SeasonResults seasonResults = CompositionRoot.DocumentSession.Load <SeasonResults>(SeasonResults.GetId(season.Value));

        if (seasonResults == null)
        {
            seasonResults = new SeasonResults(season.Value);
            CompositionRoot.DocumentSession.Store(seasonResults);
        }

        EliteMedalsViewModel viewModel = new(season.Value, playersDict, eliteMedals, seasonResults);

        return(View(viewModel));
    }
Пример #3
0
    public ActionResult EditMedalsPost(int id, EditMedalsPostModel postModel)
    {
        if (ModelState.IsValid == false)
        {
            return(EditMedals(id));
        }

        EliteMedals eliteMedals = CompositionRoot.DocumentSession.Load <EliteMedals>(Domain.EliteMedals.TheId);

        Debug.Assert(postModel.EliteMedal != null, "postModel.EliteMedal != null");
        eliteMedals.AwardMedal("players-" + id, postModel.EliteMedal !.Value, postModel.CapturedSeason);
        return(RedirectToAction("EliteMedals"));
    }
Пример #4
0
    public ActionResult EditMedals(int id)
    {
        Player      player      = CompositionRoot.DocumentSession.Load <Player>(id);
        EliteMedals eliteMedals = CompositionRoot.DocumentSession.Load <EliteMedals>(Domain.EliteMedals.TheId);

        EliteMedals.EliteMedal eliteMedal = eliteMedals.GetExistingMedal(player.Id);
        EditMedalsViewModel    viewModel  = new(
            player.Name,
            eliteMedal.Value,
            eliteMedal.CapturedSeason.GetValueOrDefault(),
            CompositionRoot.DocumentSession.LatestSeasonOrDefault(DateTime.Now.Year));

        return(View(viewModel));
    }
    public ActionResult GeneratePdf(PostModel postModel)
    {
        // find out current season
        int           season        = CompositionRoot.DocumentSession.LatestSeasonOrDefault(SystemTime.UtcNow.Year);
        SeasonResults seasonResults = CompositionRoot.DocumentSession.Load <SeasonResults>(SeasonResults.GetId(season));

        if (seasonResults == null)
        {
            ModelState.AddModelError("resultat", "Det finns inga resultat för säsongen.");
        }

        if (ModelState.IsValid == false)
        {
            return(RedirectToAction("EliteMedals", "MatchResult"));
        }

        EliteMedals eliteMedals = CompositionRoot.DocumentSession.Load <EliteMedals>(EliteMedals.TheId);
        Dictionary <string, Player> playersDict = CompositionRoot.DocumentSession.Query <Player, PlayerSearch>()
                                                  .Where(p => p.PlayerStatus == Player.Status.Active)
                                                  .ToDictionary(x => x.Id);
        EliteMedalsViewModel viewModel = new(season, playersDict, eliteMedals, seasonResults !);

        string       templateFilename = ConfigurationManager.AppSettings["ElitemedalsTemplateFilename"];
        MemoryStream stream           = new();
        string       archiveFileName  = $"Elitmedaljer_{CompositionRoot.CurrentTenant.TeamFullName}_{season}-{season + 1}.zip";

        using (ZipArchive zip = new(stream, ZipArchiveMode.Create, true))
        {
            EliteMedalsViewModel.PlayerInfo[] playersThatHaveMedalsToAchieve =
                viewModel.Players
                .Where(x => x.ExistingMedal != EliteMedals.EliteMedal.EliteMedalValue.Gold5)
                .OrderBy(x => x.Name)
                .ToArray();
            List <string> listOfMissingPersonalNumbers = new();
            foreach (EliteMedalsViewModel.PlayerInfo player in playersThatHaveMedalsToAchieve)
            {
                PlayerMedalInfo playerMedalInfo = new(
                    player.Name,
                    player.PersonalNumber,
                    player.FormattedExistingMedal(),
                    player.FormattedNextMedal(),
                    player.TopThreeResults);
                CreateFileEntryResult result = CreateFileEntry(
                    zip,
                    templateFilename,
                    playerMedalInfo,
                    CompositionRoot.CurrentTenant,
                    postModel);
                if (result == CreateFileEntryResult.MissingPersonalNumber)
                {
                    listOfMissingPersonalNumbers.Add(player.Name);
                }
            }

            if (listOfMissingPersonalNumbers.Any())
            {
                foreach (string playerName in listOfMissingPersonalNumbers)
                {
                    ModelState.AddModelError(playerName, $"{playerName} saknar personnummer i fliken Medlemmar.");
                }

                return(RedirectToAction("EliteMedals", "MatchResult"));
            }
        }

        _ = stream.Seek(0, SeekOrigin.Begin);
        return(File(stream, "application/zip", archiveFileName));
    }