Beispiel #1
0
        public static async Task <ContentResult> Add(SocialGamificationAssetContext db, Match match, Actor actor)
        {
            // Add actors to this match
            var matchActor = new MatchActor {
                MatchId = match.Id, ActorId = actor.Id
            };

            db.MatchActors.Add(matchActor);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                return(HttpResponseHelper.ErrorContentResult(e.Message, StatusCodes.Status500InternalServerError));
            }

            for (var i = 1; i <= match.TotalRounds; ++i)
            {
                // Add round(s) entry for each Actor
                var matchRound = new MatchRound {
                    MatchActorId = matchActor.Id, RoundNumber = i
                };

                db.MatchRounds.Add(matchRound);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbEntityValidationException e)
                {
                    return(HttpResponseHelper.ErrorContentResult(e.Message, StatusCodes.Status500InternalServerError));
                }
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        ///     Asynchronously save data
        /// </summary>
        /// <returns>
        ///     <para>
        ///         ErrorContentResult if
        ///         <see cref="System.Data.Entity.Infrastructure.DbUpdateException" />
        ///     </para>
        ///     <para>exception occurs</para>
        /// </returns>
        public static async Task <ContentResult> SaveChanges(
            SocialGamificationAssetContext _context,
            bool isAsync = false)
        {
            try
            {
                if (isAsync)
                {
                    await _context.SaveChangesAsync();
                }
                else
                {
                    _context.SaveChanges();
                }
            }
            catch (DbUpdateException e)
            {
                return(HttpResponseHelper.ErrorContentResult(
                           GetExceptionString(e),
                           StatusCodes.Status500InternalServerError));
            }

            return(null);
        }
Beispiel #3
0
        public virtual async Task OnAuthorizationAsync(AuthorizationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Allow Anonymous skips all authorization
            if (context.Filters.Any(item => item is IAllowAnonymousFilter))
            {
                return;
            }

            var httpContext = context.HttpContext;

            // Check if Request is from Documentation Generator
            string documentationApiKey = httpContext.Request.Query[DocumentationApiKey];

            if (!string.IsNullOrEmpty(documentationApiKey))
            {
                if (documentationApiKey.Equals(DocumentationApiValue, StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }

                context.Result = HttpResponseHelper.BadRequest($"Invalid value for {DocumentationApiKey}.");

                return;
            }

            StringValues header;
            var          headerExists = httpContext.Request.Headers.TryGetValue(SessionHeaderName, out header);

            // Check if Session Header exists
            if (!headerExists)
            {
                context.Result = HttpResponseHelper.Unauthorized($"No {SessionHeaderName} Header found.");

                return;
            }

            Guid token;
            var  isValidGuid = Guid.TryParse(header.FirstOrDefault(), out token);

            // Token value must be valid Guid
            if (!isValidGuid)
            {
                context.Result = HttpResponseHelper.Unauthorized($"Invalid {SessionHeaderName} Header.");

                return;
            }

            var localSession = SessionExtensions.GetObjectFromJson <Session>(httpContext.Session, "__session");

            // If 'active' session already exists skip DB call
            if (localSession != null && localSession.Id.Equals(token) && localSession.Player != null)
            {
                return;
            }

            var db =
                ServiceProviderExtensions.GetRequiredService <SocialGamificationAssetContext>(
                    httpContext.RequestServices);

            if (db == null)
            {
                context.Result =
                    HttpResponseHelper.ErrorContentResult(
                        "Unable to connect with requested Database service.",
                        StatusCodes.Status503ServiceUnavailable);

                return;
            }

            var session = await db.Sessions.Where(s => s.Id.Equals(token)).Include(s => s.Player).FirstOrDefaultAsync();

            // Find Session
            if (session == null)
            {
                context.Result = HttpResponseHelper.NotFound($"Session {token} is Invalid.");

                return;
            }

            // Set right Session
            SessionExtensions.SetObjectAsJson(httpContext.Session, "__session", session);
        }