Skip to content

Commit

Permalink
T #98 Updated protos in Huppy.Service
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Oct 6, 2022
1 parent e5f4451 commit d981136
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace HuppyService.Core.Interfaces.IRepositories
{
public interface IServerRepository : IRepository<ulong, Server>
{
Task<Server> GetOrCreateAsync(ulong guildId, ulong defaultChannel, string guildName);
}
}
2 changes: 1 addition & 1 deletion src/HuppyService/HuppyService.Core/Models/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Server : DbModel<ulong>
public ulong RoleID { get; set; }

[ForeignKey("ServerRoomsId")]
public ulong? ServerRommsId { get; set; }
public ulong ServerRoomsId { get; set; }
public virtual ServerRooms? Rooms { get; set; }
public virtual IList<CommandLog>? CommangLogs { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/HuppyService/HuppyService.Core/Models/ServerRooms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ServerRooms : DbModel<ulong>
public ulong GreetingRoom { get; set; }

[ForeignKey("ServerId")]
public ulong? ServerId { get; set; }
public ulong ServerId { get; set; }
public virtual Server? Server { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,4 @@ public ServerRepository(HuppyDbContext context) : base(context) { }
{
return await _context.Servers.Include(e => e.Rooms).FirstOrDefaultAsync(entry => entry.Id == id);
}

public async Task<Server> GetOrCreateAsync(ulong guildId, ulong defaultChannel, string guildName)
{
var server = await GetAsync(guildId);

if (server is not null)
{
if (server.Rooms is null)
{
server.Rooms = new()
{
OutputRoom = defaultChannel,
GreetingRoom = default
};

await base.UpdateAsync(server);
await base.SaveChangesAsync();
}

return server;
}

server = new()
{
Id = guildId,
GreetMessage = "Welcome {username}!",
Rooms = new()
{
OutputRoom = defaultChannel,
GreetingRoom = 0
},
ServerName = guildName,
RoleID = 0,
UseGreet = false,
};

await base.AddAsync(server);
await base.SaveChangesAsync();

return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override async Task<CommandLogModel> AddCommand(CommandLogModel request,
return null!;
}

public override async Task<CommandLogResponse> RemoveCommand(CommandLogModel request, ServerCallContext context)
public override async Task<CommonResponse> RemoveCommand(CommandLogModel request, ServerCallContext context)
{
Core.Models.CommandLog commandLog = new()
{
Expand All @@ -97,7 +97,7 @@ public override async Task<CommandLogResponse> RemoveCommand(CommandLogModel req
await _commandLogRepository.RemoveAsync(commandLog);
await _commandLogRepository.SaveChangesAsync();

return new CommandLogResponse() { IsSuccess = true };
return new CommonResponse() { IsSuccess = true };
}
}
}
136 changes: 136 additions & 0 deletions src/HuppyService/HuppyService.Service/Services/ServerService.cs
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 };
}
}
}

0 comments on commit d981136

Please sign in to comment.