Exemplo n.º 1
0
        public void CreateOrUpdate(ExcursionBindingModel model)
        {
            using (var context = new ZooDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Excursion elem = model.Id.HasValue ? null : new Excursion();
                        if (model.Id.HasValue)
                        {
                            elem = context.Excursions.FirstOrDefault(rec => rec.Id ==
                                                                     model.Id);
                            if (elem == null)
                            {
                                throw new Exception("Элемент не найден");
                            }
                            elem.ClientId        = model.ClientId;
                            elem.ExcursionCreate = model.ExcursionCreate;
                            elem.Final_Cost      = model.Final_Cost;
                            elem.Status          = model.Status;
                            context.SaveChanges();
                        }
                        else
                        {
                            elem.ClientId        = model.ClientId;
                            elem.ExcursionCreate = model.ExcursionCreate;
                            elem.Final_Cost      = model.Final_Cost;
                            elem.Status          = model.Status;
                            context.Excursions.Add(elem);
                            context.SaveChanges();
                            var routes = model.RouteForExcursions
                                         .GroupBy(rec => rec.RouteId)
                                         .Select(rec => new
                            {
                                RouteId = rec.Key,
                            });

                            foreach (var route in routes)
                            {
                                context.RouteForExcursions.Add(new RouteForExcursion
                                {
                                    ExcursionId = elem.Id,
                                    RouteId     = route.RouteId,
                                });
                                context.SaveChanges();
                            }
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void CreateOrUpdate(ClientBindingModel model)
        {
            using (var context = new ZooDatabase())
            {
                Client element = context.Clients.FirstOrDefault(rec => rec.ClientFIO == model.ClientFIO && rec.Id == model.Id && rec.Number == model.Number && rec.Gender == model.Gender && rec.Score == model.Score && rec.DateCreate == model.DateCreate && rec.CountTicket == model.CountTicket && rec.Email == model.Email);
                if (element != null)
                {
                    throw new Exception("Уже есть такой клиент");
                }
                if (model.Id.HasValue)
                {
                    element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
                    if (element == null)
                    {
                        throw new Exception("Элемент не найден");
                    }
                }
                else
                {
                    element = new Client();
                    context.Clients.Add(element);
                }
                element.ClientFIO   = model.ClientFIO;
                element.Gender      = model.Gender;
                element.Number      = model.Number;
                element.Email       = model.Email;
                element.CountTicket = model.CountTicket;
                element.Score       = model.Score;
                element.DateCreate  = DateTime.Now;
                context.SaveChanges();
                var groupTickets = model.TicketClients
                                   .GroupBy(rec => rec.TicketId)
                                   .Select(rec => new
                {
                    TicketId = rec.Key,
                });

                foreach (var groupTicket in groupTickets)
                {
                    context.TicketClients.Add(new TicketClient
                    {
                        ClientId = element.Id,
                        TicketId = groupTicket.TicketId,
                    });
                    context.SaveChanges();
                }
            }
        }
Exemplo n.º 3
0
 public void CreateOrUpdate(ClientBindingModel model)
 {
     using (var context = new ZooDatabase())
     {
         Client elem = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
         if (model.Id.HasValue)
         {
             elem = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
             if (elem == null)
             {
                 throw new Exception("Элемент не найден");
             }
         }
         else
         {
             elem = new Client();
             context.Clients.Add(elem);
         }
         elem.Login       = model.Login;
         elem.ClientFIO   = model.ClientFIO;
         elem.Password    = model.Password;
         elem.BlockStatus = model.BlockStatus;
         context.SaveChanges();
     }
 }
Exemplo n.º 4
0
 public void CreateOrUpdate(OrderBindingModel model)
 {
     using (var context = new ZooDatabase())
     {
         Order elem = model.Id.HasValue ? null : new Order();
         if (model.Id.HasValue)
         {
             elem = context.Orders.FirstOrDefault(rec => rec.Id ==
                                                  model.Id);
             if (elem == null)
             {
                 throw new Exception("Элемент не найден");
             }
         }
         else
         {
             elem = new Order();
             context.Orders.Add(elem);
         }
         elem.ExcursionId = model.ExcursionId;
         elem.ClientId    = model.ClientId;
         elem.Sum         = model.Sum;
         elem.DateCreate  = model.DateCreate;
         context.SaveChanges();
     }
 }
Exemplo n.º 5
0
        public void SaveToDatabase()
        {
            var workers = LoadWorkers();

            using (var context = new ZooDatabase())
            {
                foreach (var worker in workers)
                {
                    Worker element = context.Workers.FirstOrDefault(rec => rec.Id == worker.Id);
                    if (element != null)
                    {
                        break;
                    }
                    else
                    {
                        element = new Worker();
                        context.Workers.Add(element);
                    }
                    element.WorkerFIO = worker.WorkerFIO;
                    element.Email     = worker.Email;
                    element.Salary    = worker.Salary;
                    context.SaveChanges();
                }
            }
        }
Exemplo n.º 6
0
        public void SaveToDatabase()
        {
            var routes = LoadRoutes();

            using (var context = new ZooDatabase())
            {
                foreach (var route in routes)
                {
                    Route element = context.Routes.FirstOrDefault(rec => rec.Id == route.Id);
                    if (element != null)
                    {
                        break;
                    }
                    else
                    {
                        element = new Route();
                        context.Routes.Add(element);
                    }
                    element.RouteName  = route.RouteName;
                    element.StartRoute = route.StartRoute;
                    element.Cost       = route.Cost;
                    context.SaveChanges();
                }
            }
        }
Exemplo n.º 7
0
 public void Delete(ClientBindingModel model)
 {
     using (var context = new ZooDatabase())
     {
         Client element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Clients.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Элемент не найден");
         }
     }
 }
Exemplo n.º 8
0
        public void Delete(ExcursionBindingModel model)
        {
            using (var context = new ZooDatabase())
            {
                Excursion elem = context.Excursions.FirstOrDefault(rec => rec.Id == model.Id.Value);

                if (elem != null)
                {
                    context.Excursions.Remove(elem);
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Элемент не найден");
                }
            }
        }
Exemplo n.º 9
0
 public void CreateOrUpdate(WorkerBindingModel model)
 {
     using (var context = new ZooDatabase())
     {
         Worker element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id);
         if (model.Id.HasValue)
         {
             element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id);
             if (element == null)
             {
                 throw new Exception("Элемент не найден");
             }
         }
         else
         {
             element = new Worker();
             context.Workers.Add(element);
         }
         element.WorkerFIO = model.WorkerFIO;
         element.Salary    = model.Salary;
         context.SaveChanges();
     }
 }