public MedicineLaboratoryViewModel GetViewModel(Laboratory laboratory, int? page = null)
        {
            if (laboratory == null)
                return new MedicineLaboratoryViewModel();

            if (!page.HasValue)
                page = 1;
            var pageSize = Constants.GRID_PAGE_SIZE;

            var medicinesQuery = from medicine in laboratory.Medicines
                                 orderby medicine.Name
                                 select new MedicineViewModel()
                                 {
                                     Id = medicine.Id,
                                     Name = medicine.Name,
                                     Usage = medicine.Usage
                                 };

            return new MedicineLaboratoryViewModel()
            {
                Id = laboratory.Id,
                Name = laboratory.Name,
                Observations = laboratory.Observations,
                Medicines = new SearchViewModel<MedicineViewModel>()
                {
                    Objects = medicinesQuery.Skip((page.Value - 1) * pageSize).Take(pageSize).ToList(),
                    Count = medicinesQuery.Count()
                },
            };
        }
 /// <summary>
 /// Create a new Laboratory 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="practiceId">Initial value of the PracticeId property.</param>
 /// <param name="createdOn">Initial value of the CreatedOn property.</param>
 public static Laboratory CreateLaboratory(global::System.Int32 id, global::System.String name, global::System.Int32 doctorId, global::System.Int32 practiceId, global::System.DateTime createdOn)
 {
     Laboratory laboratory = new Laboratory();
     laboratory.Id = id;
     laboratory.Name = name;
     laboratory.DoctorId = doctorId;
     laboratory.PracticeId = practiceId;
     laboratory.CreatedOn = createdOn;
     return laboratory;
 }
        public ActionResult Edit([NotNull] MedicineLaboratoryViewModel formModel)
        {
            if (formModel == null) throw new ArgumentNullException("formModel");

            // if a laboratory exists with the same name, a model state error must be placed
            var existingLaboratory = this.db.Laboratories.FirstOrDefault(l => l.Name == formModel.Name);
            if (existingLaboratory != null && existingLaboratory.Id != formModel.Id)
                this.ModelState.AddModelError<MedicineLaboratoryViewModel>(model => model.Name, "Já existe um laboratório cadastrado com o mesmo nome");

            if (this.ModelState.IsValid)
            {
                Laboratory laboratory = null;
                if (formModel.Id.HasValue)
                {
                    laboratory = this.db.Laboratories.FirstOrDefault(l => l.Id == formModel.Id);
                    if (laboratory == null)
                        return this.ObjectNotFound();
                }
                else
                {
                    laboratory = new Laboratory()
                        {
                            PracticeId = this.DbUser.PracticeId,
                            DoctorId = this.Doctor.Id,
                            CreatedOn = this.GetUtcNow(),
                        };
                    this.db.Laboratories.AddObject(laboratory);
                }

                laboratory.Name = formModel.Name;
                laboratory.Observations = formModel.Observations;

                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 = laboratory.Id,
                                Value = laboratory.Name
                            });

                // The View here will DEPEND on the caller.
                // If it's EditModal, it's gonna be EditModal. Otherwise, Edit
                return this.RedirectToAction("details", new { id = laboratory.Id });
            }

            // the resulting view will depend on the request type
            return this.Request.IsAjaxRequest() ? View("EditModal", formModel) : View("Edit", formModel);
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Laboratories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToLaboratories(Laboratory laboratory)
 {
     base.AddObject("Laboratories", laboratory);
 }