Skip to content

Commit

Permalink
T #98 shared proto types
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Oct 11, 2022
1 parent 57f632d commit 4cc740c
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 60 deletions.
12 changes: 7 additions & 5 deletions src/Huppy/Huppy.Core/Services/CommandLog/CommandLogService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Google.Protobuf.WellKnownTypes;
using System.Security.Authentication.ExtendedProtection;
using Google.Protobuf.WellKnownTypes;
using Huppy.Core.Interfaces.IServices;
using HuppyService.Service;
using HuppyService.Service.Protos.Models;

namespace Huppy.Core.Services.CommandLog
Expand All @@ -22,23 +24,23 @@ public async Task<CommandLogModel> AddCommand(CommandLogModel commandLog)
public async Task<IDictionary<ulong, int>> GetAiUsage()
{
// implement single instance of empty?
var result = await _commandLogClient.GetAiUsageAsync(new Empty());
var result = await _commandLogClient.GetAiUsageAsync(new HuppyService.Service.Protos.Void());

return result.AiUsers;
}

public async Task<double> GetAverageExecutionTime()
{
var result = await _commandLogClient.GetAverageExecutionTimeAsync(new Empty());
var result = await _commandLogClient.GetAverageExecutionTimeAsync(new HuppyService.Service.Protos.Void());

return result.AverageTime;
}

public async Task<int> GetCount()
{
var result = await _commandLogClient.GetCountAsync(new Empty());
var result = await _commandLogClient.GetCountAsync(new HuppyService.Service.Protos.Void());

return result.Count;
return result.Number;
}

