private void ValidateCreateShrimpCropManagementFactor(CreateShrimpCropManagementFactorDto dto, long ShrimpCropFromDate, long ShrimpCropToDate) { if (dto.ManagementFactor == null || dto.Curator == null || dto.Frequency == null // OneTime || (dto.Frequency.Code == ShrimpCropFrequency.Onetime.ToString() && (dto.ExecutionTime == null || dto.ExecutionTime < ShrimpCropFromDate || dto.ExecutionTime > ShrimpCropToDate)) // Daily || (dto.Frequency.Code == ShrimpCropFrequency.Daily.ToString() && (dto.FromDate == null || dto.ToDate == null || dto.FromDate < ShrimpCropFromDate || dto.FromDate > ShrimpCropToDate || dto.ToDate < ShrimpCropFromDate || dto.ToDate > ShrimpCropToDate || dto.FromDate > dto.ToDate))) { throw new BusinessException("Invalid parameter!", ErrorCode.INVALID_PARAMETER); } }
public async Task <Guid> CreateOrUpdateShrimpCropManagementFactor(CreateShrimpCropManagementFactorDto dto) { _logger.LogInformation("start method create or update shirmp crop management factor"); var shrimpCrop = await GetShrimpCropById(dto.ShrimpCropId); ValidateCreateShrimpCropManagementFactor(dto, shrimpCrop.FromDate, shrimpCrop.ToDate); DateTime now = DateTime.UtcNow; using (IDbTransaction transaction = this.DatabaseConnectService.BeginTransaction()) { try { ShrimpCropManagementFactor shrimpCropManagementFactor = dto.ToShrimpCropManagementFactor(); shrimpCropManagementFactor.CreatedBy = _sessionService.UserId; shrimpCropManagementFactor.CreatedAt = now; if (dto.IsCreateWork) { shrimpCropManagementFactor.Status = CropFactorStatus.HasWork.ToString(); if (dto.Id == null) { shrimpCropManagementFactor.Id = Guid.NewGuid(); await this.DatabaseConnectService.Connection.InsertAsync <ShrimpCropManagementFactor>(shrimpCropManagementFactor, x => x.AttachToTransaction(transaction)); } await this.DatabaseConnectService.Connection.UpdateAsync <ShrimpCropManagementFactor>(shrimpCropManagementFactor, x => x.AttachToTransaction(transaction)); // create work shrimpCropManagementFactor.ShrimpCrop = shrimpCrop.ToShrimpCrop(); await CreateWork(shrimpCropManagementFactor, transaction); // create notification Notification notification = shrimpCropManagementFactor.ToNotification(); notification.CreatedAt = now; await this.DatabaseConnectService.Connection.InsertAsync <Notification>(notification, x => x.AttachToTransaction(transaction)); } else { shrimpCropManagementFactor.Id = Guid.NewGuid(); await this.DatabaseConnectService.Connection.InsertAsync <ShrimpCropManagementFactor>(shrimpCropManagementFactor, x => x.AttachToTransaction(transaction)); } transaction.Commit(); _logger.LogInformation("end method create or update shirmp crop management factor"); return(shrimpCropManagementFactor.Id); } catch (Exception) { transaction.Rollback(); throw; } } }
public static ShrimpCropManagementFactor ToShrimpCropManagementFactor(this CreateShrimpCropManagementFactorDto dto) { if (dto == null) { return(null); } ShrimpCropManagementFactor entity = new ShrimpCropManagementFactor(); entity.Id = (Guid)(dto.Id == null ? Guid.Empty : dto.Id); entity.ShrimpCropId = dto.ShrimpCropId; entity.Frequency = dto.Frequency.Code; entity.ManagementFactorId = dto.ManagementFactor.Id; entity.Curator = dto.Curator.Id; entity.Frequency = dto.Frequency.Code; entity.ExecutionTime = dto.ExecutionTime.FromUnixTimeStamp(); entity.FromDate = dto.FromDate == null ? null : dto.FromDate.FromUnixTimeStamp(); entity.ToDate = dto.ToDate == null ? null : dto.ToDate.FromUnixTimeStamp(); return(entity); }
public async Task <BaseResponse <Guid> > CreateOrUpdateShrimpCropManagementFactor([FromBody] CreateShrimpCropManagementFactorDto dto) { if (dto == null) { throw new BusinessException("Invalid parameter!", ErrorCode.INVALID_PARAMETER); } var response = new BaseResponse <Guid> { Data = await _shrimpCropService.CreateOrUpdateShrimpCropManagementFactor(dto), Status = true }; return(await Task.FromResult(response)); }