Example #1
0
        public async Task <OptionEntity> UpsertOption(OptionEntityCache cache, OptionEntity option)
        {
            // TODO: [TESTS] (OptionsService.UpsertOption) Add tests
            var category  = option.OptionCategory;
            var optionKey = option.OptionKey;
            var userId    = option.UserId;
            var dbOption  = cache.GetCachedEntry(option)
                            ?? await _optionRepo.GetRawOption(category, optionKey, userId);

            if (dbOption == null)
            {
                if (await _optionRepo.Add(option) == 0)
                {
                    return(null);
                }
            }
            else
            {
                option.OptionId = dbOption.OptionId;
                option.UserId   = dbOption.UserId;

                if (await _optionRepo.Update(option) == 0)
                {
                    return(null);
                }
            }

            dbOption = await _optionRepo.GetRawOption(category, optionKey, userId);

            cache.CacheEntry(dbOption);
            return(dbOption);
        }
        /// <summary>
        /// Update properties and fields on a <see cref="T:Domain.Entities.Option"/>.
        /// </summary>
        /// <param name="entity"><see cref="T:Domain.Entities.Option"/></param>
        /// <returns>boolean value</returns>
        public bool Update(Option entity)
        {
            if (entity.Id <= 0)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(entity.Text) || string.IsNullOrWhiteSpace(entity.Text))
            {
                return(false);
            }
            bool OptionTextExist = _dataRepository.Exist(entity.Text);

            if (OptionTextExist)
            {
                return(false);
            }
            bool response = _dataRepository.Update(entity);

            return(response);
        }