public async Task <Result <Contract> > Handle(Contract request, CancellationToken cancellationToken) { if (request.CurrentUserRole == "Secretaria") { return(Result.Fail <Contract>("Acesso Negado")); } if (request.UserFiles.Count > 10) { return(Result.Fail <Contract>("O limite de envios é de 10 arquivos")); } foreach (var userFile in request.UserFiles) { var newFile = UserFile.Create( request.CurrentUserId, userFile.Title, userFile.Description, userFile.DownloadLink, userFile.Resource, userFile.ResourceId ).Data; await _db.UserFileCollection.InsertOneAsync( newFile, cancellationToken : cancellationToken ); } return(Result.Ok(request)); }
public async Task <Result <Contract> > Handle(Contract request, CancellationToken cancellationToken) { try { if (request.CurrentUserRole == "Secretaria") { return(Result.Fail <Contract>("Acesso Negado")); } var filesUserId = ObjectId.GenerateNewId(); if ( request.CurrentUserRole == "Student" && filesUserId != request.CurrentUserId ) { return(Result.Fail <Contract>("Acesso Negado")); } if (request.UserFiles.Count > 10) { return(Result.Fail <Contract>("O limite de envios é de 10 arquivos")); } var userFiles = await _db.UserFileCollection.AsQueryable() .Where(t => t.CreatedBy == filesUserId) .ToListAsync(); var currentFilesIds = request.UserFiles .Where(uf => !string.IsNullOrEmpty(uf.Id)) .Select(uf => ObjectId.Parse(uf.Id)); await _db.UserFileCollection.DeleteManyAsync(f => !currentFilesIds.Contains(f.Id), cancellationToken : cancellationToken ); foreach (var userFile in request.UserFiles) { if (!string.IsNullOrEmpty(userFile.Id)) { var userFileId = ObjectId.Parse(userFile.Id); var dbFile = userFiles.FirstOrDefault(f => f.Id == userFileId ); if (dbFile != null) { dbFile.Title = userFile.Title; dbFile.Description = userFile.Description; dbFile.DownloadLink = userFile.DownloadLink; dbFile.Resource = userFile.Resource; await _db.UserFileCollection.ReplaceOneAsync(t => t.Id == dbFile.Id, dbFile, cancellationToken : cancellationToken ); } else { return(Result.Fail <Contract>("Arquivo editado não existe")); } } else { var newFile = UserFile.Create( filesUserId, userFile.Title, userFile.Description, userFile.DownloadLink, userFile.Resource, ObjectId.GenerateNewId().ToString() ).Data; await _db.UserFileCollection.InsertOneAsync( newFile, cancellationToken : cancellationToken ); } } return(Result.Ok(request)); } catch (Exception ex) { return(Result.Fail <Contract>(ex.Message)); } }