public int SaveTreatment(Treatment treatment, bool newTreatment)
        {
            _logger.DebugFormat("Entering SaveTreatment with treatment: {0} and newTreatment: {1}", treatment, newTreatment);

            if (newTreatment)
            {
                return SaveNewTreatment(treatment);
            }

            return UpdateTreatment(treatment);
        }
 private int UpdateTreatment(Treatment treatment)
 {
     try
     {
         var request = _mappingEngine.Map<TreatmentEntity>(treatment);
         return _treatmentRepository.UpdateTreatment(request);
     }
     catch (Exception ex)
     {
         _logger.ErrorFormat("Exception when updating treatment: {0}", ex);
         return 0;
     }
 }
        public IHttpActionResult SaveTreatment(SaveTreatmentApiModel model)
        {
            var afflications = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AfflictionArea>>(model.Afflication);
            var deleteAfflication = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AfflictionArea>>(model.DeleteAffliction);
            var observations = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(model.Observations);
            var anamnesis = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(model.Anamnesis);
            var treatmentNotes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(model.TreatmentNotes);

            bool deleteAfflictionResult = _areaAfflicationService.DeleteAfflications(deleteAfflication);
            if (!deleteAfflictionResult)
            {
                _logger.ErrorFormat("Unable to delete afflictions");
            }

            var newTreatment = model.TreatmentId == 0;

            var observationsAsTextArea = SetupAsTextArea(observations);
            var anamnesisAsTextArea = SetupAsTextArea(anamnesis);
            var treatmentNotesAsTextArea = SetupAsTextArea(treatmentNotes);

            afflications.RemoveAll(x => x.IsPersisted);

            var oldAfflications = _areaAfflicationService.GetAfflicationAreasByCustomerId(model.CustomerId);
            var newAfflications = new List<AfflictionArea>();
            if (oldAfflications != null && oldAfflications.Any())
            {
                foreach (var afflictionArea in afflications)
                {
                    var isPersisted =
                        oldAfflications.Exists(
                            x =>
                                x.MouseXPosition == afflictionArea.MouseXPosition &&
                                x.MouseYPosition == afflictionArea.MouseXPosition);

                    if (!isPersisted)
                    {
                        newAfflications.Add(afflictionArea);
                    }
                }
            }
            else
            {
                newAfflications = afflications;
            }

            foreach (var afflictionArea in newAfflications)
            {
                afflictionArea.CreatedDate = DateTime.Now;
                afflictionArea.TreatmentId = model.TreatmentId;
                afflictionArea.CustomerId = model.CustomerId;
            }

            var treatment = _treatmentService.GetTreatmentById(model.TreatmentId);

            if (treatment == null)
            {
                treatment = new Treatment
                {
                    CreatedDate = DateTime.Now,
                    UserId = 1,
                    CustomerId = model.CustomerId
                };
            }

            treatment.AfflictionAreas = newAfflications;
            treatment.Anamnesis = anamnesisAsTextArea;
            treatment.ChangedDate = DateTime.Now;
            treatment.TreatmentNotes = treatmentNotesAsTextArea;
            treatment.Observations = observationsAsTextArea;
            treatment.UserId = 1;

            var treatmentId = _treatmentService.SaveTreatment(treatment, newTreatment);
            //var persistedAfflictions = new OldAfflications
            //{
            //    Afflications = _areaAfflicationService.GetAfflicationAreasByTreatmentId(treatmentId),
            //    TreatmentId = treatmentId
            //};

            //if (persistedAfflictions.Afflications != null && persistedAfflictions.Afflications.Any())
            //{
            //    foreach (var persistedAffliction in persistedAfflictions.Afflications)
            //    {
            //        persistedAffliction.IsPersisted = true;
            //    }
            //}

            return Ok(treatmentId);
            //return Ok(persistedAfflictions);
        }