public async Task <IActionResult> CreateVersion(
            [FromRoute(Name = "score_name")] string scoreName,
            [FromForm] NewScoreVersion newScoreVersion)
        {
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var images  = newScoreVersion.Images;
            var nosText = newScoreVersion.Nos;

            var keyCount = images.GroupBy(x => x.FileName, x => x)
                           .Select(x => (filename: x.Key, count: x.Count()))
                           .Where(x => 2 <= x.count)
                           .ToImmutableArray();

            if (keyCount.Any())
            {
                var errorMessage = string.Join(Environment.NewLine, new string[]
                {
                    "次のファイルが重複しています"
                }.Concat(keyCount.Select(x => $"'{x.filename}'")));
                throw new InvalidOperationException(errorMessage);
            }

            var nos = JsonSerializer.Deserialize <Dictionary <string, double> >(nosText);

            var notContainsNos = images.Select(x => x.FileName)
                                 .Where(x => !nos.ContainsKey(x))
                                 .ToImmutableArray();

            if (notContainsNos.Any())
            {
                var errorMessage = string.Join(Environment.NewLine, new string[]
                {
                    "次のファイルの No が指定されていません"
                }.Concat(notContainsNos.Select(x => $"'{x}'")));
                throw new InvalidOperationException(errorMessage);
            }

            var now = DateTimeOffset.UtcNow;

            var nextVersion = await metaFileOperator.NewVersionNumber(scoreName);

            var versionFileKey = metaFileOperator.CreateVersionFileKey(scoreName, nextVersion, now);

            var versionMeta = new ScoreVersionMeta()
            {
                Version = int.Parse(nextVersion),
            };

            foreach (var formFile in images)
            {
                var no = nos[formFile.FileName].ToString("F").TrimEnd('0').TrimEnd('.');

                var commentPrefix = metaFileOperator.CreatePageCommentPrefix(scoreName, nextVersion, now, no);
                try
                {
                    var key = await metaFileOperator.SaveImage(scoreName, formFile);

                    versionMeta.Pages[no] = new ScoreVersionPageMeta()
                    {
                        No            = no,
                        ImageFileKey  = key,
                        CommentPrefix = commentPrefix,
                    };
                }
                catch (AmazonS3Exception e)
                {
                    Logger.LogError(e, e.Message);
                    throw;
                }
            }

            await metaFileOperator.SaveVersionMetaAsync(versionFileKey, versionMeta);

            var scoreMeta = await metaFileOperator.GetScoreMetaAsync(scoreName);

            var scoreContentMeta = await scoreMeta.GetLastScoreContent().DeepCopyAsync();

            scoreContentMeta.VersionFileKeys[nextVersion] = versionFileKey;

            var scoreMetaKey = ScoreMeta.CreateKey();

            scoreMeta[scoreMetaKey] = scoreContentMeta;

            await metaFileOperator.SaveScoreMetaAsync(scoreName, scoreMeta, scoreMetaKey);

            return(Ok());
        }
Exemple #2
0
 public async Task <IActionResult> CreateVersion(
     [FromRoute(Name = "score_name")] string scoreName,
     [FromForm] NewScoreVersion newScoreVersion)
 {
     throw new NotImplementedException();
 }