Ejemplo n.º 1
0
        public async Task <ActionResult> DetailsAsync(int id = -1)
        {
            //从数据库中读取数据
            if (!_DBServices.HasAnimeID(id))
            {
                return(NotFound());
            }
            var key = new KEY {
                Key = CacheKey.Anime_One(id).ToCharArray()
            };

            if (!_DBServices.GetDate(key, out Anime Anime))
            {
                Anime = await _DBServices.GetDateOneAsync <Anime>(db =>
                                                                  db.Where(a => a.AnimeID == id)
                                                                  .Include(a => a.Souce)
                                                                  .Include(a => a.Tags)
                                                                  .Include(a => a.AnimeComms));

                _DBServices.SetCache(key, Anime);
            }
            key = new KEY {
                Key = CacheKey.Blog_One_ByAnimeID(id).ToCharArray()
            };
            if (!_DBServices.GetDate(key, out ICollection <Blog> blogs))
            {
                blogs = await _DBServices.GetDateToListAsync <Blog>(db => db.Where(b => b.AnimeID == id).OrderByDescending(a => a.Time).Take(10));

                _DBServices.SetCache(key, blogs);
            }

            //初始化数据
            var userAnimeNumber          = 0;                               //动画观看集数
            var IsSignIn                 = false;                           //用户是否登录
            var IsSub                    = false;                           //用户是否订阅某动画
            var IsShowEdit               = false;                           //用户是否可以编辑
            ICollection <AnimeMemo> memo = new List <AnimeMemo>();          //用户写下的MEMO
            //未登录也可以显示:
            ICollection <Blog>       blog        = blogs.GetListDate();     //用户对该动画的长评短评
            ICollection <AnimeTag>   animeTags   = new List <AnimeTag>();   //动画的标签
            ICollection <AnimeSouce> animeSouces = new List <AnimeSouce>(); //动画的播放源
            ICollection <AnimeComm>  animeComms  = new List <AnimeComm>();  //动画的评论

            //var SubNum = 0;                                             //用户订阅量(暂时用不到呢)

            animeTags   = Anime.Tags;
            animeSouces = Anime.Souce;
            animeComms  = Anime.AnimeComms;
            if (Anime.AnimeNumUpdata())//计算动画集数
            {
                //需要更新动画信息
                _DBServices.SetCache(key, Anime);//全部的数据读取好之后,缓存一下
                await _DBServices.UpdateAsync(Anime);
            }
            //动画集数列表的相关计算
            AnimeNumberInfo animeNumberInfo = Anime.AnimeNumPage();

            if (!(IsSignIn = _signInManager.IsSignedIn(HttpContext.User))) //如果没登陆,后面的就不需要处理了
            {
                return(View(
                           viewName: "Bangumi_OneAnime",
                           model: new Bangumi_OneAnime
                {
                    Anime = Anime,
                    UserAnimeNumber = userAnimeNumber,
                    Memos = memo,
                    IsSub = IsSub,
                    IsSignIn = IsSignIn,
                    IsShowEdit = IsShowEdit,
                    Page = animeNumberInfo
                }
                           ));
            }
            //如果没有登陆,返回Null
            var userID = _userManager.GetUserId(HttpContext.User);

            //尝试读取缓存
            key = new KEY {
                Key = CacheKey.Anime_User_Info(userID, id).ToCharArray()
            };
            if (!_DBServices.GetDate(key, out AnimeUserInfo Infos))
            {
                Infos = await _DBServices.GetDateOneAsync <AnimeUserInfo>(db => db.Where(info => info.Users.Id == userID && info.SubAnime.AnimeID == id)
                                                                          .Include(info => info.Memos));

                _DBServices.SetCache(key, Infos);
            }
            if (Infos != null)//没有订阅
            {
                IsSub           = true;
                memo            = Infos.Memos;
                userAnimeNumber = Infos.NowAnimeNum;
            }
            //检查权限
            var EditAnime = await _authorizationService.AuthorizeAsync(HttpContext.User, Final.Yuri_Yuri4);

            IsShowEdit = EditAnime.Succeeded;

            return(View(
                       viewName: "Bangumi_OneAnime",
                       model: new Bangumi_OneAnime
            {
                Anime = Anime,
                UserAnimeNumber = userAnimeNumber,
                Memos = memo,
                IsSub = IsSub,
                IsSignIn = IsSignIn,
                IsShowEdit = IsShowEdit,
                Page = animeNumberInfo
            }
                       ));
        }