public static async Task FinishSketchReactionAsync(int id, ReactionDTO reaction) { await Task.Run(() => { using (var db = new ChContext()) { var finishreaction = db.Reactions.Where(r => r.ID == id).First(); finishreaction.ClosureDate = reaction.ClosureDate; finishreaction.ProcedureText = reaction.Procedure; finishreaction.Yield = reaction.Yield; finishreaction.Observation = reaction.Observation; foreach (var item in reaction.ObservationImgs) { var tmp = new ObservationImg { img = item, Reaction = finishreaction }; db.ObservationImgs.Add(tmp); finishreaction.ObservationImgs.Add(tmp); } finishreaction.Sketch = false; db.SaveChanges(); } }); }
public static async Task AddReactionAsync(ReactionDTO reactionDTO) { await Task.Run(() => { using (var db = new ChContext()) { var reaction = reactionDTO.TransformToReaction(); reaction.Chemist = db.People.Where(p => p.Name == reactionDTO.Chemist).First(); reaction.Chiefchemist = db.People.Where(p => p.Name == reactionDTO.Chiefchemist).First(); reaction.Project = db.Projects.Where(p => p.Name == reactionDTO.Project).First(); if (!string.IsNullOrEmpty(reactionDTO.PreviousStep)) { reaction.PreviousStep = db.Reactions.Where(re => re.ReactionCode == reactionDTO.PreviousStep).First(); } if (!reaction.Sketch.Value) { foreach (var item in reactionDTO.ObservationImgs) { var tmp = new ObservationImg { img = item, Reaction = reaction }; db.ObservationImgs.Add(tmp); reaction.ObservationImgs.Add(tmp); } } var tmpsm = reactionDTO.StartingMaterial.TransformToStartingMaterial(); tmpsm.Reaction = reaction; db.StartingMaterials.Add(tmpsm); reaction.StartingMaterials.Add(tmpsm); foreach (var item in reactionDTO.Reagents) { var tmp = item.TransformToReagent(); tmp.Reaction = reaction; db.Reagents.Add(tmp); reaction.Reagents.Add(tmp); } foreach (var item in reactionDTO.Solvents) { var tmp = item.TransformToSolvent(); tmp.Reaction = reaction; db.Solvents.Add(tmp); reaction.Solvents.Add(tmp); } foreach (var item in reactionDTO.Products) { var tmp = item.TransformToProduct(); tmp.Reaction = reaction; db.Products.Add(tmp); reaction.Products.Add(tmp); } db.Reactions.Add(reaction); db.SaveChanges(); //var sm1 = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).ToList(); var sm = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).First(); if (sm.v.HasValue) { sm.v = sm.v - reactionDTO.StartingMaterial.VValue; } else { sm.m = sm.m - reactionDTO.StartingMaterial.mValue; } db.SaveChanges(); LocationMolecule r = new LocationMolecule(); //LocationMolecule r2 = new LocationMolecule(); foreach (var item in reactionDTO.Reagents) { //var r2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); r = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); if (r.v.HasValue) { r.v = r.v - item.VValue; } else { r.m = r.m - item.mValue; } db.SaveChanges(); } LocationMolecule s = new LocationMolecule(); foreach (var item in reactionDTO.Solvents) { //var s2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); s = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); s.v = s.v - item.VValue; db.SaveChanges(); } } }); }