Esempio n. 1
0
        private async Task <IActionResult> MutateContextRecord(HashSet <string> userIdStrings, string contextid, bool throw404OnNull, Action <ContextMapping> MutatorFunc, Func <ContextMapping, IActionResult> BuildSuccessResponse)
        {
            if (contextid == null || contextid.Trim().Count() == 0)
            {
                return(StatusCode(400, "contextid query parameter must not be null or empty!"));
            }

            // Get all of the user's current context information from the context mappings table
            ContextMapping matchingcontext = await dbContext.ContextMappings.FirstOrDefaultAsync(e => userIdStrings.Contains(e.UserId.Trim()) && e.Id.Equals(contextid.Trim()));

            // Throw 404 if the context does not exist and we are set to NOT implicitly create.
            if (matchingcontext == null && throw404OnNull)
            {
                return(StatusCode(404, $"Context with id-string: {contextid} does not exist. You can not rename a context which does not exist!"));
            }
            // If the matching context is null, but don't want to throw 404 errors, then we should implicitly create the context!
            else if (matchingcontext == null)
            {
                string userId = User.FindFirst(googleSubjectClaimType).Value.Trim();
                matchingcontext = ContextMapping.BuildNewContext(contextid, userId);
                MutatorFunc(matchingcontext);
                dbContext.ContextMappings.AddRange(matchingcontext);
                await dbContext.SaveChangesAsync();

                return(BuildSuccessResponse(matchingcontext));
            }
            // Apply the update, and save!
            else
            {
                MutatorFunc(matchingcontext);
                dbContext.ContextMappings.Update(matchingcontext);
                await dbContext.SaveChangesAsync();

                return(BuildSuccessResponse(matchingcontext));
            }
        }
        private async Task <IList <ContextMapping> > DetectNewContexts(IList <GenericTodoEvent> events, string userId)
        {
            // Gather all of the current context data, in order to determine if we have any new contexts implied in the events we were just passed.
            List <ContextMapping> currentContexts = await dbContext.ContextMappings.Where(e => e.UserId.Equals(userId)).ToListAsync();

            HashSet <string> existingContextIds = currentContexts.Select(c => c.Id).ToHashSet();

            return(events.Where(e => !existingContextIds.Contains(e.Context)).Select(e => e.Context).ToHashSet().Select(s => ContextMapping.BuildNewContext(s, userId)).ToList());
        }