Пример #1
0
        private Route CreateModel(Route route, RouteBindingModel model, UrskiyPeriodDatabase context)
        {
            route = CreateModel(route, model);

            if (model.Id.HasValue)
            {
                var routeReserve = context.RouteReserves.Where(rec =>
                                                               rec.RouteId == model.Id.Value).ToList();
                // удаляем те, которых нет в модели
                context.RouteReserves.RemoveRange(routeReserve.Where(rec =>
                                                                     !model.RouteReverces.ContainsKey(rec.ReserveId)).ToList());
                context.SaveChanges();

                foreach (var Reserve in routeReserve)
                {
                    model.RouteReverces.Remove(Reserve.ReserveId);
                }
            }

            foreach (var routeReserve in model.RouteReverces)
            {
                context.RouteReserves.Add(new RouteReserve
                {
                    ReserveId = routeReserve.Key,
                    RouteId   = route.Id
                });
            }
            context.SaveChanges();
            return(route);
        }
 public List <ReserveViewModel> GetFullList()
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Reserves.Include(x => x.Payment).Select(CreateModel).ToList());
     }
 }
Пример #3
0
 public void Update(RouteBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 var route = context.Routes.FirstOrDefault(rec => rec.Id == model.Id);
                 if (route == null)
                 {
                     throw new Exception("Маршрут не найден");
                 }
                 CreateModel(route, model, context);
                 context.SaveChanges();
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Пример #4
0
        private CostItem CreateModel(CostItem costItem, CostItemBindingModel model, UrskiyPeriodDatabase context)
        {
            costItem = CreateModel(costItem, model);

            if (model.Id.HasValue)
            {
                var routeCost = context.CostItemRoutes.Where(rec =>
                                                             rec.CostItemId == model.Id.Value).ToList();
                // удаляем те, которых нет в модели
                context.CostItemRoutes.RemoveRange(routeCost.Where(rec =>
                                                                   !model.CostItemRoute.ContainsKey(rec.RouteId)).ToList());
                context.SaveChanges();

                foreach (var Reserve in routeCost)
                {
                    model.CostItemRoute.Remove(Reserve.RouteId);
                }
            }

            foreach (var cost in model.CostItemRoute)
            {
                context.CostItemRoutes.Add(new CostItemRoute
                {
                    CostItemId = costItem.Id,
                    RouteId    = cost.Key
                });
            }
            context.SaveChanges();
            return(costItem);
        }
Пример #5
0
 public List <PaymentViewModel> GetFullList()
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Payment.Select(CreateModel).ToList());
     }
 }
Пример #6
0
 public void Update(CostItemBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 var costItem = context.CostItem.FirstOrDefault(rec => rec.Id == model.Id);
                 if (costItem == null)
                 {
                     throw new Exception("Не найдено");
                 }
                 CreateModel(costItem, model, context);
                 context.SaveChanges();
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Пример #7
0
        public void Insert(RouteBindingModel model)
        {
            using (var context = new UrskiyPeriodDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        if (context.Routes.FirstOrDefault(rec => rec.Name == model.Name) != null)
                        {
                            return;
                        }

                        Route route = CreateModel(new Route(), model);
                        context.Routes.Add(route);
                        context.SaveChanges();
                        CreateModel(route, model, context);

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Пример #8
0
 public int Count()
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.CostItem.Count());
     }
 }
Пример #9
0
 public void Insert(UserBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         context.Users.Add(CreateModel(new User(), model));
         context.SaveChanges();
     }
 }
Пример #10
0
 public void Insert(PaymentBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         context.Payment.Add(CreateModel(new Payment(), model));
         context.SaveChanges();
     }
 }
Пример #11
0
 public List <RouteViewModel> GetFullList()
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Routes.Include(x => x.User).Include(x => x.RouteReserve).ThenInclude(x => x.Reserve)
                .ThenInclude(x => x.Payment)
                .Include(x => x.CostItemRoute).ThenInclude(x => x.CostItem)
                .Select(CreateModel).ToList());
     }
 }
Пример #12
0
 public List <UserViewModel> GetFilteredList(UserBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Users.Where(rec => rec.Login == model.Login).Select(CreateModel).ToList());
     }
 }
Пример #13
0
 public List <PaymentViewModel> GetFilteredList(PaymentBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Payment.Where(rec => rec.Sum == model.Sum).Select(CreateModel).ToList());
     }
 }
 public List <ReserveViewModel> GetFilteredList(ReserveBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Reserves.Include(x => x.Payment)
                .Where(rec => rec.RouteReserve.Select(x => x.RouteId).Contains(model.RouteId)).Select(CreateModel).ToList());
     }
 }
 public void Insert(ReserveBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         if (context.Reserves.FirstOrDefault(rec => rec.Name == model.Name) != null)
         {
             return;
         }
         context.Reserves.Add(CreateModel(new Reserve(), model));
         context.SaveChanges();
     }
 }
Пример #16
0
 public PaymentViewModel GetElement(PaymentBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         var route = context.Payment.FirstOrDefault(rec => rec.Id == model.Id);
         return(route != null?CreateModel(route) : null);
     }
 }
