public async Task <IActionResult> Update(CancellationToken cancellationToken, Guid id, [FromForm] TrainingVideoUpdateModel model) { var userAgent = Request.UserAgent(); var command = new TrainingVideoUpdateCommand <TrainingVideoUpdateModel, Guid>(id, model, null, userAgent); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); return(ObjectResult(result, StatusCodes.Status200OK)); }
protected override async Task <TrainingVideoReadModel> ProcessAsync(TrainingVideoUpdateCommand <TrainingVideoUpdateModel, Guid> message, CancellationToken cancellationToken) { if (message.Id == null) { throw new DomainException(422, $"Id is null , Please try again."); } var current = await _dataContext.TrainingVideo.GetByKeyAsync (message.Id); current = current == null?_mapper.Map <Data.Entities.TrainingVideo>(message.TrainingVideo) : _mapper.Map(message.TrainingVideo, current); CreateResult result = new CreateResult(); result = _mapper.Map <CreateResult>(_dataContext.FindFolder(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name })); if (result == null) { result = _dataContext.CreateDir(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name, ParentPath = null }); } if (message.TrainingVideo.File != null) { if (message.TrainingVideo.File.Length > 0) { var file = message.TrainingVideo.File; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); using (var ms = new MemoryStream()) { file.CopyTo(ms); var now = DateTimeOffset.Now; var payment = new PaymentTransactionPic() { Name = Guid.NewGuid() + "_" + message.TrainingVideo.File.FileName, File_stream = ms.ToArray(), ParentPath = result.Path, Creation_time = now, Last_access_time = now, Last_write_time = now, Stream_id = current == null?Guid.NewGuid() : current.FileId }; result = _dataContext.UpdateFile(payment); } _mapper.Map(result, current); } } if (result != null) { if (current.Id == Guid.Empty) { var dbSet = _dataContext.Set <Data.Entities.TrainingVideo>(); await dbSet.AddAsync(current, cancellationToken).ConfigureAwait(false); } var status = await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); if (status != 0) { return(_mapper.Map <TrainingVideoReadModel>(current)); } else { Logger.LogWarning($"Unable to update training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update training video, please try again later or contact administrator."); } } else { Logger.LogWarning($"Unable to update training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update training video, please try again later or contact administrator."); } }