コード例 #1
0
ファイル: SetupController.cs プロジェクト: null-d3v/diov
    public async Task <IActionResult> Import(
        [FromForm] SetupModel setupModel,
        CancellationToken cancellationToken = default)
    {
        if (!ModelState.IsValid)
        {
            Response.StatusCode = StatusCodes
                                  .Status400BadRequest;
            return(View(nameof(Index)));
        }

        var contents = await JsonSerializer
                       .DeserializeAsync <IEnumerable <Content> >(
            setupModel.ImportFile.OpenReadStream(),
            JsonSerializerOptions,
            cancellationToken);

        if (contents != null)
        {
            foreach (var content in contents)
            {
                var existingContent = await ContentAccessor
                                      .GetContentAsync(
                    content.Path,
                    cancellationToken);

                if (existingContent != null)
                {
                    content.Id = existingContent.Id;
                    await ContentAccessor.UpdateContentAsync(
                        content, cancellationToken);
                }
                else
                {
                    await ContentAccessor.AddContentAsync(
                        content, cancellationToken);
                }
            }
        }

        return(RedirectToAction(
                   "Index",
                   "Content"));
    }