private async Task SplitChunk() { var chunk = _chunk.ToArray(); var adding = await _plotlyLineSplitterBackgroundWrapper.Split(chunk); if (adding?.Any() ?? false) { foreach (var item in adding) { if (!_data.ContainsKey(item.Key)) { _data.Add(item.Key, item.Value); } else { _data[item.Key].X.AddRange(item.Value.X); _data[item.Key].Y.AddRange(item.Value.Y); _data[item.Key].Text.AddRange(item.Value.Text); } } var counter = _data.Last().Value.Y.Count; if (counter % 1000 == 0) { _activityLogger?.Add($"Processing: ", counter); _activityLogger?.ShowMessageAlways(); } } }
//====== IRequestHandler public async Task <Unit> Handle(SetAppUserRolesCommand request, CancellationToken cancellationToken) { AppUserEntity appUserEntity = context.Entities .AppUsers .Where(x => x.UserName == request.UserName.Value) .Include(x => x.UserRoles) .FirstOrDefault(); if (appUserEntity is null) { throw new Exception("App user does not exist: " + request.UserName); // TODO: custom exception } appUserEntity.UserRoles.Clear(); foreach (var role in request.UserRoles.Items) { appUserEntity.UserRoles.Add(new UserRolesEntity { RoleName = role.Value }); } await context.Entities.SaveChangesAsync(); logger.Add($"Roles updated for user '{request.UserName}'. New values: " + request.UserRoles); return(Unit.Value); }
//====== IRequestHandler public async Task <Unit> Handle(CreateAppUserCommand request, CancellationToken cancellationToken) { AppUserEntity appUserEntity = await context.Entities .AppUsers .Where(x => x.UserName == request.UserName.Value) .AsNoTracking() .FirstOrDefaultAsync(); if (appUserEntity != null) { throw new Exception("User already exists: " + request.UserName); // TODO: own exception } string hash = passwordHasher.HashPassword(request.UserPassword); var appUser = new AppUser(request.UserName, request.DisplayName, UserRoles.Empty); var appUserDetails = new AppUserDetails(appUser, null, context.UtcNow); var newEntity = new AppUserEntity { UserName = request.UserName.Value, DisplayName = request.DisplayName.Value, EmailAddress = null, PasswordHash = hash, UtcWhenCreated = context.UtcNow.Value }; context.Entities.AppUsers.Add(newEntity); await context.Entities.SaveChangesAsync(); logger.Add("New app user created:" + request.UserName); return(Unit.Value); }
//====== IRequestHandler public async Task <Unit> Handle(CreatePersonCommand request, CancellationToken cancellationToken) { var entity = new PersonEntity { Name = request.Name.Value, DisplayName = request.DisplayName.IsEmpty ? null : request.DisplayName.Value, }; await context.Entities.People.AddAsync(entity); logger.Add("Person created: " + request.Name); return(Unit.Value); }
//====== IRequestHandler public async Task <Unit> Handle(CreateTagCommand request, CancellationToken cancellationToken) { var entity = new TagEntity { Name = request.Name.Value, DisplayName = request.DisplayName.IsEmpty ? null : request.DisplayName.Value, Description = request.Description.IsEmpty ? null : request.Description.Value, UtcWhenAdded = context.UtcNow.Value, CreatedBy = await context.Entities.AppUsers.SingleAsync(x => x.UserName == context.Requestor.UserName.Value) }; await context.Entities.Tags.AddAsync(entity); logger.Add("Tag created: " + request.Name); return(Unit.Value); }
//====== IRequestHandler public async Task <Unit> Handle(UpdateAppUserDetailsCommand request, CancellationToken cancellationToken) { if (request.CanSkipExecution) { return(Unit.Value); } AppUserEntity appUserEntity = await context.Entities .AppUsers .Where(x => x.UserName == request.UserName.Value) .FirstOrDefaultAsync(); if (appUserEntity is null) { throw new Exception("App user does not exist: " + request.UserName); // TODO: custom exception } bool setDisplayName = request.DisplayName != null; if (!request.EmailAddress.Ignore) { appUserEntity.EmailAddress = request.EmailAddress.Data?.Value; } if (setDisplayName) { appUserEntity.DisplayName = request.DisplayName !.Value; } var messages = new List <string>(); if (request.EmailAddress.Ignore == false) { messages.Add($"Email updated for user '{request.UserName}'. New value: " + (request.EmailAddress.Data?.ToString() ?? "<empty>")); } if (setDisplayName) { messages.Add($"Display name updated for user '{request.UserName}'. New value: {request.DisplayName}"); } logger.Add(string.Join("\r\n", messages)); await context.Entities.SaveChangesAsync(); return(Unit.Value); }
//====== IRequestHandler public async Task <Unit> Handle(UpdateTagCommand request, CancellationToken cancellationToken) { if (request.CanSkipExecution) { return(Unit.Value); } TagEntity tagEntity = await context.Entities.Tags.FirstOrDefaultAsync(x => x.Name == request.TagName.Value); if (tagEntity is null) { throw new("Tag not found: " + request.TagName.Value); } if (request.NewName is not null) { tagEntity.Name = request.NewName.Value; } if (request.NewDisplayName is not null) { tagEntity.DisplayName = request.NewDisplayName.IsEmpty ? null : request.NewDisplayName.Value; } if (request.NewDescription is not null) { tagEntity.Description = request.NewDescription.IsEmpty ? null : request.NewDescription.Value; } tagEntity.UtcWhenUpdated = context.UtcNow.Value; int userId = (await context.Entities.AppUsers.FirstAsync(x => x.UserName == context.Requestor.UserName.Value)).Id; tagEntity.UpdatedById = userId; await context.Entities.SaveChangesAsync(); // todo: better logging logger.Add("Tag updated: " + request.TagName); return(Unit.Value); }
public void Post([FromBody] ActivityRecord record) { var entity = new ActivityEntity(record.SessionID, record.UserID) { LogInTime = record.LogInTime, LogOutTime = record.LogOutTime, FurtherTestingCount = record.FurtherTestingCount, ClientVersion = record.ClientVersion, ImportModelCount = record.ImportModelCount, NewModelCount = record.NewModelCount, RunProofCount = record.RunProofCount, RunSimulationCount = record.RunSimulationCount, SaveModelCount = record.SaveModelCount, ProofErrorCount = record.ProofErrorCount, SimulationErrorCount = record.SimulationErrorCount, FurtherTestingErrorCount = record.FurtherTestingErrorCount, AnalyzeLTLCount = record.AnalyzeLTLCount, AnalyzeLTLErrorCount = record.AnalyzeLTLErrorCount }; logger.Add(entity); }
//====== IRequestHandler public async Task <LinkId> Handle(AddFileLinkCommand request, CancellationToken cancellationToken) { var newEntity = new FileLinkEntity { //FileId = request.FileId.Value, File = context.Entities.Files.FirstOrDefault(x => x.Id == request.FileId.Value), Address = request.Address.Value, Title = request.Title?.Value, Comment = request.Comment?.Value, UtcWhenAdded = context.UtcNow.Value, Issuer = context.Entities.AppUsers.FirstOrDefault(x => x.UserName == context.Requestor.UserName.Value) }; await context.Entities.FileLinks.AddAsync(newEntity); logger.Add("Created new file link: " + newEntity.Address + " #" + newEntity.Id); return(new LinkId(newEntity.Id)); }
//====== IRequestHandler public async Task <Unit> Handle(UpdateFileDetailsCommand request, CancellationToken cancellationToken) { if (request.CanSkipExecution) { return(Unit.Value); } var entity = context.Entities.Files.Find(request.FileId.Value); if (entity is null) { throw new Exception("File not found."); } var messages = new List <string>(); if (request.FileTitle != null) { entity.Title = request.FileTitle.Value; messages.Add($"Description updated for file #{request.FileId}. New value: {request.Description}"); } if (request.Description != null) { entity.Description = request.Description.Value; messages.Add($"Title updated for file #{request.FileId}. New value: {request.FileTitle.Value}"); } if (request.PrimaryDateTime != null) { entity.PrimaryDateTime = request.PrimaryDateTime.ToSpecialString(); messages.Add($"Primary date time updated for file #{request.FileId}. New value: {request.PrimaryDateTime}"); } await context.Entities.SaveChangesAsync(); logger.Add(string.Join("\r\n", messages)); return(Unit.Value); }
public async Task <Unit> Handle(SetTagsForFileCommand request, CancellationToken cancellationToken) { FileEntity?file = context.Entities.Files.Include(x => x.Tags).FirstOrDefault(x => x.Id == request.FileId); if (file is null) { throw new("File not found, id: " + request.FileId); } IEnumerable <string> names = request.Tags.Select(x => x.Value); IReadOnlyList <TagEntity> tags = context.Entities.Tags.Where(x => names.Contains(x.Name)).ToList(); foreach (TagEntity tag in tags) { file.Tags !.Add(tag); } await context.Entities.SaveChangesAsync(); // todo: message logger.Add("Added tags to file id: " + request.FileId + ". Tags = " + string.Join(", ", request.Tags)); return(await Unit.Task); }
public async Task <UploadId> Handle(UploadFilesCommand request, CancellationToken cancellationToken) { AppUserEntity?uploader = await context.Entities.AppUsers.FirstOrDefaultAsync(x => x.UserName == context.Requestor.UserName.Value); if (uploader is null) { throw new ArgumentException("Invalid requestor."); // todo: custom exception } UtcTimestamp startTimestamp = context.UtcNow; var tempFiles = new List <FileEntity>(); var duplicates = new List <string>(); foreach (SourceFile sourceFile in request.Parameters.SourceFiles) { UtcTimestamp timestamp = context.UtcNow; Sha256Hash hash = sha256Generator.GenerateHash(sourceFile.Content); if (IsDuplicate(hash)) { duplicates.Add(sourceFile.OrginalFileName ?? "???"); } else { string newFileName = FileUtils.GetRandomFileNameWithTimestamp(timestamp.Value, sourceFile.OrginalFileName); IFileInfo fileInfo = fileDatabase.SourceFiles.Create(sourceFile.Content, new FileName(newFileName)); ThumbnailMakerResult?thumbResult = thumbnailMaker.TryMakeAndSaveThumbnail(fileInfo, timestamp); var fileEntity = new FileEntity { MimeType = sourceFile.MimeType.Value, FileName = sourceFile.OrginalFileName, UtcWhenAdded = timestamp.Value, SourceFileName = newFileName, SourceFileSize = sourceFile.Content.Length, Description = null, Title = null, Hash = hash.ToHexString(), PrimaryDateTime = null, Image = new ImageEntity() { Width = thumbResult?.OrginalImageSize.Width, Height = thumbResult?.OrginalImageSize.Height, ThumbnailFileName = thumbResult?.ThumbnailFileName.Value, }, Uploader = uploader }; tempFiles.Add(fileEntity); } } var uploadEntity = new UploadEntity { Description = request.Parameters.Description.Value, UtcWhenAdded = startTimestamp.Value, FileCount = tempFiles.Count, TotalSize = tempFiles.Select(x => x.SourceFileSize).Aggregate(0L, (a, b) => a + b), Uploader = uploader }; foreach (var file in tempFiles) { file.Upload = uploadEntity; // TODO: handle 0 files as error } context.Entities.Files.AddRange(tempFiles); await context.Entities.SaveChangesAsync(); logger.Add($"New upload #{uploadEntity.Id}. Files added (except duplicates): {uploadEntity.FileCount}"); return(new UploadId(uploadEntity.Id)); }