//TODO: Moet een dokter ook niet de treatment details kunnen inzien?
        public IActionResult Details(long id)
        {
            // Check if id is set
            if (id < 1)
            {
                return(BadRequest("No user found"));
            }

            // Retrieve from db
            TreatmentType model             = repository.GetById(id);
            TreatmentTypeDetailViewModel vm = converter.ModelToViewModel(model);

            // Get all departments for select list
            List <Department>            departments = departmentRepository.GetAll();
            IEnumerable <SelectListItem> items       =
                from value in departments
                select new SelectListItem
            {
                Text     = value.Name,
                Value    = value.Id.ToString(),
                Selected = (value.Id == vm.DepartmentId)
            };

            vm.Departments = items.ToList();

            return(View(vm));
        }
        /// <summary>
        /// Method gets all objects form departments and institutions and coverts them to a viewmodel.
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            // Retrieve for dropdown
            List <Department>  departments  = departmentRepository.GetAll();
            List <Institution> institutions = institutionRepository.GetAll();

            List <SelectListItem> items = new List <SelectListItem>();
            SelectListGroup       group;

            // Create dropdownlist for treatmenttype
            foreach (Institution i in institutions)
            {
                group = new SelectListGroup
                {
                    Name = i.Name
                };

                foreach (Department dm in departments.Where(d => d.InstitutionId == i.Id))
                {
                    items.Add(new SelectListItem
                    {
                        Value = dm.Id.ToString(),
                        Text  = dm.Name,
                        Group = group
                    });
                }
            }

            TreatmentTypeDetailViewModel vm = new TreatmentTypeDetailViewModel
            {
                Departments = items
            };

            return(View(vm));
        }
        public IActionResult Create(TreatmentTypeDetailViewModel vm)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                // Push to database
                TreatmentType tt = converter.ViewModelToModel(vm);
                long          id = repository.Insert(tt);

                // Return details page of just inserted record
                return(RedirectToAction("details", new { id }));
            }
            else
            {
                return(View(vm));
            }
        }
        /// <summary>
        /// Method gets all objects form departments and institutions and coverts them to a viewmodel.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(long id)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                // Get
                TreatmentType tt = repository.GetById(id);

                if (tt == null)
                {
                    return(BadRequest("User not found."));
                }

                // Get dropdown for treatmenttype
                List <Department>  departments  = departmentRepository.GetAll();
                List <Institution> institutions = institutionRepository.GetAll();

                List <SelectListItem> items = new List <SelectListItem>();
                SelectListGroup       group;
                foreach (Institution i in institutions)
                {
                    group = new SelectListGroup
                    {
                        Name = i.Name
                    };

                    foreach (Department dm in departments.Where(d => d.InstitutionId == i.Id))
                    {
                        items.Add(new SelectListItem
                        {
                            Value = dm.Id.ToString(),
                            Text  = dm.Name,
                            Group = group
                        });
                    }
                }

                // Convert to vm
                TreatmentTypeDetailViewModel vm = converter.ModelToViewModel(tt);
                vm.Departments = items;

                return(View(vm));
            }
            return(RedirectToAction("Index"));
        }
        public IActionResult Edit(TreatmentTypeDetailViewModel vm)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                // Update in database
                TreatmentType tt = converter.ViewModelToModel(vm);
                repository.Update(tt);

                if (tt.Active)
                {
                    return(RedirectToAction("details", new { id = tt.Id }));
                }
                else
                {
                    return(RedirectToAction("index"));
                }
            }
            return(View(vm));
        }