Exemplo n.º 1
0
        public async Task <IActionResult> PostWatchhistory([FromBody] Watchhistory watchhistory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Watchhistory.Add(watchhistory);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (WatchhistoryExists(watchhistory.Usid))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetWatchhistory", new { id = watchhistory.Usid }, watchhistory));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutWatchhistory([FromRoute] int id, [FromBody] Watchhistory watchhistory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != watchhistory.Usid)
            {
                return(BadRequest());
            }

            _context.Entry(watchhistory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WatchhistoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetVideoInfo(int vid)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { status = "error", data = ModelState.ToString() }));
            }

            var video = await _context.Video.FindAsync(vid);

            if (video == null)
            {
                return(NotFound(new { status = "not found" }));
            }

            var tags = _context.Videotag
                       .Where(x => x.Vid == video.Vid)
                       .Join(_context.Tag, x => x.TagId, y => y.TagId, (x, y) => new
            {
                name   = y.Name,
                tag_id = x.TagId,
                cat_id = y.CatId
            });

            //上传视频的user
            var user = await _context.Users.FindAsync(video.Usid);

            string baseUrl = Request.Scheme + "://" + Request.Host + "/";

            bool isLogin   = false;
            int  myid      = -1;
            int  ifavorite = 0;
            int  ilike     = 0;
            int  ifollow   = 0;

            var auth = await HttpContext.AuthenticateAsync();

            if (auth.Succeeded)
            {
                var claim = User.FindFirstValue("User");
                if (int.TryParse(claim, out myid))
                {
                    isLogin = true;
                }
                if (!Int32.TryParse(claim, out var loginUsid))
                {
                    return(BadRequest(new { status = "validation failed" }));
                }
                var user_now = await _context.Users.FindAsync(loginUsid);

                //添加观看历史
                var wh0 = await _context.Watchhistory.FirstOrDefaultAsync(x => x.Usid == loginUsid && x.Vid == video.Vid);

                if (wh0 != null)
                {
                    _context.Watchhistory.Remove(wh0);
                }
                var w_h = new Watchhistory();
                w_h.CreateTime = DateTime.Now;
                w_h.Usid       = user_now.Usid;
                w_h.Vid        = video.Vid;
                _context.Watchhistory.Add(w_h);
            }

            if (isLogin)
            {
                ilike     = (await _context.Likevideo.FindAsync(myid, vid)) == null ? 0 : 1;
                ifavorite = (await _context.Favorite.FindAsync(myid, vid)) == null ? 0 : 1;
                ifollow   = (await _context.Follow.FindAsync(myid, user.Usid)) == null ? 0 : 1;
            }

            var result = new
            {
                vid          = video.Vid,
                title        = video.Title,
                desc         = video.Description,
                cover        = baseUrl + "images/" + video.Cover,
                view_num     = video.WatchNum,
                comment_num  = video.CommentNum,
                upload_time  = video.CreateTime.ToTimestamp(),
                url          = baseUrl + "videos/" + video.Path,
                like_num     = video.LikeNum,
                favorite_num = video.FavoriteNum,
                share_num    = video.FavoriteNum,
                tags         = tags,
                up           = new
                {
                    usid       = user.Usid,
                    name       = user.Nickname,
                    desc       = user.Signature,
                    follow_num = user.FollowerNum,
                    avatar     = baseUrl + "images/" + user.Avatar,
                    ifollow
                },
                ilike,
                ifavorite
            };

            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(Ok(new { status = "Create History failed.", data = result }));
            }

            //修改播放量
            try
            {
                video.WatchNum++;
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(Ok(new { status = "Modification Failed.", data = result }));
            }

            return(Ok(new { status = "ok", data = result }));
        }