public void UpdatePerson(Person person, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                var personBusinessRules = new PersonBusinessRules();
                var results = personBusinessRules.Validate(person);

                bool validationSucceeded = results.IsValid;
                var failures = results.Errors;

                if (validationSucceeded == false)
                {
                    transaction = ValidationErrors.PopulateValidationErrors(failures);
                    return;
                }

                _personDataService.CreateSession();
                _personDataService.BeginTransaction();

                var existingPerson = _personDataService.GetPerson(person.PersonID);

                existingPerson.CompanyName = person.CompanyName;
                existingPerson.Name = person.Name;
                existingPerson.Country = person.Country;
                existingPerson.City = person.City;
                existingPerson.Address = person.Address;
                existingPerson.MobileNumber = person.MobileNumber;
                existingPerson.Image = person.Image;

                _personDataService.UpdatePerson(person);
                _personDataService.CommitTransaction(true);

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add("Person was successfully updated.");

            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }


        }
Esempio n. 2
0
        public static TransactionalInformation PopulateValidationErrors(IList<ValidationFailure> failures)
        {
            var transaction = new TransactionalInformation {ReturnStatus = false};

            foreach (var error in failures)
            {
                if (transaction.ValidationErrors.ContainsKey(error.PropertyName) == false)
                {
                    transaction.ValidationErrors.Add(error.PropertyName, error.ErrorMessage);
                }

                transaction.ReturnMessage.Add(error.ErrorMessage);                       
            }

            return transaction;
        }
        public Person CreatePerson(Person person, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                var personBusinessRules = new PersonBusinessRules();
                var results = personBusinessRules.Validate(person);

                bool validationSucceeded = results.IsValid;
                var failures = results.Errors;

                if (validationSucceeded == false)
                {
                    transaction = ValidationErrors.PopulateValidationErrors(failures);
                    return person;
                }

                _personDataService.CreateSession();
                _personDataService.BeginTransaction();
                _personDataService.CreatePerson(person);
                _personDataService.CommitTransaction(true);

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add("Person successfully created.");

            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }

            return person;


        }
        public void ActivatePerson(int personID, bool isActive, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                _personDataService.CreateSession();
                _personDataService.BeginTransaction();

                _personDataService.ActivatePerson(_personDataService.GetPerson(personID), isActive);
                _personDataService.CommitTransaction(true);

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add(string.Format("Person was successfully {0}.", isActive ? "activate" : "deactivate"));
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }
        }
        public void DeletePerson(int personID, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                _personDataService.CreateSession();
                _personDataService.BeginTransaction();
                
                _personDataService.DeletePerson(_personDataService.GetPerson(personID));
                _personDataService.CommitTransaction(true);

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add("Person was successfully deleted.");
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }
        }
        public Person GetPerson(int personID, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            var person = new Person();

            try
            {

                _personDataService.CreateSession();
                person = _personDataService.GetPerson(personID);
                _personDataService.CloseSession();      
                transaction.ReturnStatus = true;

            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }

            return person;

        }
        public List<Person> GetPersons(int currentPageNumber, int pageSize, string sortExpression, string sortDirection, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            var persons = new List<Person>();

            try
            {
                int totalRows;

                _personDataService.CreateSession();
                persons = _personDataService.GetPersons(currentPageNumber, pageSize, sortExpression, sortDirection, out totalRows);
                _personDataService.CloseSession();

                transaction.TotalPages = Utility.CalculateTotalPages(totalRows, pageSize);
                transaction.TotalRows = totalRows;

                transaction.ReturnStatus = true;

            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _personDataService.CloseSession();
            }

            return persons;

        }