Example #1
0
        //CREATE
        public async Task <SamplePlanModel> CreateSamplePlan(SamplePlanModel aSamplePlan)
        {
            using (var transaction = await Context.Database.BeginTransactionAsync())
            {
                var newSamplePlanId = (await Context.SamplePlanHead.MaxAsync(s => s.SamplePlanId)) + 1;

                await Context.SamplePlanHead.AddAsync(aSamplePlan.ToEntity(newSamplePlanId));

                await Context.SamplePlanLevel.AddRangeAsync(aSamplePlan.SamplePlanLevelModels.ToEntities(newSamplePlanId));

                var tempSamplePlanRejectEntities = new List <SamplePlanReject>();
                foreach (var levelModel in aSamplePlan.SamplePlanLevelModels)
                {
                    await Context.AddRangeAsync(levelModel.SamplePlanRejectModels.ToEntities(newSamplePlanId));
                }

                await Context.SaveChangesAsync();

                await transaction.CommitAsync();

                //Goes back to the database and returns the fully hydrated Sample Plan it just created.  This is done so the user on the front-end can verify that the data got into the database correctly.
                return((await Context.SamplePlanHead.Where(i => i.SamplePlanId == newSamplePlanId)
                        .Include(h => h.SamplePlanLevel)
                        .ThenInclude(l => l.SamplePlanReject)
                        .ThenInclude(r => r.InspectTest).FirstOrDefaultAsync()).ToHydratedModel());;
            }
        }
Example #2
0
        public async Task <int> CreateNewSpec(SpecModel aSpecModel)
        {
            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                var theNewSpecId = await context.Specification.MaxAsync(i => i.SpecId) + 1;

                short theNewRevId = 10; //All new specs start with a revId of 10
                if (aSpecModel.SpecRevModels == null)
                {
                    throw new Exception("Cannot create a new Specification without a Revision.");
                }
                if (aSpecModel.SpecRevModels.Count() > 1)
                {
                    throw new Exception("Cannot save a new Specification with multiple revisions; only one revision is allowed.");
                }
                var theSpecRevModel     = aSpecModel.SpecRevModels.FirstOrDefault(); //Only ONE revision can be passed in.
                var theSubLevelEntities = theSpecRevModel.SubLevels.ToEntities(theNewSpecId, theNewRevId);

                await context.Specification.AddAsync(aSpecModel.ToEntity(theNewSpecId));

                await context.SpecificationRevision.AddAsync(theSpecRevModel.ToEntity(theNewSpecId, theNewRevId));

                await context.SpecSubLevel.AddRangeAsync(theSubLevelEntities);

                foreach (var subLevel in theSpecRevModel.SubLevels)
                {
                    var choicesToAdd = subLevel.Choices.ToEntities(theNewSpecId, theNewRevId).ToList();
                    foreach (var choice in choicesToAdd)
                    {
                        choice.OnlyValidForChoice = null;
                    }
                    await context.AddRangeAsync(choicesToAdd); //Problem is here and it happens immediatly!!! <--- TODO?
                }

                await context.SaveChangesAsync();

                //Updating entities with dependents
                foreach (var subLevel in theSpecRevModel.SubLevels)
                {
                    var originalChoices = subLevel.Choices.ToList();
                    var choicesToUpdate = await context.SpecChoice.Where(i => i.SpecId == theNewSpecId && i.SpecRevId == theNewRevId && i.SubLevelSeqId == subLevel.LevelSeq).ToListAsync();

                    foreach (var choice in choicesToUpdate)
                    {
                        choice.OnlyValidForChoice = originalChoices.FirstOrDefault(i => i.ChoiceSeqId == choice.ChoiceSeqId).OnlyValidForChoiceId;
                    }

                    context.UpdateRange(choicesToUpdate);
                    await context.SaveChangesAsync();
                }


                //Default choice in the subLevel has to be null when saving the data for the first time to prevent an issue with circular dependency.  They must be updated after the first save.
                foreach (var subLevel in theSubLevelEntities)
                {
                    subLevel.DefaultChoice = theSpecRevModel.SubLevels.FirstOrDefault(i => i.LevelSeq == subLevel.SubLevelSeqId).DefaultChoice;
                }

                context.UpdateRange(theSubLevelEntities);

                await context.SaveChangesAsync();

                await transaction.CommitAsync();

                return(theNewSpecId);
            }
        }