likit

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 6 Imported by: 0

README

likit-go

likit-go is a Client library for the Likit in Golang.

Usage

go get github.com/likit/likit-go

A Comment Like Example

import (
	"github.com/CorrectRoadH/likit-go"
)

type CommentServer struct {
	Store      *store.CommentStore
	likitServer *likit.LikitServer
}

func NewCommentServer(store *store.CommentStore) *CommentServer {
	return &CommentServer{
		Store:      store,
        // the likit server address. You should replace it with your own server address
		// likit.NewlikitServer("localhost:4778",false), 
		likitServer: likit.NewlikitServer("likit.zeabur.app:443",true), 
		// NewlikitServer(host string, tls bool)
		// if you deploy likit server in zeabur. tls shoud be true
	}
}

const businessId = "COMMENT_LIKE"
// get comment
func (s *CommentServer) ListComment(c echo.Context)  error {
    userId := /// get user id from jwt or cookie

    // define find condition
    find := ////
	comments, err := s.Store.ListComment(ctx, find)

	return c.JSON(http.StatusOK, lo.Map(comments, func(comment types.Comment, index int) Comment {
        isLike, err := s.likitServer.IsVote(ctx, businessId, comment.Id, comment.UserId)
        if err != nil {
            // handle error
        }

        likeNum, err := s.likitServer.Count(ctx, businessId, comment.Id)
        if err != nil {
            // handle err
        }

        return convertComment(comment, isLike, likeNum)
    }))
}

func (s *CommentServer) Like(c echo.Context) (*apiv1pb.LikeResponse, error) {
	userId := /// get user id from jwt or cookie
	commentId := /// get comment id from request

	count, err := s.likitServer.Vote(ctx, businessId, commentId, UserId)
	if err != nil {
		// handle error
	}
	return c.JSON(http.StatusOK, Count: count)
}

func (s *CommentServer) Unlike(c echo.Context) (*apiv1pb.LikeResponse, error) {
    userId := /// get user id from jwt or cookie
	commentId := /// get comment id from request

	count, err := s.likitServer.UnVote(ctx, businessId, commentId, UserId)
	if err != nil {
		// handle error
	}

	return c.JSON(http.StatusOK, Count: count)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBusinessNotExist = errors.New("business is not exist")

	ErrDatabaseTypeNotSupport = errors.New("database type not support")

	ErrUserHasVoted = errors.New("user has voted")

	ErrUserNotVoted = errors.New("user has not voted")
)

Functions

This section is empty.

Types

type ErrorResponse

type ErrorResponse struct {
	Status  int    `json:"status"`
	Message string `json:"msg"`
}

type IsVoteResponse added in v0.0.2

type IsVoteResponse struct {
	Status int  `json:"status"`
	IsVote bool `json:"isVote"`
}

type LikitServer added in v0.1.0

type LikitServer struct {
	// contains filtered or unexported fields
}

func NewLikitServer added in v0.1.0

func NewLikitServer(host string, tls bool) *LikitServer

func (*LikitServer) Count added in v0.1.0

func (s *LikitServer) Count(ctx context.Context, businessId string, messageId string) (int64, error)

func (*LikitServer) IsVote added in v0.1.0

func (s *LikitServer) IsVote(ctx context.Context, businessId string, messageId string, userId string) (bool, error)

func (*LikitServer) UnVote added in v0.1.0

func (s *LikitServer) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error)

func (*LikitServer) Vote added in v0.1.0

func (s *LikitServer) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error)

func (*LikitServer) VotedUsers added in v0.1.0

func (s *LikitServer) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error)

type VoteResponse

type VoteResponse struct {
	Status int   `json:"status"`
	Count  int64 `json:"count"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL