Exemplo n.º 1
0
        public void CreateOrUpdate(ProviderBindingModel model)
        {
            using (var context = new GoToWorkDatabase())
            {
                Provider element = context.Providers.FirstOrDefault(rec => rec.Email == model.Email && rec.Id != model.Id);

                if (element != null)
                {
                    throw new Exception("Уже есть поставщик с такой почтой");
                }

                if (model.Id.HasValue)
                {
                    element = context.Providers.FirstOrDefault(rec => rec.Id == model.Id);

                    if (element == null)
                    {
                        throw new Exception("Элемент не найден");
                    }
                }
                else
                {
                    element = new Provider();
                    context.Providers.Add(element);
                }

                element.FIO      = model.FIO;
                element.Password = model.Password;
                element.Email    = model.Email;

                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public void Delete(ToyBindingModel model)
        {
            using (var context = new GoToWorkDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        context.ToyParts.RemoveRange(context.ToyParts.Where(rec => rec.ToyId == model.Id));
                        Toy element = context.Toys.FirstOrDefault(rec => rec.Id == model.Id);

                        if (element != null)
                        {
                            context.Toys.Remove(element);
                            context.SaveChanges();
                        }
                        else
                        {
                            throw new Exception("Элемент не найден");
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemplo n.º 3
0
 protected override List <T> GetList <T>()
 {
     using (var context = new GoToWorkDatabase())
     {
         return(context.Set <T>().ToList());
     }
 }
Exemplo n.º 4
0
 protected override List <PropertyInfo> GetFullList()
 {
     using (var context = new GoToWorkDatabase())
     {
         Type type = context.GetType();
         return(type.GetProperties().Where(x =>
                                           x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")).ToList());
     }
 }
Exemplo n.º 5
0
        public void CreateOrUpdate(ToyBindingModel model)
        {
            using (var context = new GoToWorkDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Toy element = context.Toys.FirstOrDefault(rec => rec.ToyName == model.ToyName && rec.Id != model.Id);
                        if (element != null)
                        {
                            throw new Exception("Уже есть игрушка с таким названием");
                        }
                        if (model.Id.HasValue)
                        {
                            element = context.Toys.FirstOrDefault(rec => rec.Id == model.Id);
                            if (element == null)
                            {
                                throw new Exception("Элемент не найден");
                            }
                        }
                        else
                        {
                            element = new Toy();
                            context.Toys.Add(element);
                        }
                        element.ToyName    = model.ToyName;
                        element.DateCreate = DateTime.Now;
                        context.SaveChanges();

                        if (model.Id.HasValue)
                        {
                            var ToyParts = context.ToyParts.Where(rec => rec.ToyId == model.Id.Value).ToList();
                        }
                        foreach (var pc in model.ToyParts)
                        {
                            context.ToyParts.Add(new ToyParts
                            {
                                ToyId     = element.Id,
                                PartId    = pc.Key,
                                PartCount = pc.Value.Item3
                            });
                            context.SaveChanges();
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemplo n.º 6
0
 public List <ToyViewModel> Read(ToyBindingModel model)
 {
     using (var context = new GoToWorkDatabase())
     {
         return(context.Toys.Where(rec => model == null || rec.Id == model.Id).ToList().Select(rec => new ToyViewModel
         {
             Id = rec.Id,
             ToyName = rec.ToyName,
             DateCreate = rec.DateCreate,
             ToyParts = context.ToyParts.Include(recPC => recPC.Part).Where(recPC => recPC.ToyId == rec.Id).ToDictionary(recPC => recPC.PartId, recPC => (recPC.Part?.PartType, recPC.Part?.PartColor, recPC.PartCount))
         }).ToList());
Exemplo n.º 7
0
        public void Delete(ProviderBindingModel model)
        {
            using (var context = new GoToWorkDatabase())
            {
                Provider element = context.Providers.FirstOrDefault(rec => rec.Id == model.Id);

                if (element != null)
                {
                    context.Providers.Remove(element);
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Элемент не найден");
                }
            }
        }
Exemplo n.º 8
0
 public List <ProviderViewModel> Read(ProviderBindingModel model)
 {
     using (var context = new GoToWorkDatabase())
     {
         return(context.Providers
                .Where(
                    rec => model == null ||
                    rec.Id == model.Id ||
                    rec.Email == model.Email && rec.Password == model.Password
                    )
                .Select(rec => new ProviderViewModel
         {
             Id = rec.Id,
             FIO = rec.FIO,
             Email = rec.Email,
             Password = rec.Password
         })
                .ToList());
     }
 }