Esempio n. 1
0
        private Request CreateModel(RequestBindingModel model, Request request, CTODatabase context)
        {
            request.ClientId    = model.ClientId;
            request.RequestName = model.RequestName;
            request.RequestCost = model.RequestCost;
            request.DateFrom    = model.DateFrom;
            request.DateTo      = model.DateTo;

            if (model.Id.HasValue)
            {
                var requestWorks = context.RequestWorks.Where(rec => rec.RequestId == model.Id.Value).ToList();
                context.RequestWorks.RemoveRange(requestWorks.Where(rec => !model.RequestWorks.ContainsKey(rec.WorkId)).ToList());
                context.SaveChanges();
                foreach (var updateComponent in requestWorks)
                {
                    model.RequestWorks.Remove(updateComponent.WorkId);
                }
                context.SaveChanges();
            }
            foreach (var pc in model.RequestWorks)
            {
                context.RequestWorks.Add(new RequestWork
                {
                    RequestId = request.Id,
                    WorkId    = pc.Key,
                });
                context.SaveChanges();
            }

            return(request);
        }
Esempio n. 2
0
 public void Update(RequestBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 var element = context.Requests.Include(rec => rec.RequestWorks)
                               .ThenInclude(rec => rec.Work).FirstOrDefault(rec => rec.Id == model.Id);
                 if (element == null)
                 {
                     throw new Exception("Заявка не найдена");
                 }
                 CreateModel(model, element, context);
                 context.SaveChanges();
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Esempio n. 3
0
 public void Insert(WorkBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         context.Works.Add(CreateModel(model, new Work()));
         context.SaveChanges();
     }
 }
Esempio n. 4
0
 public void Insert(CostBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         context.Costs.Add(CreateModel(model, new Cost()));
         context.SaveChanges();
     }
 }
Esempio n. 5
0
 public void Update(WorkBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         var element = context.Works.FirstOrDefault(rec => rec.Id == model.Id);
         if (element == null)
         {
             throw new Exception("Такого ремонта нет");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Esempio n. 6
0
 public void Update(CostBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         var element = context.Costs.FirstOrDefault(rec => rec.Id == model.Id);
         if (element == null)
         {
             throw new Exception("Статья затрат не найдена");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Esempio n. 7
0
 public void Update(WorkerBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         var element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id);
         if (element == null)
         {
             throw new Exception("Сотрудник не найден");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Esempio n. 8
0
 public void Delete(WorkBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         Work element = context.Works.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Works.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Такого ремонта нет");
         }
     }
 }
Esempio n. 9
0
 public void Delete(CostBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         Cost element = context.Costs.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Costs.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Статья затрат не найдена");
         }
     }
 }
Esempio n. 10
0
 public void Delete(WorkerBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         Worker element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Workers.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Сотрудник не найден");
         }
     }
 }
Esempio n. 11
0
 public void Delete(RequestBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         Request element = context.Requests.Include(rec => rec.RequestWorks)
                           .ThenInclude(rec => rec.Work).FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Requests.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Заявка не найдена");
         }
     }
 }
Esempio n. 12
0
 public void Insert(RequestBindingModel model)
 {
     using (var context = new CTODatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 Request request = CreateModel(model, new Request());
                 context.Requests.Add(request);
                 context.SaveChanges();
                 CreateModel(model, request, context);
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }