public async Task RemoveFavoriteUserAsync(long ownerId, long userId) { var query = _refsContext.Favorites.Where( f => f.OwnerId == ownerId && f.UserId == userId && f.Kind == FavoriteKind.User ); var storedFav = query.SingleOrDefault(); SystemContract.Require( storedFav != null, "未登録のお気に入りユーザ, ownerId: {0}, userId: {1}", ownerId.ToString(), userId.ToString() ); /// update statistics data //{ // var statisticsQuery = _refsContext.RefListStatistics.Where(s => s.RefListId == userId); // var storedRefListStatistics = await statisticsQuery.SingleAsync(); // var favCountQuery = _refsContext.Favorites.AsNoTracking(); // var favCount = await favCountQuery.CountAsync(s => s.RefListId == userId); // storedRefListStatistics.FavoriteCount = favCount - 1; //} _refsContext.Favorites.Remove(storedFav); await _refsContext.SaveChangesAsync(); }
public async Task AddFavoriteUserAsync(long ownerId, long userId) { var exists = await ExistsFavoriteUserAsync(ownerId, userId); SystemContract.Require( !exists, "登録済みのお気に入りユーザ, ownerId: {0}, userId: {1}", ownerId.ToString(), userId.ToString() ); /// update statistics data //{ // var statisticsQuery = _refsContext.RefListStatistics.Where(s => s.RefListId == userId); // var storedRefListStatistics = await statisticsQuery.SingleAsync(); // var favCountQuery = _refsContext.Favorites.AsNoTracking(); // var favCount = await favCountQuery.CountAsync(s => s.RefListId == userId); // storedRefListStatistics.FavoriteCount = favCount + 1; //} var fav = FavoriteFactory.CreateForUser(ownerId, userId); _refsContext.Favorites.Add(fav); await _refsContext.SaveChangesAsync(); }
public async Task <ActionResult> NarrowDetail( string tag, string titleSearch = "", int pageIndex = 1, RefListSortKind sort = RefListSortKind.PublishedDateDescending ) { SystemContract.RequireNotNull(tag, "tag"); SystemContract.Require(tag != CoreConsts.UnsetTagName, "$unsetが指定されました"); /// 非公開設定の確認のため本人でもPublishしか見れないようにする。 var req = new GetRefListsRequest( null, null, null, tag, titleSearch, null, PublishingStatusConditionKind.PublishOnly, pageIndex - 1, WebConsts.RefListsPageSize, sort ); var result = await _refListHandler.GetRefListsAsync(req); var vm = new PagedRefListsViewModel() { PageIndex = result.PageIndex + 1, PageCount = result.PageCount, RefLists = Mapper.Map <ICollection <RefListViewModel> >(result.RefLists), }; return(JsonNet(vm, JsonRequestBehavior.AllowGet)); }
public async Task <EntityIdentity> MoveRefToAsync( EntityIdentity fromListIdentity, EntityIdentity toListIdentity, int fromRefIndex, int toRefIndex = -1 ) { using (var tran = _refsContext.BeginTransaction()) { try { SystemContract.Require( fromRefIndex > -1, () => new ArgumentOutOfRangeException("fromRefIndex: " + fromRefIndex) ); var fromQuery = _refsContext.RefLists.Include("Refs"). Where(l => l.Id == fromListIdentity.Id); var fromStoredRefList = await fromQuery.SingleAsync(); SystemContract.Require( fromRefIndex < fromStoredRefList.Refs.Count, () => new ArgumentOutOfRangeException("fromRefIndex: " + fromRefIndex) ); BusinessContract.ValidateWritePermission(fromStoredRefList.AuthorId); BusinessContract.ValidateRowVersion(fromStoredRefList.RowVersion, fromListIdentity.RowVersion); var toQuery = _refsContext.RefLists.Include("Refs"). Where(l => l.Id == toListIdentity.Id); var toStoredRefList = await toQuery.SingleAsync(); BusinessContract.ValidateWritePermission(toStoredRefList.AuthorId); BusinessContract.ValidateRowVersion(toStoredRefList.RowVersion, toListIdentity.RowVersion); /// fromから削除 var fromRefs = fromStoredRefList.Refs.OrderBy(r => r.DisplayOrder).ToList(); var moving = fromRefs[fromRefIndex]; fromRefs.RemoveAt(fromRefIndex); for (int i = 0; i < fromRefs.Count; ++i) { fromRefs[i].DisplayOrder = i; } fromStoredRefList.Refs.Remove(moving); /// toに追加 var toRefs = toStoredRefList.Refs.OrderBy(r => r.DisplayOrder).ToList(); if (toRefIndex < 0) { toRefs.Add(moving); } else { toRefs.Insert(toRefIndex, moving); } for (int i = 0; i < toRefs.Count; ++i) { toRefs[i].DisplayOrder = i; } toStoredRefList.Refs.Add(moving); _refsContext.MarkAsModified(fromStoredRefList); await _refsContext.SaveChangesAsync(); // toのUpdatedDateをfromより後にするため早めに書き込む /// update statistics if (moving.Kind == RefKind.Link) { var fromStatQuery = _refsContext.RefListStatistics.Where(s => s.RefListId == fromListIdentity.Id); var storedFromStat = await fromStatQuery.SingleAsync(); storedFromStat.LinkCount = fromStoredRefList.Refs.Count(r => r.Kind == RefKind.Link); var toStatQuery = _refsContext.RefListStatistics.Where(s => s.RefListId == toListIdentity.Id); var storedToStat = await toStatQuery.SingleAsync(); storedToStat.LinkCount = toStoredRefList.Refs.Count(r => r.Kind == RefKind.Link); } _refsContext.MarkAsModified(toStoredRefList); await _refsContext.SaveChangesAsync(); // toのUpdatedDateをfromより後にするため遅めに書き込む SendRefListUpdated(fromListIdentity.Id); SendRefListUpdated(toListIdentity.Id); fromListIdentity.RowVersion = fromStoredRefList.RowVersion; tran.Commit(); return(fromListIdentity); } catch (Exception) { tran.Rollback(); throw; } } }