public async Task<bool> RemoveCommand(CommandLogModel commandLog)
Expand Down
6 changes: 3 additions & 3 deletions src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public async Task<IList<ReminderModel>> GetSortedUserReminders(ulong userId, int

public async Task<int> GetRemindersCount(ulong userId)
{
var result = await _reminderClient.GetRemindersCountAsync(new GetReminderCountInput() { UserId = userId });
return result.Count;
var result = await _reminderClient.GetRemindersCountAsync(new HuppyService.Service.Protos.UserId() { Id = userId });
return result.Number;
}

public async Task<ReminderModel> GetReminderAsync(ulong userId, int reminderId)
Expand Down Expand Up @@ -85,7 +85,7 @@ await _eventService.AddEvent(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.U

public async Task<IList<ReminderModel>> GetUserRemindersAsync(ulong userId)
{
return (await _reminderClient.GetUserRemindersAsync(new() { UserId = userId })).ReminderModels;
return (await _reminderClient.GetUserRemindersAsync(new HuppyService.Service.Protos.UserId() { Id = userId })).ReminderModels;
}

public async Task AddReminderAsync(DateTime date, ulong userId, string message)
Expand Down
6 changes: 3 additions & 3 deletions src/Huppy/Huppy.Core/Services/Server/ServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public ServerService(ServerProto.ServerProtoClient serverClient)

public async Task<ServerModelCollection> GetAllAsync(Empty empty)
{
var result = await _serverClient.GetAllAsync(new Empty());
var result = await _serverClient.GetAllAsync(new HuppyService.Service.Protos.Void());
return result;
}

public async Task<ServerModel> GetAsync(ulong serverId)
{
var result = await _serverClient.GetAsync(new ServerIdInput() { Id = serverId });
var result = await _serverClient.GetAsync(new HuppyService.Service.Protos.ServerId() { Id = serverId });
return result;
}

Expand All @@ -40,7 +40,7 @@ public async Task<ServerModel> GetOrCreateAsync(ulong serverId, string serverNam
public async Task<CommonResponse> UpdateAsync(ServerModel server)
{
var updateResult = await _serverClient.UpdateAsync(server);

return updateResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<ItemGroup>
<None Remove="Protos\reminder.proto" />
<None Remove="Protos\shared.proto" />
<None Remove="Protos\ticket.proto" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,6 +19,8 @@
<Protobuf Include="Protos\database.proto" GrpcServices="Server" />
<Protobuf Include="Protos\reminder.proto" GrpcServices="Server" />
<Protobuf Include="Protos\server.proto" GrpcServices="Server" />
<Protobuf Include="Protos\shared.proto" GrpcServices="Server" />
<Protobuf Include="Protos\ticket.proto" GrpcServices="Server" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 5 additions & 9 deletions src/HuppyService/HuppyService.Service/Protos/commandLog.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ syntax = "proto3";

option csharp_namespace = "HuppyService.Service.Protos";

import "google/protobuf/empty.proto";
import "Protos/database.proto";
import "Protos/shared.proto";

package CommandLogProto;

service CommandLogProto {
rpc GetCount(google.protobuf.Empty) returns (CommandLogCount);
rpc GetAverageExecutionTime(google.protobuf.Empty) returns (AverageTimeResponse);
rpc GetAiUsage(google.protobuf.Empty) returns (AiUsageResponse);
rpc GetCount(shared.Void) returns (shared.Int32);
rpc GetAverageExecutionTime(shared.Void) returns (AverageTimeResponse);
rpc GetAiUsage(shared.Void) returns (AiUsageResponse);
rpc AddCommand(database.models.CommandLogModel) returns (database.models.CommandLogModel);
rpc RemoveCommand(database.models.CommandLogModel) returns (database.models.CommonResponse);
}

message CommandLogCount {
int32 Count = 1;
rpc RemoveCommand(database.models.CommandLogModel) returns (shared.CommonResponse);
}

message AverageTimeResponse {
Expand Down
6 changes: 1 addition & 5 deletions src/HuppyService/HuppyService.Service/Protos/database.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ message ServerRoomsModel {
uint64 ServerId = 4;
}

message CommonResponse {
bool IsSuccess = 1;
}

message ReminderModel {
int32 Id = 1;
uint64 UnixTime = 2;
Expand All @@ -53,4 +49,4 @@ message TicketModel {
uint64 CreatedDate = 6;
uint64 ClosedDate = 7;
uint64 UserId = 8;
}
}
23 changes: 6 additions & 17 deletions src/HuppyService/HuppyService.Service/Protos/reminder.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ syntax = "proto3";

option csharp_namespace = "HuppyService.Service.Protos";

import "Protos/database.proto";
import "Protos/database.proto";
import "Protos/shared.proto";

package ReminderProto;

service ReminderProto {
rpc GetReminder(GetReminderInput) returns (database.models.ReminderModel);
rpc GetReminderBatch(ReminderBatchInput) returns (ReminderModelCollection);
rpc GetUserReminders(ReminderUserInput) returns (ReminderModelCollection);
rpc GetUserReminders(shared.UserId) returns (ReminderModelCollection);
rpc GetSortedUserReminders(SortedUserRemindersInput) returns (ReminderModelCollection);
rpc GetRemindersCount(GetReminderCountInput) returns (GetReminderCountResponse);
rpc GetRemindersCount(shared.UserId) returns (shared.Int32);
rpc AddReminder(database.models.ReminderModel) returns (database.models.ReminderModel);
rpc RemoveReminder(database.models.ReminderModel) returns (database.models.CommonResponse);
rpc RemoveReminderRange(RemoveReminderRangeInput) returns (database.models.CommonResponse);
rpc RemoveReminder(database.models.ReminderModel) returns (shared.CommonResponse);
rpc RemoveReminderRange(RemoveReminderRangeInput) returns (shared.CommonResponse);
}

message SortedUserRemindersInput {
Expand All @@ -23,14 +24,6 @@ message SortedUserRemindersInput {
int32 Take = 3;
}

message GetReminderCountInput {
uint64 UserId = 1;
}

message GetReminderCountResponse {
int32 Count = 1;
}

message GetReminderInput {
uint64 UserId = 1;
int32 ReminderId = 2;
Expand All @@ -40,10 +33,6 @@ message ReminderBatchInput {
uint64 EndDate = 1;
}

message ReminderUserInput {
uint64 UserId = 1;
}

message ReminderModelCollection {
repeated database.models.ReminderModel ReminderModels = 1;
}
Expand Down
12 changes: 4 additions & 8 deletions src/HuppyService/HuppyService.Service/Protos/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ syntax = "proto3";

option csharp_namespace = "HuppyService.Service.Protos";

import "google/protobuf/empty.proto";
import "Protos/database.proto";
import "Protos/shared.proto";

package ServerProto;

service ServerProto {
rpc Get(ServerIdInput) returns (database.models.ServerModel);
rpc Get(shared.ServerId) returns (database.models.ServerModel);
rpc GetOrCreate(GetOrCreateServerInput) returns (database.models.ServerModel);
rpc Update(database.models.ServerModel) returns(database.models.CommonResponse);
rpc GetAll(google.protobuf.Empty) returns (ServerModelCollection);
}

message ServerIdInput {
uint64 Id = 1;
rpc Update(database.models.ServerModel) returns(shared.CommonResponse);
rpc GetAll(shared.Void) returns (ServerModelCollection);
}

message ServerModelCollection {
Expand Down
23 changes: 23 additions & 0 deletions src/HuppyService/HuppyService.Service/Protos/shared.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";

option csharp_namespace = "HuppyService.Service.Protos";

package shared;

message CommonResponse {
bool IsSuccess = 1;
}

message UserId {
uint64 Id = 1;
}

message ServerId {
uint64 Id = 1;
}

message Int32 {
int32 Number = 1;
}

message Void { }
61 changes: 61 additions & 0 deletions src/HuppyService/HuppyService.Service/Protos/ticket.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
syntax = "proto3";

option csharp_namespace = "HuppyService.Service.Protos";

import "Protos/database.proto";
import "Protos/shared.proto";

package TicketProto;

service TicketProto {
rpc GetCountAsync(shared.UserId) returns (shared.Int32);
rpc GetTickets(shared.Void) returns (TicketModelCollection);
rpc GetUserTickets(shared.UserId) returns (TicketModelCollection);
rpc GetPaginatedTickets(GetPaginatedTicketsInput) returns (TicketModelCollection);
rpc GetUserPaginatedTickets(GetUserPaginatedTicketsInput) returns (TicketModelCollection);
rpc GetTicket(GetTicketInput) returns (database.models.TicketModel);
rpc AddTicket(AddTicketInput) returns (database.models.TicketModel);
rpc RemoveTicketAsync(TicketRemoveInput) returns (shared.CommonResponse);
rpc UpdateTicketAsync(TicketUpdateInput) returns (shared.CommonResponse);
rpc CloseTicket(CloseTicketInput) returns (shared.CommonResponse);
}

message TicketRemoveInput {
string TicketId = 1;
}

message TicketUpdateInput {
string TicketId = 1;
string Description = 2;
}

message CloseTicketInput {
string TicketId = 1;
string Answer = 2;
}

message AddTicketInput {
uint64 UserId = 1;
string Topic = 2;
string Description = 3;
}

message GetTicketInput {
string TicketId = 1;
uint64 UserId = 2;
}

message GetUserPaginatedTicketsInput {
uint64 UserId = 1;
int32 Skip = 2;
int32 Take = 3;
}

message GetPaginatedTicketsInput {
int32 Skip = 1;
int32 Take = 2;
}

message TicketModelCollection {
repeated database.models.TicketModel TicketsModels = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public CommandLogService(ICommandLogRepository commandLogRepository)
_commandLogRepository = commandLogRepository;
}

public override async Task<CommandLogCount> GetCount(Empty request, ServerCallContext context)
public override async Task<Protos.Int32> GetCount(Protos.Void request, ServerCallContext context)
{
var query = await _commandLogRepository.GetAllAsync();
var count = await query.CountAsync();

return new CommandLogCount() { Count = count };
return new Protos.Int32() { Number = count };
}

public override async Task<AiUsageResponse> GetAiUsage(Empty request, ServerCallContext context)
public override async Task<AiUsageResponse> GetAiUsage(Protos.Void request, ServerCallContext context)
{
Dictionary<ulong, int> result = new();

Expand All @@ -46,7 +46,7 @@ public override async Task<AiUsageResponse> GetAiUsage(Empty request, ServerCall
return response;
}

public override async Task<AverageTimeResponse> GetAverageExecutionTime(Empty request, ServerCallContext context)
public override async Task<AverageTimeResponse> GetAverageExecutionTime(Protos.Void request, ServerCallContext context)
{
var query = await _commandLogRepository.GetAllAsync();
var avgTime = await query.AverageAsync(e => e.ExecutionTimeMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public override async Task<ReminderModelCollection> GetSortedUserReminders(Sorte
return result;
}

public override async Task<GetReminderCountResponse> GetRemindersCount(GetReminderCountInput request, ServerCallContext context)
public override async Task<Protos.Int32> GetRemindersCount(Protos.UserId request, ServerCallContext context)
{
var query = await _reminderRepository.GetAllAsync();
return new GetReminderCountResponse() { Count = await query.Where(e => e.UserId == request.UserId).CountAsync() };
return new Protos.Int32 () { Number = await query.Where(e => e.UserId == request.Id).CountAsync() };
}

public override async Task<ReminderModel?> GetReminder(GetReminderInput request, ServerCallContext context)
Expand Down Expand Up @@ -109,11 +109,11 @@ public override async Task<ReminderModel> AddReminder(ReminderModel request, Ser
return result;
}

public override async Task<ReminderModelCollection> GetUserReminders(ReminderUserInput request, ServerCallContext context)
public override async Task<ReminderModelCollection> GetUserReminders(Protos.UserId request, ServerCallContext context)
{
var remindersQuery = await _reminderRepository.GetAllAsync();

var reminders = await remindersQuery.Where(reminder => reminder.UserId == request.UserId)
var reminders = await remindersQuery.Where(reminder => reminder.UserId == request.Id)
.ToListAsync();

ReminderModelCollection result = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ServerService(IServerRepository serverRepository)
_serverRepository = serverRepository;
}

public override async Task<ServerModel> Get(ServerIdInput request, ServerCallContext context)
public override async Task<ServerModel> Get(Protos.ServerId request, ServerCallContext context)
{
var server = await _serverRepository.GetAsync(request.Id);

Expand All @@ -37,7 +37,7 @@ public override async Task<ServerModel> Get(ServerIdInput request, ServerCallCon
};
}

public override async Task<ServerModelCollection> GetAll(Empty request, ServerCallContext context)
public override async Task<ServerModelCollection> GetAll(Protos.Void request, ServerCallContext context)
{
var query = await _serverRepository.GetAllAsync();
var servers = await query.Include(e => e.Rooms).ToListAsync();
Expand Down

0 comments on commit 4cc740c

Please sign in to comment.