public void Delete_WhenTheresAReceipt() { PatientsController controller; int patientId; Patient patient; try { var doctor = Firestarter.Create_CrmMg_Psiquiatria_DrHouse_Andre(this.db); var mr = new MockRepository(true); controller = mr.CreateController<PatientsController>(); Firestarter.CreateFakePatients(doctor, this.db, 1); // we now have 1 patient patient = this.db.Patients.FirstOrDefault(); Assert.IsNotNull(patient); patientId = patient.Id; var medicine = new Medicine() { Laboratory = new Laboratory() { Name = "Lab1", Doctor = doctor }, Name = "Med1", Doctor = doctor, PracticeId = doctor.PracticeId, }; medicine.ActiveIngredients.Add(new MedicineActiveIngredient() { Name = "AI1", PracticeId = doctor.PracticeId, }); this.db.Medicines.AddObject(medicine); // now, let's add an receipt var receipt = new Receipt() { PatientId = patientId, CreatedOn = DateTime.UtcNow, PracticeId = doctor.PracticeId, }; receipt.ReceiptMedicines.Add(new ReceiptMedicine() { Medicine = medicine, Quantity = "1 caixa", Prescription = "toma 1 de manha", PracticeId = doctor.PracticeId, }); this.db.Receipts.AddObject(receipt); this.db.SaveChanges(); } catch { Assert.Inconclusive("Test initialization has failed."); return; } controller.Delete(patientId); // this patient must have been deleted patient = this.db.Patients.FirstOrDefault(p => p.Id == patientId); Assert.IsNull(patient); }
/// <summary> /// Create a new Medicine object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="name">Initial value of the Name property.</param> /// <param name="doctorId">Initial value of the DoctorId property.</param> /// <param name="usage">Initial value of the Usage property.</param> /// <param name="practiceId">Initial value of the PracticeId property.</param> /// <param name="createdOn">Initial value of the CreatedOn property.</param> public static Medicine CreateMedicine(global::System.Int32 id, global::System.String name, global::System.Int32 doctorId, global::System.Int16 usage, global::System.Int32 practiceId, global::System.DateTime createdOn) { Medicine medicine = new Medicine(); medicine.Id = id; medicine.Name = name; medicine.DoctorId = doctorId; medicine.Usage = usage; medicine.PracticeId = practiceId; medicine.CreatedOn = createdOn; return medicine; }
public MedicineViewModel GetViewModelFromModel(Medicine medicine, int? page = null) { if (!page.HasValue) page = 1; var pageSize = Constants.GRID_PAGE_SIZE; var prescriptionsQuery = from receiptMedicine in medicine.ReceiptMedicines let patient = receiptMedicine.Receipt.Patient orderby receiptMedicine.Receipt.CreatedOn descending select new PrescriptionViewModel() { PatientId = patient.Id, PatientName = patient.Person.FullName, Prescription = receiptMedicine.Prescription, Quantity = receiptMedicine.Quantity, Date = receiptMedicine.Receipt.CreatedOn }; return new MedicineViewModel { Id = medicine.Id, Name = medicine.Name, Usage = medicine.Usage, Observations = medicine.Observations, ActiveIngredients = (from activeIngredient in medicine.ActiveIngredients select new MedicineActiveIngredientViewModel() { Id = activeIngredient.Id, Name = activeIngredient.Name }).ToList(), Leaflets = (from leaflet in medicine.Leaflets select this.GetLeafletViewModel(leaflet) ).ToList(), Prescriptions = new SearchViewModel<PrescriptionViewModel>() { Objects = prescriptionsQuery.Skip((page.Value - 1) * pageSize).Take(pageSize).ToList(), Count = prescriptionsQuery.Count() }, LaboratoryId = medicine.Laboratory.Id, LaboratoryName = medicine.Laboratory.Name }; }
/// <summary> /// Deprecated Method for adding a new object to the Medicines EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToMedicines(Medicine medicine) { base.AddObject("Medicines", medicine); }
public ActionResult Edit(MedicineViewModel formModel) { if (formModel.IsImporting) { // in this case it's importing the medicine from Anvisa // remove model state errors this.ModelState.Remove(() => formModel.Name); this.ModelState.Remove(() => formModel.LaboratoryName); // validating the existence of the sys medicine var sysMedicine = this.db.SYS_Medicine.FirstOrDefault(sm => sm.Id == formModel.AnvisaId); if (sysMedicine == null && formModel.AnvisaId.HasValue) // I verify formModel.AnvisaId.HasValue here to prevent double errors this.ModelState.AddModelError<AnvisaImportViewModel>(model => model.AnvisaId, "O medicamento informado não foi encontrado"); // validating the name is unique if (sysMedicine != null && this.db.Medicines.Any(m => m.DoctorId == this.Doctor.Id && m.Name == formModel.AnvisaCustomText)) this.ModelState.AddModelError<AnvisaImportViewModel>( model => model.AnvisaId, "Já existe um medicamento com o mesmo nome do medicamento informado"); if (this.ModelState.IsValid) { Debug.Assert(sysMedicine != null, "sysMedicine != null"); var medicine = new Medicine() { Name = formModel.AnvisaCustomText, PracticeId = this.DbPractice.Id, DoctorId = this.Doctor.Id, CreatedOn = this.GetUtcNow(), }; // verify the need to create a new laboratory var laboratory = this.db.Laboratories.FirstOrDefault(l => l.DoctorId == this.Doctor.Id && l.Name == sysMedicine.Laboratory.Name) ?? new Laboratory() { Name = sysMedicine.Laboratory.Name, PracticeId = this.DbPractice.Id, DoctorId = this.Doctor.Id, CreatedOn = this.GetUtcNow(), }; medicine.Laboratory = laboratory; // verify the need to create new active ingredients foreach (var ai in sysMedicine.ActiveIngredients) { medicine.ActiveIngredients.Add(new MedicineActiveIngredient() { Name = ai.Name, PracticeId = this.DbPractice.Id, }); } // create the leaflets foreach (var l in sysMedicine.Leaflets) medicine.Leaflets.Add( new Leaflet() { Description = l.Description, Url = l.Url, PracticeId = this.DbPractice.Id }); this.db.Medicines.AddObject(medicine); this.db.SaveChanges(); // depending on whether or not this is an Ajax request, // this should return an AutocompleteNewJsonResult or the view if (this.Request.IsAjaxRequest()) return this.Json( new AutocompleteNewJsonResult() { Id = medicine.Id, Value = medicine.Name }); return this.RedirectToAction("Details", new { id = medicine.Id }); } } else { // In this case the medicine is being edited manually // remove model state errors this.ModelState.Remove(() => formModel.AnvisaId); this.ModelState.Remove(() => formModel.AnvisaText); this.ModelState.Remove(() => formModel.AnvisaCustomText); // if a medicine exists with the same name, a model state error must be placed var existingMedicine = this.db.Medicines.FirstOrDefault(m => m.Name == formModel.Name); if (existingMedicine != null && existingMedicine.Id != formModel.Id) this.ModelState.AddModelError<MedicineViewModel>( model => model.Name, "Já existe um medicamento cadastrado com o mesmo nome"); // add validation error when Laboratory Id is invalid Laboratory laboratory = null; if (formModel.LaboratoryId != null) { laboratory = this.db.Laboratories.FirstOrDefault(lab => lab.Id == formModel.LaboratoryId && lab.DoctorId == this.Doctor.Id); if (laboratory == null) this.ModelState.AddModelError<MedicineViewModel>((model) => model.LaboratoryId, "O laboratório informado é inválido"); } if (this.ModelState.IsValid) { Medicine medicine = null; if (formModel.Id.HasValue) { medicine = this.db.Medicines.FirstOrDefault(m => m.Id == formModel.Id); if (medicine == null) return this.ObjectNotFound(); } else { medicine = new Medicine { PracticeId = this.DbUser.PracticeId, DoctorId = this.Doctor.Id, CreatedOn = this.GetUtcNow(), }; this.db.Medicines.AddObject(medicine); } medicine.Name = formModel.Name; medicine.Observations = formModel.Observations; medicine.Usage = (short)formModel.Usage; if (formModel.LaboratoryId != null) medicine.Laboratory = laboratory; // Active ingredients { // Verify whether any existing active ingredient should be REMOVED. var activeIngredientsDeathQueue = new Queue<MedicineActiveIngredient>(); foreach (var existingActiveIngredient in medicine.ActiveIngredients) if (formModel.ActiveIngredients.All(a => a.Id != existingActiveIngredient.Id)) activeIngredientsDeathQueue.Enqueue(existingActiveIngredient); while (activeIngredientsDeathQueue.Any()) this.db.ActiveIngredients.DeleteObject(activeIngredientsDeathQueue.Dequeue()); // Verify whether any new active ingredient should be UPDATED or ADDED foreach (var activeIngredientViewModel in formModel.ActiveIngredients) { var existingActiveIngredient = medicine.ActiveIngredients.SingleOrDefault(l => l.Id == activeIngredientViewModel.Id); if (existingActiveIngredient == null) { // ADD when not existing medicine.ActiveIngredients.Add( new MedicineActiveIngredient() { Name = activeIngredientViewModel.Name, PracticeId = this.DbPractice.Id }); } else { // UPDATE when existing existingActiveIngredient.Name = activeIngredientViewModel.Name; } } } // Leaflets { // Verify whether any existing leaflet must be REMOVED var leafletsDeathQueue = new Queue<Leaflet>(); foreach (var existingLeaflet in medicine.Leaflets) if (formModel.Leaflets.All(l => l.Id != existingLeaflet.Id)) leafletsDeathQueue.Enqueue(existingLeaflet); while (leafletsDeathQueue.Any()) this.db.Leaflets.DeleteObject(leafletsDeathQueue.Dequeue()); // Verify whether any leaflet should be UPDATED or ADDED foreach (var leafletViewModel in formModel.Leaflets) { var existingLeaftlet = medicine.Leaflets.SingleOrDefault(l => l.Id == leafletViewModel.Id); if (existingLeaftlet == null) { // ADD when not existing medicine.Leaflets.Add( new Leaflet() { PracticeId = this.DbPractice.Id, Url = leafletViewModel.Url, Description = leafletViewModel.Description }); } else { // UPDATE when existing existingLeaftlet.Url = leafletViewModel.Url; existingLeaftlet.Description = leafletViewModel.Description; } } } this.db.SaveChanges(); // depending on whether or not this is an Ajax request, // this should return an AutocompleteNewJsonResult or the view if (this.Request.IsAjaxRequest()) return this.Json( new AutocompleteNewJsonResult() { Id = medicine.Id, Value = medicine.Name }); return Redirect(Url.Action("details", new { id = medicine.Id })); } } return this.Request.IsAjaxRequest() ? View("EditModal", formModel) : View("Edit", formModel); }