package post

import (
	"fmt"
	"mini_server/internal/dao"
	"mini_server/internal/server/help"
	"mini_server/internal/server/httpreply"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func Like(c *gin.Context) {
	reply := httpreply.NewDefaultReplyData()
	defer httpreply.Reply(c, reply)

	userId, ok := help.GetUserId(c)
	if !ok {
		reply.Status = http.StatusNotFound
		reply.Message = "无法获取用户信息"
		return
	}

	var param QueryURI
	if err := c.ShouldBindUri(&param); err != nil {
		reply.Status = http.StatusBadRequest
		reply.Message = fmt.Sprintf("invalid uri path,err:%v", err)
		return
	}

	data := dao.PostLike{UserId: userId, PostId: param.PostId}
	if err := dao.PostLike_Like(&data); err != nil {
		logrus.Errorf("贴子点赞插入数据库异常，userId:%d,postId:%d,err:%v", userId, param.PostId, err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "贴子点赞异常"
		return
	}
}