Пример #17
0
 public void Update(UserBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var user = context.Users.FirstOrDefault(rec => rec.Id == model.Id);
         if (user == null)
         {
             throw new Exception("Клиент не найден");
         }
         CreateModel(user, model);
         context.SaveChanges();
     }
 }
 public void Delete(ReserveBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var user = context.Reserves.FirstOrDefault(rec => rec.Id == model.Id);
         if (user == null)
         {
             throw new Exception("Заповедник не найден");
         }
         context.Reserves.Remove(user);
         context.SaveChanges();
     }
 }
 public ReserveViewModel GetElement(ReserveBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         var route = context.Reserves.Include(x => x.Payment)
                     .FirstOrDefault(rec => rec.Id == model.Id || rec.Name == model.Name);
         return(route != null?CreateModel(route) : null);
     }
 }
Пример #20
0
 public void Delete(RouteBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var route = context.Routes.FirstOrDefault(rec => rec.Id == model.Id);
         if (route == null)
         {
             throw new Exception("Маршрут не найден");
         }
         context.Routes.Remove(route);
         context.SaveChanges();
     }
 }
Пример #21
0
 public void Delete(PaymentBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var user = context.Payment.FirstOrDefault(rec => rec.Id == model.Id);
         if (user == null)
         {
             throw new Exception("Не найдено");
         }
         context.Payment.Remove(user);
         context.SaveChanges();
     }
 }
Пример #22
0
 public void Update(PaymentBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var payment = context.Payment.FirstOrDefault(rec => rec.Id == model.Id);
         if (payment == null)
         {
             throw new Exception("Не найдено");
         }
         CreateModel(payment, model);
         context.SaveChanges();
     }
 }
Пример #23
0
 public CostItemViewModel GetElement(CostItemBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         var costItem = context.CostItem.Include(x => x.CostItemRoute).ThenInclude(x => x.Route)
                        .FirstOrDefault(rec => rec.Id == model.Id || rec.Name == model.Name);
         return(costItem != null ? new CostItemViewModel
         {
             Id = costItem.Id,
             Name = costItem.Name,
             Sum = costItem.Sum,
             CostItemRoute = costItem.CostItemRoute.ToDictionary(x => x.RouteId, x => x.Route.Name)
         } : null);
     }
 }
Пример #24
0
 public List <RouteViewModel> GetFilteredList(RouteBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Routes.Include(x => x.User).Include(x => x.RouteReserve).ThenInclude(x => x.Reserve)
                .ThenInclude(x => x.Payment)
                .Include(x => x.CostItemRoute).ThenInclude(x => x.CostItem)
                .Where(rec =>
                       // сортируем по клиенту
                       (model.UserId.HasValue && !model.DateFrom.HasValue && model.PickedRoutes == null && model.UserId == rec.UserId) ||
                       // маршруты без статей затрат для заполнения бд
                       (!model.DateFrom.HasValue && model.PickedRoutes == null && !model.UserId.HasValue && model.NoCost.HasValue &&
                        model.NoCost.Value && rec.CostItemRoute.Count == 0))
                .ToList().Select(CreateModel).ToList());
     }
 }
Пример #25
0
 public UserViewModel GetElement(UserBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new UrskiyPeriodDatabase())
     {
         var user = context.Users.FirstOrDefault(rec => rec.Id == model.Id || rec.Email == model.Email ||
                                                 rec.Login == model.Login);
         if (user == null || (model.Password != null && user.Password != model.Password))
         {
             return(null);
         }
         return(CreateModel(user));
     }
 }
Пример #26
0
 public List <RouteViewModel> GetFilteredByPickList(RouteBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Routes.Include(x => x.User).Include(x => x.RouteReserve).ThenInclude(x => x.Reserve)
                .Where(x => model.PickedRoutes.Contains(x.Id)).Select(x => new RouteViewModel
         {
             Reserves = x.RouteReserve.Select(rec => new ReserveViewModel
             {
                 Name = rec.Reserve.Name,
                 Price = rec.Reserve.Price
             }).ToList(),
             Name = x.Name,
             Cost = x.Cost
         }).ToList());
     }
 }
Пример #27
0
 public List <RouteViewModel> GetFilteredByDateList(RouteBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         return(context.Routes.Include(x => x.User).Include(x => x.CostItemRoute).ThenInclude(x => x.CostItem)
                .Where(rec => model.DateFrom.HasValue && model.DateFrom.HasValue &&
                       model.DateFrom.Value.Date <= rec.DateVisit.Date &&
                       rec.DateVisit.Date <= model.DateTo.Value.Date && model.UserId == rec.UserId)
                .Select(x => new RouteViewModel
         {
             CostItems = x.CostItemRoute.Select(rec => new CostItemViewModel
             {
                 Name = rec.CostItem.Name,
                 Sum = rec.CostItem.Sum
             }).ToList(),
             Name = x.Name,
             Cost = x.Cost,
             DateVisit = x.DateVisit,
             Count = x.Count
         }).ToList());
     }
 }
Пример #28
0
 public void Insert(CostItemBindingModel model)
 {
     using (var context = new UrskiyPeriodDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 CostItem cost = CreateModel(new CostItem(), model);
                 context.CostItem.Add(cost);
                 context.SaveChanges();
                 CreateModel(cost, model, context);
                 context.SaveChanges();
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }