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; } } } }
public void CreateOrUpdate(ReceptionBindingModel model) { using (var context = new VetClinicDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Reception element = model.Id.HasValue ? null : new Reception(); if (model.Id.HasValue) { element = context.Receptions.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.TotalSum = model.TotalSum; element.ReceptionStatus = model.ReceptionStatus; context.SaveChanges(); } else { element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.TotalSum = model.TotalSum; element.ReceptionStatus = model.ReceptionStatus; context.Receptions.Add(element); context.SaveChanges(); var groupServices = model.ReceptionServices .GroupBy(rec => rec.ServiceId) .Select(rec => new { ServiceId = rec.Key, Count = rec.Sum(r => r.Count) }); foreach (var groupService in groupServices) { context.ReceptionServices.Add(new ReceptionService { ReceptionId = element.Id, ServiceId = groupService.ServiceId, Count = groupService.Count }); context.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
protected override List <T> GetList <T>() { using (var context = new VetClinicDatabase()) { return(context.Set <T>().ToList()); } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new VetClinicDatabase()) { Client element = model.Id.HasValue ? null : new Client(); 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.Email = model.Email; element.Login = model.Login; element.Phone = model.Phone; element.Block = model.Block; element.Password = model.Password; context.SaveChanges(); } }
public void CreateOrUpdate(PaymentBindingModel model) { using (var context = new VetClinicDatabase()) { Payment element = model.Id.HasValue ? null : new Payment(); if (model.Id.HasValue) { element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Payment(); context.Payments.Add(element); } element.ReceptionId = model.ReceptionId; element.ClientId = model.ClientId; element.Sum = model.Sum; element.DatePayment = model.DatePayment; context.SaveChanges(); } }
protected override List <PropertyInfo> GetFullList() { using (var context = new VetClinicDatabase()) { Type type = context.GetType(); return(type.GetProperties().Where(x => x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")).ToList()); } }
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; } } } }
public void Delete(ReceptionBindingModel model) { using (var context = new VetClinicDatabase()) { Reception element = context.Receptions.FirstOrDefault(rec => rec.Id == model.Id.Value); if (element != null) { context.Receptions.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public List <PaymentViewModel> Read(PaymentBindingModel model) { using (var context = new VetClinicDatabase()) { return(context.Payments .Where(rec => model == null || rec.Id == model.Id || rec.ReceptionId.Equals(model.ReceptionId)) .Select(rec => new PaymentViewModel { Id = rec.Id, ClientId = rec.ClientId, DatePayment = rec.DatePayment, ReceptionId = rec.ReceptionId, Sum = rec.Sum }) .ToList()); } }
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()); } }
public static List <ClientPetViewModel> GetClientPetViewModel(Pet pet) { using (var context = new VetClinicDatabase()) { var ClientPets = context.ClientPets .Where(rec => rec.PetId == pet.Id) .Include(rec => rec.Client) .Select(rec => new ClientPetViewModel { Id = rec.Id, PetId = rec.PetId, ClientId = rec.ClientId, Count = rec.Count }).ToList(); foreach (var client in ClientPets) { var clientData = context.Clients.Where(rec => rec.Id == client.ClientId).FirstOrDefault(); client.ClientFIO = clientData.ClientFIO; } return(ClientPets); } }
public List <ClientViewModel> Read(ClientBindingModel model) { using (var context = new VetClinicDatabase()) { return(context.Clients .Where(rec => model == null || rec.Id == model.Id || (rec.Login == model.Login || rec.Email == model.Email) && (model.Password == null || rec.Password == model.Password)) .Select(rec => new ClientViewModel { Id = rec.Id, Login = rec.Login, ClientFIO = rec.ClientFIO, Email = rec.Email, Password = rec.Password, Phone = rec.Phone, Block = rec.Block }) .ToList()); } }
public static List <ReceptionServiceViewModel> GetReceptionServiceViewModel(Reception reception) { using (var context = new VetClinicDatabase()) { var ReceptionServices = context.ReceptionServices .Where(rec => rec.ReceptionId == reception.Id) .Include(rec => rec.Service) .Select(rec => new ReceptionServiceViewModel { Id = rec.Id, ReceptionId = rec.ReceptionId, ServiceId = rec.ServiceId, Count = rec.Count }).ToList(); foreach (var service in ReceptionServices) { var serviceData = context.Services.Where(rec => rec.Id == service.ServiceId).FirstOrDefault(); service.ServiceName = serviceData.ServiceName; service.Price = serviceData.Price; } return(ReceptionServices); } }
public void SaveToDatabase() { var services = LoadServices(); using (var context = new VetClinicDatabase()) { foreach (var service in services) { Service element = context.Services.FirstOrDefault(rec => rec.Id == service.Id); if (element != null) { break; } else { element = new Service(); context.Services.Add(element); } element.ServiceName = service.ServiceName; element.Price = service.Price; context.SaveChanges(); } } }
public List <ReceptionViewModel> Read(ReceptionBindingModel model) { using (var context = new VetClinicDatabase()) { return(context.Receptions.Where(rec => rec.Id == model.Id || (rec.ClientId == model.ClientId) && (model.Date == null && model.DateTo == null || rec.DateCreate >= model.Date && rec.DateCreate <= model.DateTo)) .Select(rec => new ReceptionViewModel { Id = rec.Id, ClientId = rec.ClientId, ClientFIO = rec.Client.ClientFIO, TotalSum = rec.TotalSum, DateCreate = rec.DateCreate, LeftSum = rec.TotalSum - context.Payments.Where(recP => recP.ReceptionId == rec.Id).Select(recP => recP.Sum).Sum(), ReceptionStatus = rec.ReceptionStatus, ReceptionServices = GetReceptionServiceViewModel(rec) }) .ToList()); } }