12月14号

1.点赞视频前端代码

//唯一标识
private const string keyPrefix = "VideoLike";

/// <summary>
/// 点赞计数
/// </summary>
/// <param name="videoId">视频Id</param>
/// <returns></returns>
public long Count(string videoId)
{
string key = $"{keyPrefix}_{videoId}";
return RedisHelper.SetLength(key);
}

/// <summary>
/// 是否已点赞
/// </summary>
/// <param name="videoId">视频Id</param>
/// <param name="userId">用户Id</param>
/// <returns></returns>
public bool IsLike(string videoId, int userId)
{
string key = $"{keyPrefix}_{videoId}";
return RedisHelper.SetContains(key, userId);
}

/// <summary>
/// 添加点赞
/// </summary>
/// <param name="videoId">视频Id</param>
/// <param name="userId">用户Id</param>
/// <returns></returns>
public bool AddLike(string videoId, int userId)
{
string key = $"{keyPrefix}_{videoId}";
return RedisHelper.SetAdd(key, userId);
}

/// <summary>
/// 移除点赞
/// </summary>
/// <param name="videoId">视频Id</param>
/// <param name="userId">用户Id</param>
/// <returns></returns>
public bool RemoveLike(string videoId, int userId)
{
string key = $"{keyPrefix}_{videoId}";
return RedisHelper.SetRemove(key, userId);
}

2.点赞视频后端代码接受用户名id信息。视频id信息

public void ProcessRequest(HttpContext context)
{

var reg = context.Request;
var videoId= reg.QueryString["videoId"];
var userId =Convert.ToInt32(reg.QueryString["userId"]);
var action = reg.QueryString["action"];
ILikeManager like = new LikeManager();
if (action == "add")
{
like.AddLike(videoId, userId);
}
else if (action== "remove")
{
like.RemoveLike(videoId,userId);
}
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.End();
}

posted @ 2021-12-14 23:06  Luckyyi  阅读(50)  评论(0编辑  收藏  举报