public async Task <ApiResult <bool> > InsertOrUpdateFavoriteTree(FavoriteTree favoriteTree)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();
                _logger.LogInformation("Insert or update favorite tree");

                var result = await _treeService.InsertOrUpdateFavoriteTree(favoriteTree);

                _logger.LogInformation("Insert or update favorite tree");

                stopwatch.Stop();
                result.ExecutionTime = stopwatch.Elapsed.TotalMilliseconds;
                _logger.LogInformation($"Execution time: {result.ExecutionTime}ms");

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Insert or update favorite tree error: {ex}");

                return(new ApiResult <bool>
                {
                    Result = false,
                    ApiCode = ApiCode.UnknownError,
                    ErrorMessage = ex.ToString()
                });
            }
        }
예제 #2
0
        public async Task <ApiResult <bool> > InsertOrUpdateFavoriteTree(FavoriteTree favoriteTree)
        {
            if (favoriteTree == null)
            {
                _logger.LogError("FavoriteTree is null");
                return(new ApiResult <bool>
                {
                    ApiCode = ApiCode.NullObject,
                    ErrorMessage = "FavoriteTree inputted is null",
                    Result = false
                });
            }

            var existing = await _unitOfWork.FavoriteTreeRepository.GetFirstOrDefault(x => x.Id == favoriteTree.Id);

            if (existing == null)
            {
                _logger.LogInformation($"FavoriteTree to insert: {JsonConvert.SerializeObject(favoriteTree)}");
                var insertResult = await _unitOfWork.FavoriteTreeRepository.Insert(favoriteTree);

                return(new ApiResult <bool>
                {
                    Result = insertResult,
                    ApiCode = ApiCode.Success
                });
            }

            _logger.LogInformation($"FavoriteTree to update: {JsonConvert.SerializeObject(favoriteTree)}");

            existing.IsFavorite = favoriteTree.IsFavorite;

            var updateResult = await _unitOfWork.FavoriteTreeRepository.Update(existing);

            return(new ApiResult <bool>
            {
                Result = updateResult,
                ApiCode = ApiCode.Success
            });
        }