-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T #98 Updated protos in Huppy.Service
- Loading branch information
Showing
6 changed files
with
140 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/HuppyService/HuppyService.Service/Services/ServerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using Google.Protobuf.WellKnownTypes; | ||
using Grpc.Core; | ||
using HuppyService.Core.Interfaces.IRepositories; | ||
using HuppyService.Core.Models; | ||
using HuppyService.Service.Protos; | ||
using HuppyService.Service.Protos.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HuppyService.Service.Services | ||
{ | ||
public class ServerService : ServerProto.ServerProtoBase | ||
{ | ||
private readonly IServerRepository _serverRepository; | ||
public ServerService(IServerRepository serverRepository) | ||
{ | ||
_serverRepository = serverRepository; | ||
} | ||
|
||
public override async Task<ServerModelCollection> GetAll(Empty request, ServerCallContext context) | ||
{ | ||
var query = await _serverRepository.GetAllAsync(); | ||
var servers = await query.Include(e => e.Rooms).ToListAsync(); | ||
|
||
ServerModelCollection result = new(); | ||
result.ServerModel.AddRange(servers.Select(server => new ServerModel() | ||
{ | ||
Id = server.Id, | ||
ServerName = server.ServerName, | ||
UseGreet = server.UseGreet, | ||
GreetMessage = server.GreetMessage, | ||
RoleId = server.RoleID, | ||
ServerRoomsId = server.ServerRoomsId, | ||
Rooms = new() | ||
{ | ||
Id = server.Rooms.Id, | ||
GreetingRoom = server.Rooms.GreetingRoom, | ||
OutputRoom = server.Rooms.OutputRoom, | ||
ServerId = server.Rooms.ServerId | ||
} | ||
}).ToList()); | ||
|
||
return result; | ||
} | ||
|
||
public override async Task<ServerModel> GetOrCreate(GetOrCreateServerInput request, ServerCallContext context) | ||
{ | ||
var server = await _serverRepository.GetAsync(request.Id); | ||
|
||
if (server is not null) | ||
{ | ||
if (server.Rooms is null) | ||
{ | ||
server.Rooms = new() | ||
{ | ||
OutputRoom = request.DefaultChannel, | ||
GreetingRoom = default, | ||
ServerId = request.Id | ||
}; | ||
|
||
await _serverRepository.UpdateAsync(server); | ||
await _serverRepository.SaveChangesAsync(); | ||
} | ||
|
||
return new ServerModel() | ||
{ | ||
Id = server.Id, | ||
GreetMessage = server.GreetMessage, | ||
RoleId = server.RoleID, | ||
ServerName = server.ServerName, | ||
UseGreet = server.UseGreet, | ||
Rooms = new() | ||
{ | ||
GreetingRoom = server.Rooms.GreetingRoom, | ||
OutputRoom = server.Rooms.OutputRoom, | ||
ServerId = server.Id | ||
} | ||
}; | ||
} | ||
|
||
server = new() | ||
{ | ||
Id = request.Id, | ||
GreetMessage = "Welcome {username}!", | ||
ServerName = request.ServerName, | ||
RoleID = 0, | ||
UseGreet = false, | ||
Rooms = new() | ||
{ | ||
OutputRoom = request.DefaultChannel, | ||
GreetingRoom = 0, | ||
ServerId = request.Id | ||
} | ||
}; | ||
|
||
await _serverRepository.AddAsync(server); | ||
await _serverRepository.SaveChangesAsync(); | ||
|
||
return new ServerModel() | ||
{ | ||
Id = server.Id, | ||
GreetMessage = server.GreetMessage, | ||
RoleId = server.RoleID, | ||
ServerName = server.ServerName, | ||
UseGreet = server.UseGreet, | ||
Rooms = new() | ||
{ | ||
GreetingRoom = server.Rooms.GreetingRoom, | ||
OutputRoom = server.Rooms.OutputRoom, | ||
ServerId = server.Id | ||
} | ||
}; | ||
} | ||
|
||
public override async Task<CommonResponse> UpdateAsync(ServerModel request, ServerCallContext context) | ||
{ | ||
Server server = new() | ||
{ | ||
Id = request.Id, | ||
ServerName = request.ServerName, | ||
GreetMessage = request.GreetMessage, | ||
RoleID = request.RoleId, | ||
UseGreet = request.UseGreet, | ||
ServerRoomsId = request.ServerRoomsId, | ||
Rooms = new() { | ||
Id = request.Id, | ||
GreetingRoom = request.Rooms.GreetingRoom, | ||
OutputRoom = request.Rooms.OutputRoom, | ||
ServerId = request.Rooms.ServerId | ||
} | ||
}; | ||
|
||
await _serverRepository.UpdateAsync(server); | ||
return new CommonResponse() { IsSuccess = true }; | ||
} | ||
} | ||
} |