Like is a very common feature, but the implementation of the like function for QBS 1 and QBS 100000 is vastly different in difficulty. However, even the simplest like system might take about half an hour to develop from scratch. But there's an open-source and free Backend as a Service that can help you implement a like feature in your application in just 10 minutes.
Likit encapsulates different like implementations for different performance requirements, making it easy for developers to invoke through different methods like SDK, gRPC, and RESTful.
Let us help you implement a like feature in your application in less than 10 minutes through a journal.
You can deploy likit on Zeabur by this template.
Likit needs to expose two ports to the outside. In Zeabur, this can be achieved by binding a domain.The first port handles HTTP requests. Through it, you can access the dashboard to manage Likit and also use RESTful to call APIs.The second port handles gRPC requests. In the SDKs for Golang and Java, please enter the domain name bound to this port.
We click on the previously bound HTTP domain to enter Likit's dashboard.
You should copy the password for the Likit Dashboard from the left side of the Zeabur control panel. Afterward, click on the bound domain to access Likit, paste the password, and then enter the Dashboard.
You should copy the password of the Likit Dashboard from the left side of the Zeabur control panel, then click on the previously bound domain to enter Likit, paste the password, and then enter the Dashboard.
Here, we briefly explain three concepts that will often be encountered later: BusinessId, MessageId, and UserId.
Up to now, we have already created the Business, so everything related to Likit has been configured. Now, we can start calling the API in our backend code.
If your backend is written in Java with Spring, then all you need to use is this library.
If your backend is written in Golang, then github.com/correctroadh/likit-go is all you need.
import (
"github.com/CorrectRoadH/likit-go"
)
type VideoServer struct {
Store *store.VideoStore
likeServer *likit.LikitServer
}
func NewVideoServer(store *store.VideoStore) *VideoServer {
return &VideoServer{
Store: store,
// the likit server address. You should replace it with your own server address
// NewLikitServer(host string, tls bool)
likeServer: likit.NewLikitServer("likit-grpc.zeabur.app:443",true),
// if you deploy likit server in zeabur. tls should be true
}
}
type Video struct {
Id string `json:"id"`
LikeNum int `json:"likeNum"`
IsLike bool `json:"isLike"`
}
const businessId = "VIDEO_LIKE"
func (s *VideoServer) ListVideo(c echo.Context) error {
userId := /// get user id from jwt or cookie
find := // get find condition from request
videos, err := s.Store.ListVideo(ctx, find)
return c.JSON(http.StatusOK, lo.Map(videos, func(video domain.Video, index int) Video {
isLike, err := s.likeServer.IsVote(ctx, businessId, video.Id, video.UserId)
if err != nil {
// handle error
}
likeNum, err := s.likeServer.Count(ctx, businessId, video.Id)
if err != nil {
// handle err
}
// just for example
return Video{
Id: video.Id,
LikeNum: likeNum,
IsLike: isLike,
}
}))
}
func (s *VideoServer) Like(c echo.Context) error {
userId := // get user id from jwt or cookie
videoId := // get video id from request
count, err := s.likeServer.Vote(ctx, businessId, videoId, UserId)
if err != nil {
// handle error
}
return c.JSON(http.StatusOK, count)
}
func (s *VideoServer) Unlike(c echo.Context) error {
userId := // get user id from jwt or cookie
videoId := // get video id from request
count, err := s.likeServer.UnVote(ctx, businessId, videoId, UserId)
if err != nil {
// handle error
}
return c.JSON(http.StatusOK, count)
}
If your backend is implemented neither in Golang nor Java, then I recommend using gRPC to call the Likit API. You can see the complete API definition of Likit and some gRPC SDKs for other languages here.
Likit is a very simple and easy-to-use Backend as a Service. If you want to implement a like, dislike, or voting feature in your new project, it's worth a try.