public static string ImportProcedures(PetClinicContext context, string xmlString) { var serializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures")); var deserializedProcedures = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString)); List <Procedure> procedures = new List <Procedure>(); List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>(); var sb = new StringBuilder(); foreach (var proc in deserializedProcedures) { var vet = context.Vets.SingleOrDefault(v => v.Name == proc.VetName); var animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == proc.AnimalSerialNumber); if (vet == null || animal == null || proc.AnimalAids.Length != proc.AnimalAids.Distinct().Count() || context.AnimalAids.Where(x => proc.AnimalAids.Any(p => p.Name == x.Name)).Count() != proc.AnimalAids.Count()) { sb.AppendLine(ErrorMessage); continue; } var procedure = new Procedure() { Vet = vet, Animal = animal, DateTime = DateTime.ParseExact(proc.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture) }; List <AnimalAid> animalAids = context.AnimalAids.Where(ai => proc.AnimalAids.Any(x => x.Name == ai.Name)).ToList(); foreach (var aid in animalAids) { var procedureAnimalAid = new ProcedureAnimalAid() { AnimalAid = aid, Procedure = procedure }; procedureAnimalAids.Add(procedureAnimalAid); } procedures.Add(procedure); sb.AppendLine(SuccessProcedure); } context.Procedures.AddRange(procedures); context.SaveChanges(); context.ProceduresAnimalAids.AddRange(procedureAnimalAids); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var animalsJson = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); var animals = new List <Animal>(); var sb = new StringBuilder(); foreach (var animalDto in animalsJson) { if (!IsPassportValid(animalDto.Passport) || !IsValid(animalDto)) { sb.AppendLine(Failure_Message); continue; } var passport = context.Passports.FirstOrDefault(p => p.SerialNumber == animalDto.Passport.SerialNumber); if (passport != null) { sb.AppendLine(Failure_Message); continue; } var passportDto = animalDto.Passport; passport = new Passport { SerialNumber = passportDto.SerialNumber, OwnerName = passportDto.OwnerName, OwnerPhoneNumber = passportDto.OwnerPhoneNumber, RegistrationDate = DateTime.ParseExact(passportDto.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture) }; context.Passports.Add(passport); context.SaveChanges(); animals.Add(new Animal { Name = animalDto.Name, Type = animalDto.Type, Age = animalDto.Age, PassportSerialNumber = passport.SerialNumber, Passport = passport }); sb.AppendLine($"Record {animalDto.Name} Passport №: {passport.SerialNumber} successfully imported."); } context.Animals.AddRange(animals); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var sb = new StringBuilder(); var deserializedAnimalDtos = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); var passports = new List <Passport>(); var animals = new List <Animal>(); foreach (var animalDto in deserializedAnimalDtos) { var passportExists = passports.Any(p => p.SerialNumber.Equals(animalDto.Passport.SerialNumber)); if (!IsValid(animalDto) || (!IsValid(animalDto.Passport)) || passportExists) { sb.AppendLine(ERROR_MESSAGE); continue; } var animal = new Animal { Name = animalDto.Name, Type = animalDto.Type, Age = animalDto.Age, PassportSerialNumber = animalDto.Passport.SerialNumber }; var registrationDate = DateTime.ParseExact(animalDto.Passport.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture); var passport = new Passport { SerialNumber = animalDto.Passport.SerialNumber, Animal = animal, OwnerPhoneNumber = animalDto.Passport.OwnerPhoneNumber, OwnerName = animalDto.Passport.OwnerName, RegistrationDate = registrationDate }; animals.Add(animal); passports.Add(passport); sb.AppendLine($"Record {animal.Name} Passport №: {passport.SerialNumber} successfully imported."); } context.Animals.AddRange(animals); context.SaveChanges(); context.AddRange(passports); context.SaveChanges(); var result = sb.ToString(); return(result); }
public ActionResult Create(Pet pet) { if (ModelState.IsValid) { db.Pets.Add(pet); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(pet)); }
public ActionResult Create(Owner owner) { if (ModelState.IsValid) { db.Owners.Add(owner); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(owner)); }
public ActionResult Create(Visit visit) { if (ModelState.IsValid) { db.Visits.Add(visit); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.PetId = new SelectList(db.Pets, "Id", "Name", visit.PetId); return(View(visit)); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var inputJson = @"[{'Name':'DontInsertMe','Type':'cat','Age':3,'Passport':{'SerialNumber':'anothev150','OwnerName':'Owner','OwnerPhoneNumber':'12345','RegistrationDate':'15-04-2015'}},{'Name':'DontInsertMe','Type':'cat','Age':3,'Passport':{'SerialNumber':'anothev650','OwnerName':'Owner','OwnerPhoneNumber':'1234567890','RegistrationDate':'15-04-2015'}},{'Name':'DontInsertMe','Type':'cat','Age':3,'Passport':{'SerialNumber':'anothev950','OwnerName':'Owner','OwnerPhoneNumber':'+123456789123','RegistrationDate':'15-04-2015'}}]"; var sb = new StringBuilder(); var validPassports = new List <Passport>(); var validAnimal = new List <Animal>(); var deserializedItems = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); foreach (var item in deserializedItems) { if (!IsValid(item)) { sb.AppendLine("Error: Invalid data."); continue; } if (!IsValid(item.Passport)) { sb.AppendLine("Error: Invalid data."); continue; } if (context.Passports.Any(e => e.SerialNumber == item.Passport.SerialNumber)) { sb.AppendLine("Error: Invalid data."); continue; } var passport = new Passport() { SerialNumber = item.Passport.SerialNumber, OwnerName = item.Passport.OwnerName, RegistrationDate = DateTime.ParseExact(item.Passport.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture), OwnerPhoneNumber = item.Passport.OwnerPhoneNumber, }; validPassports.Add(passport); context.Passports.Add(passport); context.SaveChanges(); var passportId = context.Passports.FirstOrDefault(e => e.SerialNumber == passport.SerialNumber).SerialNumber; var animal = new Animal(); animal.Age = item.Age; animal.Name = item.Name; animal.PassportSerialNumber = passportId; animal.Type = item.Type; validAnimal.Add(animal); sb.AppendLine($"Record {animal.Name} Passport №: {passport.SerialNumber} successfully imported."); } context.Animals.AddRange(validAnimal); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public IActionResult Delete(int id) { var medic = _dbContext.Medics.Find(id); if (medic == null) { return(NotFound()); } _dbContext.Medics.Remove(medic); _dbContext.SaveChanges(); return(Ok()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var toImport = JsonConvert.DeserializeObject <ImportAnimalDto[]>(jsonString); StringBuilder sb = new StringBuilder(); foreach (var animalDto in toImport) { var animal = new Animal { Name = animalDto.Name, Type = animalDto.Type, Age = animalDto.Age, PassportSerialNumber = animalDto.Passport.SerialNumber, Passport = new Passport { SerialNumber = animalDto.Passport.SerialNumber, OwnerName = animalDto.Passport.OwnerName, OwnerPhoneNumber = animalDto.Passport.OwnerPhoneNumber, RegistrationDate = DateTime.ParseExact(animalDto.Passport.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture) } }; var check = IsValid(animal); var check1 = IsValid(animal.Passport); var check2 = context.Passports .FirstOrDefault(x => x.SerialNumber == animal.Passport.SerialNumber) == null; if (check && check1 && check2) { context.Passports.Add(animal.Passport); context.Animals.Add(animal); context.SaveChanges(); sb.AppendLine(String.Format(AnimalsSuccessMessage, animal.Name, animal.Passport.SerialNumber)); } else { sb.AppendLine(ErrorMessage); } } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var format = "dd-MM-yyyy"; // your datetime format var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = format }; var deserializedAnimal = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString, dateTimeConverter); List <Animal> animals = new List <Animal>(); foreach (var animalDto in deserializedAnimal) { bool isExistsAnimal = animals.Any(x => x.Passport.SerialNumber == animalDto.Passport.SerialNumber); if (!IsValid(animalDto) || isExistsAnimal || !IsValid(animalDto.Passport)) { sb.AppendLine("Error: Invalid data."); continue; } var passport = new Passport() { SerialNumber = animalDto.Passport.SerialNumber, OwnerName = animalDto.Passport.OwnerName, OwnerPhoneNumber = animalDto.Passport.OwnerPhoneNumber, RegistrationDate = animalDto.Passport.RegistrationDate }; context.Passports.Add(passport); context.SaveChanges(); var animal = new Animal() { Name = animalDto.Name, Type = animalDto.Type, Age = animalDto.Age, Passport = passport }; animals.Add(animal); sb.AppendLine($"Record {animal.Name} Passport №: {animal.Passport.SerialNumber} successfully imported."); } context.Animals.AddRange(animals); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { AnimalDto[] dtos = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); StringBuilder stringBuilder = new StringBuilder(); IList <Animal> existingAnimals = new List <Animal>(); foreach (var dto in dtos) { Animal currentAnimal = Mapper.Map <Animal>(dto); bool validAnimal = IsValid(currentAnimal); bool validPassport = IsValid(currentAnimal.Passport); bool existsSerialNumber = existingAnimals.Any(x => x.Passport.SerialNumber == currentAnimal.Passport.SerialNumber); if (!validAnimal || !validPassport || existsSerialNumber) { stringBuilder.AppendLine(FailureMessage); continue; } existingAnimals.Add(currentAnimal); stringBuilder.AppendLine(String.Format(SuccessMessage, $"{currentAnimal.Name} Passport №: {currentAnimal.Passport.SerialNumber}")); } context.Animals.AddRange(existingAnimals); context.SaveChanges(); string result = stringBuilder.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { Models.AnimalAid[] animalAids = JsonConvert.DeserializeObject <Models.AnimalAid[]>(jsonString); StringBuilder sb = new StringBuilder(); List <Models.AnimalAid> animalAidss = new List <Models.AnimalAid>(); foreach (Models.AnimalAid ai in animalAids) { bool contains = animalAidss.Any(x => x.Name == ai.Name); if (!IsValid(ai) || contains) { sb.AppendLine(FailureMessage); continue; } animalAidss.Add(ai); sb.AppendLine(String.Format(SuccessMessage, ai.Name)); } context.AnimalAids.AddRange(animalAidss); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { JsonSerializerSettings settings = new JsonSerializerSettings { DateFormatString = "dd-MM-yyyy" }; var animals = JsonConvert.DeserializeObject <Animal[]>(jsonString, settings); var serialNumbers = new HashSet <string>(); var validAniamls = new List <Animal>(); var sb = new StringBuilder(); foreach (var animal in animals) { if (!IsValid(animal) || !IsValid(animal.Passport) || serialNumbers.Contains(animal.Passport.SerialNumber)) { sb.AppendLine("Error: Invalid data."); continue; } serialNumbers.Add(animal.Passport.SerialNumber); validAniamls.Add(animal); sb.AppendLine($"Record {animal.Name} Passport №: {animal.Passport.SerialNumber} successfully imported."); } context.Animals.AddRange(validAniamls); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var animals = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); var sb = new StringBuilder(); var validEntries = new List <Animal>(); foreach (var dto in animals) { var animal = Mapper.Map <Animal>(dto); var animalIsValid = IsValid(animal); var passportIsValid = IsValid(animal.Passport); var alreadyExists = validEntries.Any(a => a.Passport.SerialNumber == animal.Passport.SerialNumber); if (!animalIsValid || !passportIsValid || alreadyExists) { sb.AppendLine(FailureMessage); continue; } validEntries.Add(animal); sb.AppendLine(string.Format(SuccessMessage, $"{animal.Name} Passport №: {animal.Passport.SerialNumber}")); } context.Animals.AddRange(validEntries); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportVets(PetClinicContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(VetDto[]), new XmlRootAttribute("Vets")); var deserializedXml = (VetDto[])serializer.Deserialize(new StringReader(xmlString)); var validVets = new List <Vet>(); foreach (var vetDto in deserializedXml) { var vetPhoneNumberExists = validVets.Any(x => x.PhoneNumber == vetDto.PhoneNumber); if (!IsValid(vetDto) || vetPhoneNumberExists) { sb.AppendLine(ErrorMessage); continue; } var vet = Mapper.Map <Vet>(vetDto); validVets.Add(vet); sb.AppendLine($"Record {vetDto.Name} successfully imported."); } context.Vets.AddRange(validVets); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var sb = new StringBuilder(); var deserializedJson = JsonConvert.DeserializeObject <AnimalAidDto[]>(jsonString); var validAnimalAids = new List <AnimalAid>(); foreach (var animalAidDto in deserializedJson) { var animalAidExists = validAnimalAids.Any(ai => ai.Name == animalAidDto.Name); if (!IsValid(animalAidDto) || animalAidExists) { sb.AppendLine(ErrorMessage); continue; } var animalAid = Mapper.Map <AnimalAid>(animalAidDto); validAnimalAids.Add(animalAid); sb.AppendLine($"Record {animalAidDto.Name} successfully imported."); } context.AnimalAids.AddRange(validAnimalAids); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var deserializedEntities = JsonConvert.DeserializeObject <AnimalAid[]>(jsonString); StringBuilder sb = new StringBuilder(); List <AnimalAid> animalAids = new List <AnimalAid>(); foreach (var entity in deserializedEntities) { if (!IsValid(entity)) { sb.AppendLine(errorMessage); continue; } if (animalAids.Any(aa => aa.Name == entity.Name) || context.AnimalAids.Any(aa => aa.Name == entity.Name)) { sb.AppendLine(errorMessage); continue; } animalAids.Add(entity); sb.AppendLine($"Record {entity.Name} successfully imported."); } context.AnimalAids.AddRange(animalAids); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportVets(PetClinicContext context, string xmlString) { XmlSerializer serializer = new XmlSerializer(typeof(VetDto[]), new XmlRootAttribute("Vets")); VetDto[] purchasesDto = (VetDto[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder sb = new StringBuilder(); IList <Vet> allVets = new List <Vet>(); foreach (VetDto dto in purchasesDto) { Vet currentVet = Mapper.Map <Vet>(dto); bool isValidVet = IsValid(currentVet); bool phoneNumberExist = allVets.Any(v => v.PhoneNumber == currentVet.PhoneNumber); if (!isValidVet || phoneNumberExist) { sb.AppendLine(FailureMessage); continue; } allVets.Add(currentVet); sb.AppendLine(string.Format(SuccessVetAdd, currentVet.Name)); } context.Vets.AddRange(allVets); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var deserializedAnimalAids = JsonConvert.DeserializeObject <AnimalAidsDto[]>(jsonString); var animalAids = new List <AnimalAid>(); StringBuilder sb = new StringBuilder(); foreach (var dto in deserializedAnimalAids) { if (!IsValid(dto)) { sb.AppendLine($"Error: Invalid data."); continue; } AnimalAid animalAid = new AnimalAid(dto.Name, dto.Price); if (animalAids.Any(a => a.Name == animalAid.Name)) { sb.AppendLine($"Error: Invalid data."); continue; } animalAids.Add(animalAid); sb.AppendLine($"Record {animalAid.Name} successfully imported."); } context.AnimalAids.AddRange(animalAids); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var animalAids = new List <AnimalAid>(); var result = new StringBuilder(); var objAnimalAids = JsonConvert.DeserializeObject <AnimalAid[]>(jsonString); foreach (var objAnimalAid in objAnimalAids) { var ifAaExists = animalAids.Any(aa => aa.Name == objAnimalAid.Name); if (!IsValid(objAnimalAid) || ifAaExists) { result.AppendLine(ErrorMsg); continue; } animalAids.Add(objAnimalAid); result.AppendLine(string.Format(SuccessMsg, objAnimalAid.Name)); } context.AnimalAids.AddRange(animalAids); context.SaveChanges(); return(result.ToString().Trim()); }
public static string ImportProcedures(PetClinicContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures")); var deserializedXml = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString)); var validProcedures = new List <Procedure>(); foreach (var procedureDto in deserializedXml) { var vetObj = context.Vets.SingleOrDefault(x => x.Name == procedureDto.Vet); var animalObj = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal); var validProcedureAnimalAids = new List <ProcedureAnimalAid>(); var allAidsExists = true; foreach (var procedureDtoAnimalAid in procedureDto.AnimalAids) { var animalAid = context.AnimalAids .SingleOrDefault(ai => ai.Name == procedureDtoAnimalAid.Name); if (animalAid == null || validProcedureAnimalAids.Any(p => p.AnimalAid.Name == procedureDtoAnimalAid.Name)) { allAidsExists = false; break; } var animalAidProcedure = new ProcedureAnimalAid() { AnimalAid = animalAid }; validProcedureAnimalAids.Add(animalAidProcedure); } if (!IsValid(procedureDto) || !procedureDto.AnimalAids.All(IsValid) || vetObj == null || animalObj == null || !allAidsExists) { sb.AppendLine(ErrorMessage); continue; } var procedure = new Procedure() { Animal = animalObj, Vet = vetObj, DateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture), ProcedureAnimalAids = validProcedureAnimalAids }; validProcedures.Add(procedure); sb.AppendLine("Record successfully imported."); } context.Procedures.AddRange(validProcedures); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var animals = new List <Animal>(); var passports = new List <Passport>(); var result = new StringBuilder(); var objAnimals = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); foreach (var objAnimal in objAnimals) { var ifPassportExists = animals.Any(a => a.PassportSerialNumber == objAnimal.Passport.SerialNumber); if (!IsValid(objAnimal) || !IsValid(objAnimal.Passport) || ifPassportExists || objAnimal.Name.Length < 3 || objAnimal.Type.Length < 3) { result.AppendLine(ErrorMsg); continue; } animals.Add(Mapper.Map <Animal>(objAnimal)); passports.Add(Mapper.Map <Passport>(objAnimal.Passport)); var msg = $"{objAnimal.Name} Passport №: {objAnimal.Passport.SerialNumber}"; result.AppendLine(string.Format(SuccessMsg, msg)); } context.Animals.AddRange(animals); context.SaveChanges(); return(result.ToString().Trim()); }
public static string ImportAnimals(PetClinicContext context, string jsonString) { var sb = new StringBuilder(); var deserializedJson = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString); var validAnimals = new List <Animal>(); foreach (var animalDto in deserializedJson) { var passportSerialNumberExists = validAnimals.Any(p => p.Passport.SerialNumber == animalDto.Passport.SerialNumber); if (!IsValid(animalDto) || !IsValid(animalDto.Passport) || passportSerialNumberExists) { sb.AppendLine(ErrorMessage); continue; } var animal = Mapper.Map <Animal>(animalDto); validAnimals.Add(animal); sb.AppendLine($"Record {animalDto.Name} Passport №: {animalDto.Passport.SerialNumber} successfully imported."); } context.Animals.AddRange(validAnimals); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportVets(PetClinicContext context, string xmlString) { var vets = new List <Vet>(); var result = new StringBuilder(); var serializer = new XmlSerializer(typeof(VetDto[]), new XmlRootAttribute("Vets")); var objVets = (VetDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); foreach (var objVet in objVets) { var ifVetExists = vets.Any(v => v.PhoneNumber == objVet.PhoneNumber); if (!IsValid(objVet) || ifVetExists) { result.AppendLine(ErrorMsg); continue; } vets.Add(Mapper.Map <Vet>(objVet)); result.AppendLine(string.Format(SuccessMsg, objVet.Name)); } context.Vets.AddRange(vets); context.SaveChanges(); return(result.ToString().Trim()); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var animalAids = JsonConvert.DeserializeObject <AnimalAid[]>(jsonString); var validEntries = new List <AnimalAid>(); var sb = new StringBuilder(); foreach (var aa in animalAids) { var isValid = IsValid(aa); var alreadyExists = validEntries.Any(a => a.Name == aa.Name); if (!isValid || alreadyExists) { sb.AppendLine(FailureMessage); continue; } validEntries.Add(aa); sb.AppendLine(string.Format(SuccessMessage, aa.Name)); } context.AnimalAids.AddRange(validEntries); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportVets(PetClinicContext context, string xmlString) { StringBuilder sb = new StringBuilder(); var validVets = new List <Vet>(); var vets = XmlConverter.Deserializer <VetXmlInputModel>(xmlString, "Vets"); foreach (var currentVet in vets) { if (!IsValid(currentVet) || validVets.Any(vv => vv.PhoneNumber == currentVet.PhoneNumber)) { sb.AppendLine(ErrorMessage); continue; } var vetToAdd = new Vet { Name = currentVet.Name, Profession = currentVet.Profession, Age = currentVet.Age, PhoneNumber = currentVet.PhoneNumber }; validVets.Add(vetToAdd); sb.AppendLine(string.Format(SuccessMessage, vetToAdd.Name)); } context.Vets.AddRange(validVets); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { var animalAids = JsonConvert.DeserializeObject <AnimalAidDto[]>(jsonString); var sb = new StringBuilder(); var validAnimalAid = new List <AnimalAid>(); foreach (var dto in animalAids) { if (!IsValid(dto) || validAnimalAid.Any(a => a.Name.Equals(dto.Name, StringComparison.OrdinalIgnoreCase))) { sb.AppendLine("Error: Invalid data."); continue; } var animalAid = new AnimalAid { Name = dto.Name, Price = dto.Price }; validAnimalAid.Add(animalAid); sb.AppendLine($"Record {dto.Name} successfully imported."); } context.AnimalAids.AddRange(validAnimalAid); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportAnimalAids(PetClinicContext context, string jsonString) { StringBuilder sb = new StringBuilder(); var animalAidsToImport = new List <AnimalAid>(); var animalAidDtos = JsonConvert.DeserializeObject <IEnumerable <AnimalAidJsonInputModel> >(jsonString); foreach (var currentAnimalAid in animalAidDtos) { if (!IsValid(currentAnimalAid)) { sb.AppendLine(ErrorMessage); continue; } var animalAidtoAdd = new AnimalAid { Name = currentAnimalAid.Name, Price = currentAnimalAid.Price }; if (animalAidsToImport.Any(a => a.Name == animalAidtoAdd.Name)) { sb.AppendLine(ErrorMessage); continue; } animalAidsToImport.Add(animalAidtoAdd); sb.AppendLine(string.Format(SuccessMessage, animalAidtoAdd.Name)); } context.AnimalAids.AddRange(animalAidsToImport); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportVets(PetClinicContext context, string xmlString) { var serializer = new XmlSerializer(typeof(ImportVetDto[]), new XmlRootAttribute("Vets")); var vets = (ImportVetDto[])serializer.Deserialize(new StringReader(xmlString)); var phoneNumbers = new HashSet <string>(); var sb = new StringBuilder(); var validVets = new List <Vet>(); foreach (var vet in vets) { if (!IsValid(vet) || phoneNumbers.Contains(vet.PhoneNumber)) { sb.AppendLine("Error: Invalid data."); continue; } phoneNumbers.Add(vet.PhoneNumber); var validVet = Mapper.Map <Vet>(vet); validVets.Add(validVet); sb.AppendLine($"Record {vet.Name} successfully imported."); } context.Vets.AddRange(validVets); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportVets(PetClinicContext context, string xmlString) { List <string> phoneNumbersAvailable = context.Vets.Select(x => x.PhoneNumber).ToList(); var serializer = new XmlSerializer(typeof(imp_xml_vetDto[]), new XmlRootAttribute("Vets")); var vetsDtos = (imp_xml_vetDto[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder sb = new StringBuilder(); List <Vet> vetsToBeAdded = new List <Vet>(); foreach (var dto in vetsDtos) { if (AttributeValidator.IsValid(dto) && phoneNumbersAvailable.All(x => x != dto.PhoneNumber)) { phoneNumbersAvailable.Add(dto.PhoneNumber); Vet newVet = Mapper.Map <Vet>(dto); vetsToBeAdded.Add(newVet); sb.AppendLine($"Record {newVet.Name} successfully imported."); continue; } sb.AppendLine(invalidEntryMessage); } context.Vets.AddRange(vetsToBeAdded); context.SaveChanges(); return(sb.ToString().Trim()); }