Implement gRPC on client and server
Pending Testing
This commit is contained in:
@@ -1,23 +1,53 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "Aoba.RPC";
|
||||
package aoba;
|
||||
|
||||
service AobaRPC {
|
||||
rpc GetMedia (Id) returns (MediaModel);
|
||||
service AobaRpc {
|
||||
rpc GetMedia (Id) returns (MediaResponse);
|
||||
rpc ListMedia(PageFilter) returns (ListResponse);
|
||||
}
|
||||
|
||||
message PageFilter{
|
||||
optional int32 page = 1;
|
||||
optional int32 pageSize = 2;
|
||||
optional string query = 3;
|
||||
}
|
||||
|
||||
message Id{
|
||||
string idString = 1;
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
message MediaResponse {
|
||||
oneof result {
|
||||
MediaModel value = 1;
|
||||
Empty empty = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message ListResponse{
|
||||
repeated MediaModel items = 1;
|
||||
Pagination pagination = 2;
|
||||
}
|
||||
|
||||
message Pagination {
|
||||
int32 page = 1;
|
||||
int32 pageSize = 2;
|
||||
int64 totalPages = 3;
|
||||
int64 totalItems = 4;
|
||||
optional string query = 5;
|
||||
}
|
||||
|
||||
message Empty{}
|
||||
|
||||
message MediaModel {
|
||||
int32 version = 1;
|
||||
Id id = 2;
|
||||
string mediaId = 3;
|
||||
string fileName = 4;
|
||||
MediaType mediaType = 5;
|
||||
string ext = 6;
|
||||
int32 viewCount = 7;
|
||||
Id owner = 8;
|
||||
Id id = 1;
|
||||
Id mediaId = 2;
|
||||
string fileName = 3;
|
||||
MediaType mediaType = 4;
|
||||
string ext = 5;
|
||||
int32 viewCount = 6;
|
||||
Id owner = 7;
|
||||
}
|
||||
|
||||
enum MediaType{
|
||||
@@ -27,4 +57,4 @@ enum MediaType{
|
||||
Text = 3;
|
||||
Code = 4;
|
||||
Raw = 5;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,25 @@
|
||||
using AobaCore;
|
||||
|
||||
using Aoba.RPC;
|
||||
|
||||
using AobaServer.Utils;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
namespace AobaServer.Services;
|
||||
|
||||
public class AobaRpcService(AobaService aobaService) : AobaRPC.AobaRPCBase
|
||||
public class AobaRpcService(AobaService aobaService) : AobaRpc.AobaRpcBase
|
||||
{
|
||||
public override Task<MediaModel> GetMedia(Id request, ServerCallContext context)
|
||||
public override async Task<MediaResponse> GetMedia(Id request, ServerCallContext context)
|
||||
{
|
||||
return base.GetMedia(request, context);
|
||||
var media = await aobaService.GetMediaAsync(request.ToObjectId());
|
||||
return media.ToResponse();
|
||||
}
|
||||
|
||||
public override async Task<ListResponse> ListMedia(PageFilter request, ServerCallContext context)
|
||||
{
|
||||
var result = await aobaService.FindMediaAsync(request.Query, request.HasPage ? request.Page : 1, request.HasPageSize ? request.PageSize : 50);
|
||||
return result.ToResponse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
15
AobaServer/Utils/Extensions.cs
Normal file
15
AobaServer/Utils/Extensions.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static ObjectId ToObjectId(this string? value)
|
||||
{
|
||||
if(value == null)
|
||||
return ObjectId.Empty;
|
||||
if(ObjectId.TryParse(value, out ObjectId result))
|
||||
return result;
|
||||
return ObjectId.Empty;
|
||||
}
|
||||
}
|
||||
65
AobaServer/Utils/ProtoExtensions.cs
Normal file
65
AobaServer/Utils/ProtoExtensions.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using AobaCore.Models;
|
||||
using Aoba.RPC;
|
||||
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class ProtoExtensions
|
||||
{
|
||||
public static ListResponse ToResponse(this PagedResult<Media> result)
|
||||
{
|
||||
var res = new ListResponse()
|
||||
{
|
||||
Pagination = result.ToPagination(),
|
||||
};
|
||||
res.Items.AddRange(result.Items.Select(i => i.ToMediaModel()));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Pagination ToPagination<T>(this PagedResult<T> result)
|
||||
{
|
||||
return new Pagination()
|
||||
{
|
||||
Page = result.Page,
|
||||
PageSize = result.PageSize,
|
||||
TotalItems = result.TotalItems,
|
||||
TotalPages = result.TotalPages,
|
||||
Query = result.Query,
|
||||
};
|
||||
}
|
||||
|
||||
public static MediaResponse ToResponse(this Media? media)
|
||||
{
|
||||
if(media == null)
|
||||
return new MediaResponse() { Empty = new Empty() };
|
||||
return new MediaResponse()
|
||||
{
|
||||
Value = media.ToMediaModel()
|
||||
};
|
||||
}
|
||||
|
||||
public static MediaModel ToMediaModel(this Media media)
|
||||
{
|
||||
return new MediaModel()
|
||||
{
|
||||
Ext = media.Ext,
|
||||
FileName = media.Filename,
|
||||
Id = media.Id.ToId(),
|
||||
MediaId = media.MediaId.ToId(),
|
||||
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
||||
Owner = media.Owner.ToId(),
|
||||
ViewCount = media.ViewCount,
|
||||
};
|
||||
}
|
||||
|
||||
public static Id ToId(this ObjectId id)
|
||||
{
|
||||
return new Id() { Value = id.ToString() };
|
||||
}
|
||||
|
||||
public static ObjectId ToObjectId(this Id id)
|
||||
{
|
||||
return id.Value.ToObjectId();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user