Пример #1
0
        public void Delete(PetBindingModel model)
        {
            using (var context = new VetClinicDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Pet element = context.Pets.FirstOrDefault(rec => rec.Id == model.Id.Value);

                        if (element != null)
                        {
                            context.ClientPets.RemoveRange(
                                context.ClientPets.Where(
                                    rec => rec.PetId == model.Id.Value));
                            context.Pets.Remove(element);
                            context.SaveChanges();
                        }
                        else
                        {
                            throw new Exception("Элемент не найден");
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Пример #2
0
        public void CreateOrUpdate(PetBindingModel model)
        {
            using (var context = new VetClinicDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Pet element = model.Id.HasValue ? null : new Pet();
                        if (model.Id.HasValue)
                        {
                            element = context.Pets.FirstOrDefault(rec => rec.ClientId ==
                                                                  model.ClientId && rec.PetName == model.PetName);
                            if (element == null)
                            {
                                throw new Exception("Такой питомец уже существует");
                            }
                            element.PetName  = model.PetName;
                            element.Kind     = model.Kind;
                            element.Breed    = model.Breed;
                            element.Age      = model.Age;
                            element.Gender   = model.Gender;
                            element.ClientId = model.ClientId;
                            context.SaveChanges();
                        }
                        else
                        {
                            element.PetName  = model.PetName;
                            element.Kind     = model.Kind;
                            element.Breed    = model.Breed;
                            element.Age      = model.Age;
                            element.Gender   = model.Gender;
                            element.ClientId = model.ClientId;
                        }
                        context.Pets.Add(element);
                        context.SaveChanges();

                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Пример #3
0
 public List <PetViewModel> Read(PetBindingModel model)
 {
     using (var context = new VetClinicDatabase())
     {
         return(context.Pets
                .Where(rec => model == null ||
                       rec.Id == model.Id ||
                       (rec.ClientId == model.ClientId))
                .Select(rec => new PetViewModel
         {
             Id = rec.Id,
             ClientId = rec.ClientId,
             Kind = rec.Kind,
             PetName = rec.PetName,
             Breed = rec.Breed,
             Age = rec.Age,
             Gender = rec.Gender,
             ClientPets = GetClientPetViewModel(rec)
         })
                .ToList());
     }
 }