mirror of
https://github.com/hexolan/panels.git
synced 2026-03-26 12:40:21 +00:00
init post-service
This commit is contained in:
55
services/post-service/internal/rpc/postv1/conversion.go
Normal file
55
services/post-service/internal/rpc/postv1/conversion.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Handles conversion between Protobuf types and service types
|
||||
package postv1
|
||||
|
||||
import (
|
||||
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/hexolan/panels/post-service/internal"
|
||||
)
|
||||
|
||||
// Post -> Protobuf 'Post'
|
||||
func PostToProto(post *internal.Post) *Post {
|
||||
proto := Post{
|
||||
Id: post.Id.GetReprId(),
|
||||
|
||||
PanelId: post.PanelId,
|
||||
AuthorId: post.AuthorId,
|
||||
|
||||
Title: post.Title,
|
||||
Content: post.Content,
|
||||
|
||||
CreatedAt: timestamppb.New(post.CreatedAt.Time),
|
||||
}
|
||||
|
||||
// convert nullable attributes to PB form (if present)
|
||||
if post.UpdatedAt.Valid == true {
|
||||
proto.UpdatedAt = timestamppb.New(post.UpdatedAt.Time)
|
||||
}
|
||||
|
||||
return &proto
|
||||
}
|
||||
|
||||
// []Post -> []Protobuf 'Post'
|
||||
func PostsToProto(posts []*internal.Post) []*Post {
|
||||
protoPosts := []*Post{}
|
||||
for _, post := range posts {
|
||||
protoPosts = append(protoPosts, PostToProto(post))
|
||||
}
|
||||
return protoPosts
|
||||
}
|
||||
|
||||
// Protobuf 'PostMutable' -> PostCreate
|
||||
func PostCreateFromProto(proto *PostMutable) internal.PostCreate {
|
||||
return internal.PostCreate{
|
||||
Title: proto.GetTitle(),
|
||||
Content: proto.GetContent(),
|
||||
}
|
||||
}
|
||||
|
||||
// Protobuf 'PostMutable' -> PostUpdate
|
||||
func PostUpdateFromProto(proto *PostMutable) internal.PostUpdate {
|
||||
return internal.PostUpdate{
|
||||
Title: proto.Title,
|
||||
Content: proto.Content,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user