public async Task <IActionResult> GetVariants(int fieldNameId, int maxCount)
        {
            var template = await ControllerHelpers
                           .ReadModelFromBodyAsync <PatientSearchTemplateDTM>(this.Request.Body);

            var fieldNames = await _dbContext.Patients
                             .MyExt_GetFieldValuesForTemplate(template, fieldNameId, maxCount)
                             .ToListAsync();

            return(Ok(fieldNames));
        }
        public async Task <List <PatientDTM> > GetPortion(int skip, int take)
        {
            var template = await ControllerHelpers
                           .ReadModelFromBodyAsync <PatientSearchTemplateDTM>(this.Request.Body);

            var patients = await _dbContext.Patients
                           .MyExt_PortionByTemplate(template, skip, take)
                           .ToListAsync();

            var patientsDTM = patients
                              .Select(p => new PatientDTM(p))
                              .ToList();

            return(patientsDTM);
        }
        public async Task <PatientSearchTemplateDTM> UpdateTemplate()
        {
            var updatedTemplate = await ControllerHelpers
                                  .ReadModelFromBodyAsync <PatientSearchTemplateDTM>(this.Request.Body);

            var existingFieldNames = await _dbContext.FieldNames.ToListAsync();

            bool needSave = false;

            foreach (var updatedField in updatedTemplate.Fields)
            {
                if (updatedField.NameId > 0)
                {
                    var existingFN = existingFieldNames.FirstOrDefault(
                        fn => fn.Id == updatedField.NameId);

                    if (existingFN == null)
                    {
                        throw new MyException(
                                  MyExceptionType.DoesNotExistInDatabase,
                                  updatedField);
                    }

                    if (updatedField.UpdateModel(existingFN))
                    {
                        needSave = true;
                    }
                }
                else
                {
                    needSave = true;

                    _dbContext.FieldNames.Add(new FieldName(updatedField.Name));
                }
            }

            if (needSave)
            {
                await _dbContext.SaveChangesAsync();
            }

            List <FieldName> fns = await _dbContext.FieldNames.ToListAsync();

            var loadedTemplate = new PatientSearchTemplateDTM(fns);

            return(loadedTemplate);
        }
        public async Task <int> Restore()
        {
            var patientDTM = await ControllerHelpers
                             .ReadModelFromBodyAsync <PatientDTM>(this.Request.Body);

            Patient patient = await _dbContext.Patients.FindAsync(patientDTM.Id);

            if (patient == null)
            {
                throw new MyException(
                          MyExceptionType.DoesNotExistInDatabase,
                          patientDTM);
            }

            patient.IsDeleted = false;

            await _dbContext.SaveChangesAsync(true);

            return(patient.Id);
        }
        public async Task <PatientDTM> Add()
        {
            var patientDTM = await ControllerHelpers
                             .ReadModelFromBodyAsync <PatientDTM>(this.Request.Body);

            List <FieldName> existingFieldNames = await _dbContext.FieldNames.ToListAsync();

            Patient patient = new Patient(
                existingFieldNames
                .Select(fn => new PatientField(fn))
                .ToArray());

            patientDTM.UpdateModel(patient);

            _dbContext.Patients.Add(patient);

            await _dbContext.SaveChangesAsync(true);

            patientDTM = new PatientDTM(patient);

            return(patientDTM);
        }
        public async Task <PatientDTM> Update()
        {
            var patientDTM = await ControllerHelpers
                             .ReadModelFromBodyAsync <PatientDTM>(this.Request.Body);

            List <FieldName> existingFieldNames = await _dbContext.FieldNames.ToListAsync();

            Patient patient = await _dbContext.Patients
                              .IncludeFields()
                              .FirstOrDefaultAsync(p => p.Id == patientDTM.Id);

            if (patient == null)
            {
                throw new MyException(
                          MyExceptionType.DoesNotExistInDatabase,
                          patientDTM);
            }

            patientDTM.UpdateModel(patient);

            await _dbContext.SaveChangesAsync(true);

            return(patientDTM);
        }