init post-service

This commit is contained in:
2023-09-27 16:17:47 +01:00
parent e0bd8ef953
commit 0341a938fd
31 changed files with 5666 additions and 0 deletions

View 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,
}
}