Пример #1
0
        public async Task <SaveResponse> SaveAsync(IUser user)
        {
            var response = new SaveResponse();

            try
            {
                var validateResponse = await _userValidator.ValidateAsync(user);

                if (validateResponse.IsValid)
                {
                    response = await _userRepository.SaveAsync(user);
                }
                else
                {
                    response.AddErrors(validateResponse.Errors);
                }
            }
            catch (Exception e)
            {
                response.AddError(e);
                Console.WriteLine(e);
            }

            return(response);
        }
Пример #2
0
        public async Task <SaveResponse <IContact> > SaveAsync(IContact model)
        {
            var saveResponse = new SaveResponse <IContact>();

            try
            {
                // Make sure contact is valid before saving
                var validationResult = await _contactValidator.ValidateAsync(model);

                if (validationResult.IsValid)
                {
                    saveResponse = await _contactRepository.SaveAsync(model);
                }
                else
                {
                    saveResponse.AddErrors(validationResult.Errors);
                    saveResponse.Message = string.Join <ValidationFailure>(". \n", validationResult.Errors.ToArray());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                saveResponse.AddError(ex);
                _logManager.LogError(ex, "Error saving contact");
            }

            return(saveResponse);
        }
Пример #3
0
        public virtual async Task <SaveResponse <IReadOnlyList <TModel> > > SaveAllAsync(IReadOnlyList <TModel> items)
        {
            var response = new SaveResponse <IReadOnlyList <TModel> >();

            try
            {
                var savedItems = new List <TModel>();
                using (var connection = ConnectionFactory.GetConnection())
                {
                    var sql   = GetMergeStatement();
                    var trans = connection.BeginTransaction();

                    foreach (var item in items)
                    {
                        var singleResponse = await SaveAsync(item);

                        if (singleResponse.IsSuccessful)
                        {
                            savedItems.Add(singleResponse.Content);
                        }
                        else
                        {
                            response.AddErrors(singleResponse.Errors);
                        }
                    }

                    trans.Commit();
                    response.Content = savedItems;
                    response.Message = $@"{savedItems.Count} {typeof(TModel).Name} records were inserted/updated.";
                }
            }
            catch (Exception e)
            {
                var message = $@"Error saving {typeof(TModel).Name} information.";
                LogManager.LogError(e, message);
                response.AddError(e);
                Console.WriteLine(e);
            }

            return(response);
        }
Пример #4
0
        public async Task <SaveResponse <IReadOnlyList <IContact> > > SaveAllAsync(IReadOnlyList <IContact> modelList)
        {
            var saveResponse = new SaveResponse <IReadOnlyList <IContact> >();

            try
            {
                var isValid  = true;
                var messages = new List <string>();

                foreach (var model in modelList)
                {
                    // Make sure contacts are valid before saving
                    var validationResult = await _contactValidator.ValidateAsync(model);

                    if (!validationResult.IsValid)
                    {
                        messages.Add(string.Join <ValidationFailure>(". \n", validationResult.Errors.ToArray()));
                        isValid = false;
                    }
                    saveResponse.AddErrors(validationResult.Errors);
                }

                if (isValid)
                {
                    saveResponse = await _contactRepository.SaveAllAsync(modelList);
                }
                else
                {
                    saveResponse.Message = string.Join <string>(". \n", messages.ToArray());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                saveResponse.AddError(ex);
                _logManager.LogError(ex, "Error saving contacts");
            }

            return(saveResponse);
        }