public async Task <ISaida> CadastrarAnexo(int idLancamento, LancamentoAnexoEntrada entrada) { // Verifica se as informações para cadastro foram informadas corretamente if (entrada.Invalido) { return(new Saida(false, entrada.Mensagens, null)); } var lancamento = await _lancamentoRepositorio.ObterPorId(idLancamento); // Verifica se o lançamento existe this.NotificarSeNulo(lancamento, LancamentoMensagem.Id_Lancamento_Nao_Existe); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } // Insere as informações do anexo no banco de dados e realiza o upload do arquivo para o Google Drive var anexo = await _anexoRepositorio.Inserir(idLancamento, lancamento.Data, entrada); if (_anexoRepositorio.Invalido) { return(new Saida(false, _anexoRepositorio.Mensagens, null)); } await _uow.Commit(); return(new Saida(true, new[] { LancamentoAnexoMensagem.Anexo_Cadastrado_Com_Sucesso }, new LancamentoAnexoSaida(anexo))); }
public LancamentoAnexo(int idLancamento, LancamentoAnexoEntrada entrada, string idGoogleDrive) { if (entrada.Invalido) { return; } this.IdLancamento = idLancamento; this.IdGoogleDrive = idGoogleDrive; this.Descricao = entrada.Descricao; this.NomeArquivo = entrada.NomeArquivo; }
/// <summary> /// Realiza o upload de um arquivo /// </summary> private async Task <string> RealizarUploadAnexo(DateTime dataLancamento, LancamentoAnexoEntrada cadastroEntrada) { if (_apiGoogleDriveProxy.Invalido) { this.AdicionarNotificacoes(_apiGoogleDriveProxy.Notificacoes); return(null); } this.NotificarSeNuloOuVazio(_idPastaGoogleDrive, "O ID da pasta no Google Drive não foi informado."); if (this.Invalido) { return(null); } var periodo = await _periodoRepositorio.ObterPorData(dataLancamento, cadastroEntrada.IdUsuario); Google.Apis.Drive.v3.Data.File pastaPeriodo; if (periodo != null) { // Caso exista um período, é criado uma pasta com o nome do período pastaPeriodo = await _apiGoogleDriveProxy.CriarPasta(periodo.Nome, _idPastaGoogleDrive); } else { // Caso não exista um período, é criado uma pasta com a partir do mês/ano do lançamento pastaPeriodo = await _apiGoogleDriveProxy.CriarPasta(dataLancamento.ToString("MM/yyyy"), _idPastaGoogleDrive); } // Verifica se um arquivo com o mesmo nome já existe na pasta do mês do lançamento var anexoJaExistente = await _apiGoogleDriveProxy.ProcurarPorNome(ApiGoogleDriveProxy.TipoGoogleDriveFile.Arquivo, cadastroEntrada.NomeArquivo, pastaPeriodo.Id); this.NotificarSeNaoNulo(anexoJaExistente, LancamentoAnexoMensagem.Nome_Arquivo_Ja_Existe_Google_Drive); if (this.Invalido) { return(null); } // Realiza o upload do arquivo return(await _apiGoogleDriveProxy.RealizarUpload(cadastroEntrada.NomeArquivo, cadastroEntrada.MimeTypeArquivo, cadastroEntrada.ConteudoArquivo, cadastroEntrada.Descricao, pastaPeriodo.Id)); }
public async Task <IActionResult> CadastrarAnexo( [FromQuery, SwaggerParameter("ID do lançamento.", Required = true)] int idLancamento, [FromForm, SwaggerParameter("Informações de cadastro do anexo.", Required = true)] LancamentoAnexoViewModel model) { LancamentoAnexoEntrada entrada; using (var memoryStream = new MemoryStream()) { await model.Arquivo.CopyToAsync(memoryStream); entrada = new LancamentoAnexoEntrada( base.ObterIdUsuarioClaim(), model.Descricao, model.NomeArquivo + model.Arquivo.FileName.Substring(model.Arquivo.FileName.LastIndexOf(".")), memoryStream.ToArray(), model.Arquivo.ContentType); } return(new ApiResult(await _lancamentoServico.CadastrarAnexo(idLancamento, entrada))); }
public async Task <LancamentoAnexo> Inserir(int idLancamento, DateTime dataLancamento, LancamentoAnexoEntrada cadastroEntrada) { // Realiza o upload do arquivo do anexo para o Google Drive var idGoogleDrive = await RealizarUploadAnexo(dataLancamento, cadastroEntrada); if (this.Invalido) { return(null); } var anexo = new LancamentoAnexo(idLancamento, cadastroEntrada, idGoogleDrive); await _efContext.AddAsync(anexo); return(anexo); }