Пример #1
0
        /// <summary>
        /// Set the update status of the current league
        /// </summary>
        /// <param name="leagueName">Shortname of the league</param>
        /// <param name="principal">Principal of the current User</param>
        protected void UpdateLeague(string leagueName, IPrincipal principal)
        {
            var register = LeagueRegister.Get();

            LeagueEntry leagueEntry = null;

            if (register.Leagues.Any(x => x.Name == leagueName) == false)
            {
                leagueEntry = new LeagueEntry()
                {
                    Name        = leagueName,
                    CreatorName = principal.Identity.Name,
                    OwnerId     = Guid.Parse(principal.Identity.GetUserId()),
                    CreatorId   = Guid.Parse(principal.Identity.GetUserId()),
                    CreatedOn   = DateTime.Now,
                    PrettyName  = leagueName
                };
                register.Leagues.Add(leagueEntry);
            }
            else
            {
                leagueEntry = register.Leagues.SingleOrDefault(x => x.Name == leagueName);
            }
            leagueEntry.LastUpdate = DateTime.Now;
            if (leagueEntry.OwnerId == null)
            {
                leagueEntry.OwnerId = leagueEntry.CreatorId;
            }

            register.Save();
        }
Пример #2
0
        public IHttpActionResult PostLeague([FromUri] string leagueName, [FromUri] string fullName = "")
        {
            // check for empty body and invalid data
            if (string.IsNullOrEmpty(leagueName))
            {
                return(BadRequestEmptyParameter(nameof(leagueName)));
            }
            if (string.IsNullOrEmpty(fullName))
            {
                fullName = leagueName;
            }
            base.CheckLeagueRole(User, leagueName);

            var dbName   = GetDatabaseNameFromLeagueName(leagueName);
            var register = LeagueRegister.Get();

            var league = register.GetLeague(leagueName);

            if (league == null)
            {
                // check current number of leagues for that user
                string userId      = User.Identity.GetUserId();
                var    leagueCount = register.Leagues.Count(x => x.CreatorId.ToString() == userId);
                var    maxLeagues  = 3;
                if (leagueCount >= maxLeagues)
                {
                    return(BadRequest($"Create league failed. Maximum numbers of {maxLeagues} leagues per user reached."));
                }

                using (var dbContext = new LeagueDbContext(dbName, createDb: true))
                {
                    var seasons = dbContext.Seasons;
                    if (seasons == null || seasons.Count() == 0)
                    {
                        seasons.Add(new iRLeagueDatabase.Entities.SeasonEntity()
                        {
                            SeasonName             = "First Season",
                            CreatedByUserName      = User.Identity.Name,
                            CreatedByUserId        = User.Identity.GetUserId(),
                            LastModifiedByUserName = User.Identity.Name,
                            LastModifiedByUserId   = User.Identity.GetUserId()
                        });
                        dbContext.SaveChanges();
                        // Create new entry in league register
                        return(Ok($"New League {leagueName} with database {dbName} created!"));
                    }
                    UpdateLeague(leagueName, User);
                    return(BadRequest($"League {leagueName} already exists!"));
                }
            }

            // if league exists just update fullname
            league.PrettyName = fullName;
            register.Save();
            return(Ok("League information updated"));
        }
Пример #3
0
        public IHttpActionResult Get([FromUri] string leagueName)
        {
            var register = LeagueRegister.Get();

            if (register.Leagues.Any(x => x.Name == leagueName))
            {
                var leagueEntry = register.Leagues.Single(x => x.Name == leagueName);
                return(Ok(leagueEntry.LastUpdate));
            }
            return(BadRequest($"No register entry for {leagueName} found."));
        }
Пример #4
0
        /// <summary>
        /// Get informaiton about the league from the league register
        /// </summary>
        /// <param name="leagueName">Shortname of the league</param>
        /// <returns>DTO containing league information</returns>
        protected LeagueDTO GetLeagueRegisterInfo(string leagueName)
        {
            var register = LeagueRegister.Get();

            var leagueEntry = register.GetLeague(leagueName);

            if (leagueEntry == null)
            {
                return(null);
            }

            var leagueDto = new LeagueDTO()
            {
                Name              = leagueEntry.Name,
                LongName          = leagueEntry.PrettyName,
                CreatedByUserId   = leagueEntry.CreatorId.ToString(),
                CreatedByUserName = leagueEntry.CreatorName,
                CreatedOn         = leagueEntry.CreatedOn,
                LastModifiedOn    = leagueEntry.LastUpdate,
                OwnerUserId       = leagueEntry.OwnerId.ToString()
            };

            return(leagueDto);
        }