Ejemplo n.º 1
0
        public async Task SubmitPositiveDiagnosisAsync(SelfDiagnosisSubmissionRequest diagnosis)
        {
            using (var ctx = new ExposureNotificationContext(dbContextOptions))
                using (var transaction = ctx.Database.BeginTransaction())
                {
                    // Ensure the database contains the diagnosis uid
                    var dbDiag = await ctx.Diagnoses.FirstOrDefaultAsync(d => d.DiagnosisUid == diagnosis.DiagnosisUid);

                    // Check that the diagnosis uid exists and that there aren't too many keys associated
                    // already, otherwise it might be someone submitting fake data with a legitimate key
                    if (dbDiag == null || dbDiag.KeyCount > maxKeysPerDiagnosisFile)
                    {
                        throw new InvalidOperationException();
                    }

                    var dbKeys = diagnosis.Keys.Select(k => DbTemporaryExposureKey.FromKey(k)).ToList();

                    // Add the new keys to the db
                    foreach (var dbk in dbKeys)
                    {
                        // Only add key if it doesn't exist already
                        if (!await ctx.TemporaryExposureKeys.AnyAsync(k => k.Base64KeyData == dbk.Base64KeyData))
                        {
                            ctx.TemporaryExposureKeys.Add(dbk);
                        }
                    }

                    // Increment key count
                    dbDiag.KeyCount += diagnosis.Keys.Count();

                    await ctx.SaveChangesAsync();

                    await transaction.CommitAsync();
                }
        }
Ejemplo n.º 2
0
        public async Task SubmitPositiveDiagnosisAsync(SelfDiagnosisSubmissionRequest diagnosis)
        {
            using (var ctx = new ExposureNotificationContext(dbContextOptions))
            {
                // Ensure the database contains the diagnosis uid
                if (!ctx.Diagnoses.Any(d => d.DiagnosisUid == diagnosis.DiagnosisUid))
                {
                    throw new InvalidOperationException();
                }

                var dbKeys = diagnosis.Keys.Select(k => DbTemporaryExposureKey.FromKey(k)).ToList();

                foreach (var dbk in dbKeys)
                {
                    ctx.TemporaryExposureKeys.Add(dbk);
                }

                await ctx.SaveChangesAsync();
            }
        }