✨ Friends api
This commit is contained in:
46
pkg/grpc/friendships.go
Normal file
46
pkg/grpc/friendships.go
Normal file
@ -0,0 +1,46 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/grpc/proto"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/services"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func (v *Server) ListFriendship(_ context.Context, request *proto.FriendshipLookupRequest) (*proto.ListFriendshipResponse, error) {
|
||||
account, err := services.GetAccount(uint(request.GetAccountId()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
friends, err := services.ListFriend(account, models.FriendshipStatus(request.GetStatus()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &proto.ListFriendshipResponse{
|
||||
Data: lo.Map(friends, func(item models.AccountFriendship, index int) *proto.FriendshipResponse {
|
||||
return &proto.FriendshipResponse{
|
||||
AccountId: uint64(item.AccountID),
|
||||
RelatedId: uint64(item.RelatedID),
|
||||
Status: uint32(item.Status),
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) GetFriendship(ctx context.Context, request *proto.FriendshipTwoSideLookupRequest) (*proto.FriendshipResponse, error) {
|
||||
friend, err := services.GetFriendWithTwoSides(uint(request.GetAccountId()), uint(request.GetRelatedId()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if friend.Status != models.FriendshipStatus(request.GetStatus()) {
|
||||
return nil, fmt.Errorf("status mismatch")
|
||||
}
|
||||
|
||||
return &proto.FriendshipResponse{
|
||||
AccountId: uint64(friend.AccountID),
|
||||
RelatedId: uint64(friend.RelatedID),
|
||||
Status: uint32(friend.Status),
|
||||
}, nil
|
||||
}
|
31
pkg/grpc/proto/friendships.proto
Normal file
31
pkg/grpc/proto/friendships.proto
Normal file
@ -0,0 +1,31 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option go_package = ".;proto";
|
||||
|
||||
package proto;
|
||||
|
||||
service Friendships {
|
||||
rpc ListFriendship(FriendshipLookupRequest) returns (ListFriendshipResponse) {}
|
||||
rpc GetFriendship(FriendshipTwoSideLookupRequest) returns (FriendshipResponse) {}
|
||||
}
|
||||
|
||||
message FriendshipLookupRequest {
|
||||
uint64 account_id = 1;
|
||||
uint32 status = 2;
|
||||
}
|
||||
|
||||
message FriendshipTwoSideLookupRequest {
|
||||
uint64 account_id = 1;
|
||||
uint64 related_id = 2;
|
||||
uint32 status = 3;
|
||||
}
|
||||
|
||||
message ListFriendshipResponse {
|
||||
repeated FriendshipResponse data = 1;
|
||||
}
|
||||
|
||||
message FriendshipResponse {
|
||||
uint64 account_id = 1;
|
||||
uint64 related_id = 2;
|
||||
uint32 status = 3;
|
||||
}
|
@ -12,6 +12,7 @@ import (
|
||||
type Server struct {
|
||||
proto.UnimplementedAuthServer
|
||||
proto.UnimplementedNotifyServer
|
||||
proto.UnimplementedFriendshipsServer
|
||||
}
|
||||
|
||||
func StartGrpc() error {
|
||||
@ -24,6 +25,7 @@ func StartGrpc() error {
|
||||
|
||||
proto.RegisterAuthServer(server, &Server{})
|
||||
proto.RegisterNotifyServer(server, &Server{})
|
||||
proto.RegisterFriendshipsServer(server, &Server{})
|
||||
|
||||
reflection.Register(server)
|
||||
|
||||
|
Reference in New Issue
Block a user