public async Task <ApiResult <bool> > UpdatePlantingProcess(int id, PlantingProcess plantingProcess)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();
                _logger.LogInformation("Update plantingProcess");

                plantingProcess.Id = id;

                var result = await _plantingProcessService.UpdatePlantingProcess(plantingProcess);

                _logger.LogInformation("Update plantingProcess complete");

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

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Update plantingProcess error: {ex}");

                return(new ApiResult <bool>
                {
                    Result = false,
                    ApiCode = ApiCode.UnknownError,
                    ErrorMessage = ex.ToString()
                });
            }
        }
Exemplo n.º 2
0
        public async Task <ApiResult <bool> > InsertPlantingProcess(PlantingProcess plantingProcess)
        {
            if (plantingProcess == null)
            {
                _logger.LogError("plantingProcess is null");
                return(new ApiResult <bool>
                {
                    ApiCode = ApiCode.NullObject,
                    ErrorMessage = "plantingProcess is null",
                    Result = false
                });
            }

            _logger.LogInformation($"Planting process to insert: {JsonConvert.SerializeObject(plantingProcess)}");

            var processSteps = new List <ProcessStep>();

            for (var date = plantingProcess.StartDate.Date;
                 date <= plantingProcess.HarvestDate.Date;
                 date = date.AddDays(1))
            {
                processSteps.Add(new ProcessStep
                {
                    StartDate = date.Date,
                    CreatedAt = DateTime.UtcNow,
                });
            }

            plantingProcess.CreatedAt    = DateTime.UtcNow;
            plantingProcess.ProcessSteps = processSteps;

            var result = await _unitOfWork.PlantingProcessRepository.Insert(plantingProcess);

            return(new ApiResult <bool>
            {
                ApiCode = ApiCode.Success,
                Result = result
            });
        }
Exemplo n.º 3
0
        public async Task <ApiResult <bool> > UpdatePlantingProcess(PlantingProcess plantingProcess)
        {
            if (plantingProcess == null)
            {
                _logger.LogError("plantingProcess is null");
                return(new ApiResult <bool>
                {
                    ApiCode = ApiCode.NullObject,
                    ErrorMessage = "plantingProcess inputted is null",
                    Result = false
                });
            }

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

            var existing = await _unitOfWork.PlantingProcessRepository
                           .GetQueryable()
                           .Include(x => x.ProcessSteps)
                           .FirstOrDefaultAsync(x => x.Id == plantingProcess.Id);

            if (existing == null)
            {
                _logger.LogError($"plantingProcess not found with id: {plantingProcess.Id}");
                return(new ApiResult <bool>
                {
                    ApiCode = ApiCode.NotFound,
                    ErrorMessage = $"plantingProcess not found with id: {plantingProcess.Id}",
                    Result = false
                });
            }

            existing.FloweringDate   = plantingProcess.FloweringDate;
            existing.GerminationDate = plantingProcess.GerminationDate;
            existing.VegetativeDate  = plantingProcess.VegetativeDate;
            existing.StartDate       = plantingProcess.StartDate;
            existing.HarvestDate     = plantingProcess.HarvestDate;
            existing.UserId          = plantingProcess.UserId;
            existing.TreeId          = plantingProcess.TreeId;
            existing.UpdatedAt       = DateTime.UtcNow;

            for (var date = plantingProcess.StartDate.Date;
                 date <= plantingProcess.HarvestDate.Date;
                 date = date.AddDays(1))
            {
                if (existing.ProcessSteps.Any(x => x.StartDate == date))
                {
                    continue;
                }

                existing.ProcessSteps.Add(new ProcessStep
                {
                    StartDate = date.Date,
                    CreatedAt = DateTime.UtcNow,
                });
            }

            existing.ProcessSteps = existing.ProcessSteps.OrderBy(x => x.StartDate).ToList();

            var result = await _unitOfWork.PlantingProcessRepository.Update(existing);

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