/// <inheritdoc/>
        public async Task <ServiceResponse <TResult> > GetAsync <TResult>(string cacheKey)
        {
            string cacheResult = null;

            try
            {
                cacheResult = await distributedCache
                              .GetStringAsync(cacheKey)
                              .ConfigureAwait(false);

                if (string.IsNullOrWhiteSpace(cacheResult))
                {
                    return(ServiceResponseHelper.WithResult(default(TResult)));
                }

                var settings           = new JsonSerializerSettings().GetJsonSerializerSettingsWithPrivateCamelCaseSerializer();
                var deserializedObject = JsonConvert.DeserializeObject <TResult>(cacheResult, settings);
                return(ServiceResponseHelper.WithResult(deserializedObject));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"[{nameof(CacheHelper)}] Fail using {nameof(GetAsync)} for result {typeof(TResult).FullName}: {ex.Message}");
                return(ServiceResponseHelper.WithError <TResult>(new Error {
                    Message = HttpMessage.CACHE_MESSAGE_FAIL, Exception = ex
                }));
            }
        }
        /// <inheritdoc/>
        public async Task <ServiceResponse <byte[]> > GetByteAsync(string cacheKey)
        {
            byte[] cacheResult = null;

            try
            {
                cacheResult = await distributedCache
                              .GetAsync(cacheKey)
                              .ConfigureAwait(false);

                if (cacheResult == null)
                {
                    return(ServiceResponseHelper.WithResult(default(byte[])));
                }

                return(ServiceResponseHelper.WithResult(cacheResult));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"[{nameof(CacheHelper)}] Fail using {nameof(GetByteAsync)} for result {typeof(byte[]).FullName}: {ex.Message}");
                return(ServiceResponseHelper.WithError <byte[]>(new Error {
                    Message = HttpMessage.CACHE_MESSAGE_FAIL, Exception = ex
                }));
            }
        }
Example #3
0
        public async Task <ServiceResponse <Guid> > CreateAsync(Customer customer)
        {
            var parameters = new
            {
                name      = customer.Name,
                birthDate = customer.BirthDate,
                email     = customer.Contacts.Email,
                phone     = customer.Contacts.Phone,
                createdAt = customer.CreatedAt
            };

            Guid result;

            using (var con = sqlDatabase.Connection)
            {
                result = await Task.FromResult(con.ExecuteScalar <Guid>(CommandQuery.CreateCustomer, parameters))
                         .ConfigureAwait(false);
            }

            return(ServiceResponseHelper.WithResult(result));
        }