public async Task <IActionResult> CreatePayload([FromBody, Required] CreatePayloadRequest request) { var envelope = await _blueprintConverter.Decode(request.Encoded); var firstBlueprint = FirstBlueprintOrDefault(envelope); if (firstBlueprint == null) { return(BadRequest("A blueprint book must contain at least one blueprint")); } var hash = Hash.Compute(request.Encoded); var payload = new Payload( hash, _blueprintConverter.ParseType(envelope.Entity.Item), _blueprintConverter.DecodeGameVersion(envelope.Entity.Version), request.Encoded); var cache = new PayloadCache(); cache.TryAdd(envelope, payload); await cache.EnsureInitializedGraph(envelope); await _buildService.SavePayloadGraph(hash, cache.Values); return(Ok(new CreatePayloadResult { Payload = payload.ToViewModel(Url, envelope, cache), ExtractedSlug = await _slugService.Validate(TryValidateModel, envelope.Entity.Label?.ToSlug(), User.GetUserId()), })); }
public async Task <IActionResult> GetDetails([Required] Hash hash, [FromQuery(Name = "include_children")] bool includeChildren = false) { var payload = await _dbContext.Payloads.AsNoTracking() .Where(v => v.Hash == hash) .FirstOrDefaultAsync(); if (payload == null) { return(NotFound()); } var envelope = await _blueprintConverter.Decode(payload.Encoded); PayloadCache?payloadGraph = null; if (includeChildren) { payloadGraph = new PayloadCache(); payloadGraph.TryAdd(envelope, payload); await payloadGraph.EnsureInitializedGraph(envelope); } return(Ok(payload.ToViewModel(Url, envelope, payloadGraph))); }
private async Task SeedBuilds(IReadOnlyCollection <SeedBlueprint> blueprints) { _logger.LogInformation("Seeding {TotalCount} builds", blueprints.Count); var createdCount = 0; var skippedCount = 0; var errorCount = 0; foreach (var blueprint in blueprints) { var slug = blueprint.Title.ToSlug(); var username = blueprint.Owner.ToSlug(); var existing = await _dbContext.Builds.FirstOrDefaultAsync(bp => bp.OwnerSlug == username && bp.Slug == slug); if (existing != null) { skippedCount++; _logger.LogInformation("Blueprint {Title} by {Owner} already exists", blueprint.Title, blueprint.Owner); continue; } var owner = await _dbContext.Users.FirstOrDefaultAsync(u => u.UserName == username); if (owner == null) { var email = Guid.NewGuid() + "@test.local.factorio.tech"; _logger.LogInformation("Creating user {DisplayName} / {UserName} / {Email}", blueprint.Owner, username, email); owner = new User { DisplayName = blueprint.Owner, UserName = username, NormalizedUserName = username.ToUpperInvariant(), Email = email, NormalizedEmail = email.ToUpperInvariant(), EmailConfirmed = true, RegisteredAt = SystemClock.Instance.GetCurrentInstant(), }; _dbContext.Add(owner); await _dbContext.SaveChangesAsync(); } var envelope = await _blueprintConverter.Decode(blueprint.Encoded); var payload = new Payload( Hash.Compute(blueprint.Encoded), _blueprintConverter.ParseType(envelope.Entity.Item), _blueprintConverter.DecodeGameVersion(envelope.Entity.Version), blueprint.Encoded); var cache = new PayloadCache(); cache.TryAdd(envelope, payload); await cache.EnsureInitializedGraph(envelope); await _buildService.SavePayloadGraph(payload.Hash, cache.Values); var tags = blueprint.Tags .Select(t => t.TrimEnd('/')) .Where(_buildTags.Contains) .ToArray(); var request = new BuildService.CreateRequest( username, slug, blueprint.Title, blueprint.Description, tags.Any() ? tags : GetSomeRandomTags(), (payload.Hash, null, null, envelope.Entity.Icons.ToGameIcons()), null); var principal = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new(ClaimTypes.NameIdentifier, owner.Id.ToString()), }));