Пример #1
0
        public async Task <ActionResult> AddTagUse(string name)
        {
            SystemContract.RequireNotNullOrWhiteSpace(name, "name");

            var user   = GetUser();
            var result = await _tagHandler.EnsureTagUseAsync(user.Id, name);

            var tagUses = await _tagHandler.GetAllTagUsesAsync(user.Id);

            var ret = Mapper.Map <ICollection <TagUseViewModel> >(tagUses);

            return(JsonNet(ret));
        }
Пример #2
0
        private async Task <EntityIdentity> SetTagUsesAsync(RefList refList, IEnumerable <string> tagNames, bool isPublishingStatusChanged)
        {
            tagNames = tagNames ?? new string[0];
            if (tagNames.Any(n => string.IsNullOrWhiteSpace(n)))
            {
                throw new ArgumentException();
            }

            refList.TagUses = refList.TagUses ?? new List <TagUse>();
            var storedTagNames = refList.TagUses.Select(u => u.Name).ToArray();

            /// どちらにも含まれているタグに対してやることはない
            ///var asIsTagUses = storedTagNames.Intersect(tagNames);

            /// 削除分
            var removedTagUseNames = storedTagNames.Except(tagNames);
            var removedTagUses     = removedTagUseNames.Select(n => refList.TagUses.Single(u => u.Name == n)).ToArray();

            foreach (var removed in removedTagUses)
            {
                refList.TagUses.Remove(removed);
            }

            /// 追加分
            var addedTagUseNames = tagNames.Except(storedTagNames);
            var addedTagUses     = new List <TagUse>();

            foreach (var name in addedTagUseNames)
            {
                var tagUse = await _tagHandler.EnsureTagUseAsync(refList.AuthorId, name);

                addedTagUses.Add(tagUse);
            }
            /// 下の書き方ではEnsureTagUseAsync()が同時に複数回呼ばれるため?例外が発生する。
            /// ちゃんとawaitで待って同時には一回しか呼ばれないようにする。
            ///var addedTagUses = (await addedTagUseNames.Select(
            ///    async n => await _tagHandler.EnsureTagUseAsync(refList.AuthorId, n)
            ///).WhenAll()).ToArray();

            foreach (var added in addedTagUses)
            {
                refList.TagUses.Add(added);
            }

            /// リストの更新
            if (removedTagUseNames.Any() || addedTagUseNames.Any())
            {
                _refsContext.MarkAsModified(refList);
                SendRefListUpdated(refList.Id);
            }
            await _refsContext.SaveChangesAsync();

            /// update statistics
            {
                if (isPublishingStatusChanged)
                {
                    var asIsTagUseNames = storedTagNames.Intersect(tagNames);
                    var asIsTagUses     = asIsTagUseNames.Select(n => refList.TagUses.Single(u => u.Name == n)).ToArray();
                    foreach (var asIs in asIsTagUses)
                    {
                        asIs.Statistics.RefListCount          = asIs.RefLists.Count();
                        asIs.Statistics.PublishedRefListCount =
                            asIs.RefLists.Where(l => l.PublishingStatus == PublishingStatusKind.Publish).Count();
                    }
                }

                foreach (var removed in removedTagUses)
                {
                    removed.Statistics.RefListCount          = removed.RefLists.Count();
                    removed.Statistics.PublishedRefListCount =
                        removed.RefLists.Where(l => l.PublishingStatus == PublishingStatusKind.Publish).Count();
                }
                foreach (var added in addedTagUses)
                {
                    added.Statistics.RefListCount          = added.RefLists.Count();
                    added.Statistics.PublishedRefListCount =
                        added.RefLists.Where(l => l.PublishingStatus == PublishingStatusKind.Publish).Count();
                }
            }
            await _refsContext.SaveChangesAsync();

            return(new EntityIdentity()
            {
                Id = refList.Id,
                RowVersion = refList.RowVersion,
            });
        }