public async Task <ApiResult <bool> > InsertPlantingEnvironment(PlantingEnvironment plantingEnvironment) { if (plantingEnvironment == null) { _logger.LogError("plantingEnvironment is null"); return(new ApiResult <bool> { ApiCode = ApiCode.NullObject, ErrorMessage = "plantingEnvironment inputted is null", Result = false }); } if (plantingEnvironment.UserId == null) { _logger.LogError($"User id is null for {JsonConvert.SerializeObject(plantingEnvironment)}"); return(new ApiResult <bool> { ApiCode = ApiCode.NullObject, ErrorMessage = "User id is null", Result = false }); } _logger.LogInformation($"Insert planting environment: {JsonConvert.SerializeObject(plantingEnvironment)}"); var insertResult = await _unitOfWork.PlantingEnvironmentRepository.Insert(plantingEnvironment); return(new ApiResult <bool> { Result = insertResult, ApiCode = ApiCode.Success }); }
public async Task <ApiResult <bool> > UpdatePlantingEnvironment(int id, PlantingEnvironment plantingEnvironment) { try { var stopwatch = Stopwatch.StartNew(); _logger.LogInformation("Update plantingEnvironment"); plantingEnvironment.Id = id; var result = await _plantingEnvironmentService.UpdatePlantingEnvironment(plantingEnvironment); _logger.LogInformation("Update plantingEnvironment complete"); stopwatch.Stop(); result.ExecutionTime = stopwatch.Elapsed.TotalMilliseconds; _logger.LogInformation($"Execution time: {result.ExecutionTime}ms"); return(result); } catch (Exception ex) { _logger.LogInformation($"Update plantingEnvironment error: {ex}"); return(new ApiResult <bool> { Result = false, ApiCode = ApiCode.UnknownError, ErrorMessage = ex.ToString() }); } }
public async Task <ApiResult <bool> > UpdatePlantingEnvironment(PlantingEnvironment plantingEnvironment) { if (plantingEnvironment == null) { _logger.LogError("plantingEnvironment is null"); return(new ApiResult <bool> { ApiCode = ApiCode.NullObject, ErrorMessage = "plantingEnvironment inputted is null", Result = false }); } _logger.LogInformation($"plantingEnvironment to update: {JsonConvert.SerializeObject(plantingEnvironment)}"); var existing = await _unitOfWork.PlantingEnvironmentRepository.GetFirstOrDefault(x => x.Id == plantingEnvironment.Id); if (existing == null) { _logger.LogError($"plantingEnvironment not found with id: {plantingEnvironment.Id}"); return(new ApiResult <bool> { ApiCode = ApiCode.NotFound, ErrorMessage = $"plantingEnvironment not found with id: {plantingEnvironment.Id}", Result = false }); } existing.Name = plantingEnvironment.Name; existing.Country = plantingEnvironment.Country; existing.Humidity = plantingEnvironment.Humidity; existing.Temperature = plantingEnvironment.Temperature; existing.ExposureTime = plantingEnvironment.ExposureTime; existing.Length = plantingEnvironment.Length; existing.Width = plantingEnvironment.Width; existing.Light = plantingEnvironment.Light; existing.Light = plantingEnvironment.Light; existing.EnvironmentType = plantingEnvironment.EnvironmentType; existing.UpdatedAt = DateTime.UtcNow; var result = await _unitOfWork.PlantingEnvironmentRepository.Update(existing); return(new ApiResult <bool> { Result = result, ApiCode = ApiCode.Success }); }