public Bank CreateBank(Bank bank) { using (var database = new Core.Entity.SeedsEntities()) { var entity = bank.ToEntity(database); database.AddToBank(entity); database.SaveChanges(); var result = new Bank(database.Bank.Execute(MergeOption.NoTracking).Single(b => b.ID == entity.ID)); Cache.Instance.Banks[result.ID] = result; return result; } }
public Plant CreatePlant(Plant plant) { using (var database = new Core.Entity.SeedsEntities()) { var entity = plant.ToEntity(database); entity.ID = database.Plant.Execute(MergeOption.NoTracking).Any() ? database.Plant.Execute(MergeOption.NoTracking).Max(p => p.ID) + 1 : 0; database.AddToPlant(entity); database.SaveChanges(); var result = new Plant(database.Plant.Execute(MergeOption.NoTracking).Single(p => p.ID == entity.ID), true); result.UpdateParent(); Cache.Instance.Plants[result.ID] = result; return result; } }
public Employee CreateEmployee(Employee employee) { using (var database = new Core.Entity.SeedsEntities()) { var entity = employee.ToEntity(database); entity.ID = database.Employee.Execute(MergeOption.NoTracking).Any() ? database.Employee.Execute(MergeOption.NoTracking).Max(e => e.ID) + 1 : 0; long subjectID = employee.Subject.ID; entity.SubjectID = subjectID; database.AddToEmployee(entity); database.SaveChanges(); employee = new Employee(database.Employee.Execute(MergeOption.NoTracking).First(e => e.ID == entity.ID)); employee.Subject = Cache.Instance.Subjects[subjectID]; employee.Subject.Employees[employee.ID] = employee; return employee; } }
public bool ChangePassword(long userId, string oldPassword, string newPassword) { using (var database = new Core.Entity.SeedsEntities()) { var user = database.Employee.FirstOrDefault(e => e.ID == userId); if (user == null) return false; if (user.Password != oldPassword) return false; user.Password = newPassword; try { database.SaveChanges(); Cache.Instance.Subjects[user.SubjectID].Employees[user.ID].Password = newPassword; return true; } catch { return false; } } }
public void DeleteSampleSelectionRequest(long id) { using (var database = new Core.Entity.SeedsEntities()) { var document = database.SampleSelectionRequest.First(doc => doc.DocumentID == id); // Здесь не нужна проверка на даты, потому что заказ может быть проплачен заранее if (!document.HeaderReference.IsLoaded) { document.HeaderReference.Load(); } if (document.Header.StateEnum == DocumentStateEnum.Active) { document.Header.StateEnum = DocumentStateEnum.Deleted; } else { throw new Exception("Нельзя удалить неактуальную версию или уже удалённый документ."); } database.SaveChanges(); } }
public Subject EditSubject(Subject subject) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.Subject.Single(s => s.ID == subject.ID); if (!entity.Employees.IsLoaded) { entity.Employees.Load(); } subject.ApplyChanges(database, entity); database.SaveChanges(); var result = new Subject(entity); Cache.Instance.Subjects[result.ID] = result; return result; } }
public SampleSelectionRequest EditSampleSelectionRequest(SampleSelectionRequest document) { using (var database = new Core.Entity.SeedsEntities()) { var entity = document.ToEntity(database); entity.Header.StateEnum = DocumentStateEnum.Active; entity.Header.PreviousVersionID = document.ID; entity.Header.CreationDate = DateTime.Now; // потому что иначе в значение пойдёт DateTime.MinVaue database.AddToSampleSelectionRequest(entity); var oldDocument = database.DocumentHeader.First(d => d.ID == document.ID); if (oldDocument.StateEnum == DocumentStateEnum.Active) { oldDocument.StateEnum = DocumentStateEnum.Edited; } database.SaveChanges(); long newId = entity.DocumentID; return new SampleSelectionRequest(database.SampleSelectionRequest.Execute(MergeOption.NoTracking).Single(doc => doc.DocumentID == newId)); } }
public ReproductionStep EditReproductionStep(ReproductionStep reproductionStep) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.ReproductionStep.Single(e => e.ID == reproductionStep.ID); reproductionStep.ApplyChanges(database, entity); database.SaveChanges(); var result = new ReproductionStep(entity); Cache.Instance.Plants[result.Plant.ID].ReproductionSteps[result.ID] = result; return result; } }
public PlantVariety EditPlantVariety(PlantVariety plantVariety) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.PlantVariety.Single(e => e.ID == plantVariety.ID); plantVariety.ApplyChanges(database, entity); database.SaveChanges(); var result = new PlantVariety(entity); Cache.Instance.Plants[result.Plant.ID].Varieties[result.ID] = result; return result; } }
public Plant EditPlant(Plant plant) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.Plant.Single(p => p.ID == plant.ID); plant.ApplyChanges(database, entity); database.SaveChanges(); var result = new Plant(entity, true, true); Cache.Instance.Plants[result.ID] = result; return result; } }
public AnalyzePriceList CreatePriceList(AnalyzePriceList priceList) { using (var database = new Core.Entity.SeedsEntities()) { var entity = priceList.ToEntity(database); database.AddToAnalyzePriceList(entity); database.SaveChanges(); var result = new AnalyzePriceList(database.AnalyzePriceList.Execute(MergeOption.NoTracking).Single(pl => pl.ID == entity.ID), true); Cache.Instance.PriceLists[result.ID] = result; return result; } }
public ReproductionStep CreateReproductionStep(ReproductionStep reproductionStep) { using (var database = new Core.Entity.SeedsEntities()) { var entity = reproductionStep.ToEntity(database); entity.ID = database.ReproductionStep.Execute(MergeOption.NoTracking).Any() ? database.ReproductionStep.Execute(MergeOption.NoTracking).Max(rs => rs.ID) + 1 : 0; database.AddToReproductionStep(entity); database.SaveChanges(); var newEntity = database.ReproductionStep.Execute(MergeOption.NoTracking).Single(rs => rs.ID == entity.ID); var result = new ReproductionStep(newEntity); var plant = Cache.Instance.Plants[newEntity.PlantID]; result.Plant = plant; plant.ReproductionSteps[result.ID] = result; return result; } }
public List<AnalyzePrice> UploadPrices(long priceListId, List<AnalyzePrice> prices) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.AnalyzePriceList.Single(pl => pl.ID == priceListId); if (!entity.Prices.IsLoaded) { entity.Prices.Load(); } entity.Prices.Clear(); foreach (var price in prices) { entity.Prices.Add(price.ToEntity(database)); } database.SaveChanges(); var priceList = new AnalyzePriceList(database.AnalyzePriceList.Execute(MergeOption.NoTracking).First(pl => pl.ID == priceListId), true); Cache.Instance.PriceLists[priceListId] = priceList; return priceList.Prices.ToList(); } }
public void DeleteReproductionSteps(List<long> ids) { var plants = new Dictionary<long, List<long>>(); using (var database = new Core.Entity.SeedsEntities()) { foreach (var reproductionStep in database.ReproductionStep.Where(e => ids.Contains(e.ID))) { List<long> plant; if (!plants.TryGetValue(reproductionStep.PlantID, out plant)) { plant = new List<long>(); } plant.Add(reproductionStep.ID); database.ReproductionStep.DeleteObject(reproductionStep); } database.SaveChanges(); foreach (long plantId in plants.Keys) { var plant = Cache.Instance.Plants[plantId]; foreach (long reproductionStepId in plants[plantId]) { plant.ReproductionSteps.Remove(reproductionStepId); } } } }
public void DeleteEmployees(List<long> ids) { var subjects = new Dictionary<long, List<long>>(); using (var database = new Core.Entity.SeedsEntities()) { foreach (var employee in database.Employee.Where(e => ids.Contains(e.ID))) { List<long> subject; if (!subjects.TryGetValue(employee.SubjectID, out subject)) { subject = new List<long>(); } subject.Add(employee.ID); database.Employee.DeleteObject(employee); } database.SaveChanges(); foreach (long subjectId in subjects.Keys) { var subject = Cache.Instance.Subjects[subjectId]; foreach (long employeeId in subjects[subjectId]) { subject.Employees.Remove(employeeId); } } } }
public Subject CreateSubject(Subject subject) { using (var database = new Core.Entity.SeedsEntities()) { var entity = subject.ToEntity(database); database.AddToSubject(entity); database.SaveChanges(); var result = new Subject(database.Subject.Execute(MergeOption.NoTracking).Single(s => s.ID == entity.ID)); result.UpdateParent(); Cache.Instance.Subjects[result.ID] = result; return result; } }
public SampleSelectionRequest CreateSampleSelectionRequest(SampleSelectionRequest document) { using (var database = new Core.Entity.SeedsEntities()) { var entity = document.ToEntity(database); entity.Header.CreationDate = DateTime.Now; // потому что иначе в значение пойдёт DateTime.MinVaue database.AddToSampleSelectionRequest(entity); database.SaveChanges(); return new SampleSelectionRequest(database.SampleSelectionRequest.Execute(MergeOption.NoTracking).Single(doc => doc.DocumentID == entity.DocumentID)); } }
public Bank EditBank(Bank bank) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.Bank.Single(b => b.ID == bank.ID); bank.ApplyChanges(database, entity); database.SaveChanges(); var result = new Bank(entity); Cache.Instance.Banks[result.ID] = result; return result; } }
public Employee EditEmployee(Employee employee) { using (var database = new Core.Entity.SeedsEntities()) { var entity = database.Employee.Single(e => e.ID == employee.ID); employee.ApplyChanges(database, entity); database.SaveChanges(); var result = new Employee(entity); Cache.Instance.Subjects[result.Subject.ID].Employees[result.ID] = result; return result; } }
public PlantVariety CreatePlantVariety(PlantVariety plantVariety) { using (var database = new Core.Entity.SeedsEntities()) { var entity = plantVariety.ToEntity(database); entity.ID = database.PlantVariety.Execute(MergeOption.NoTracking).Any() ? database.PlantVariety.Execute(MergeOption.NoTracking).Max(pv => pv.ID) + 1 : 0; database.AddToPlantVariety(entity); database.SaveChanges(); var newEntity = database.PlantVariety.Execute(MergeOption.NoTracking).Single(v => v.ID == entity.ID); var result = new PlantVariety(newEntity); var plant = Cache.Instance.Plants[newEntity.PlantID]; result.Plant = plant; plant.Varieties[result.ID] = result; return result; } }
public Position CreatePosition(Position position) { using (var database = new Core.Entity.SeedsEntities()) { var entity = position.ToEntity(database); entity.ID = database.Position.Execute(MergeOption.NoTracking).Any() ? database.Position.Execute(MergeOption.NoTracking).Max(p => p.ID) + 1 : 0; database.AddToPosition(entity); database.SaveChanges(); var newEntity = database.Position.Execute(MergeOption.NoTracking).Single(p => p.ID == entity.ID); var result = new Position(newEntity); Cache.Instance.Positions[result.ID] = result; return result; } }