Skip to content

Commit

Permalink
T #98 huppy microservice base
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Sep 20, 2022
1 parent 8ac432b commit 9ea6731
Show file tree
Hide file tree
Showing 42 changed files with 744 additions and 50 deletions.
14 changes: 2 additions & 12 deletions src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,11 @@ public ModuleConfigurator AddAppSettings(AppSettings? settings = null)

public ModuleConfigurator AddGRPCServices()
{
//var channel = GrpcChannel.ForAddress("https://localhost:9001");
//var client = new HuppyService.Service.Protos.GPT.GPTClient(channel);

//_services.AddSingleton(client);

_services.AddGrpcClient<GPT.GPTClient>((services, options) =>
{
//var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcBasket;
options.Address = new Uri("https://localhost:9001");
//var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.HuppyCoreUrl;
options.Address = new Uri(_appSettings?.Microservices?.HuppyCoreUrl!);
});
//.AddInterceptor<GrpcExceptionInterceptor>();
//var client = new Huppy.Core;
//var client = new GPT.GPTClient(channel);

//_services.AddSingleton(client);

return this;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Huppy/Huppy.Kernel/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AppSettings
public Logger? Logger { get; set; }
public GPTSettings? GPT { get; set; }
public UrbanApi? UrbanApi { get; set; }
public Microservices? Microservices { get; set; }

[JsonIgnore]
private readonly static string FILE_NAME = AppContext.BaseDirectory + "appsettings.json";
Expand Down Expand Up @@ -67,7 +68,8 @@ public static AppSettings Create()
BaseUrl = "https://mashape-community-urban-dictionary.p.rapidapi.com/define",
Host = "mashape-community-urban-dictionary.p.rapidapi.com",
Key = ""
}
},
Microservices = new()
};

JsonSerializerOptions options = new()
Expand All @@ -87,6 +89,11 @@ private static void CreateFolders()
}
}

public class Microservices
{
public string? HuppyCoreUrl { get; set; }
}

public class Logger
{
public string? LogLevel { get; set; }
Expand Down
91 changes: 91 additions & 0 deletions src/HuppyService/HuppyService.Core/Abstraction/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Microsoft.EntityFrameworkCore;

namespace HuppyService.Core.Abstraction;

public class BaseRepository<TKeyType, TEntity, TContext> : IRepository<TKeyType, TEntity>
where TKeyType : IConvertible
where TEntity : DbModel<TKeyType>, new()
where TContext : DbContext, new()
{
protected internal readonly TContext _context;
public BaseRepository(TContext context)
{
_context = context ?? new TContext();
}

public virtual async Task<bool> AddAsync(TEntity? entity)
{
if (entity is null) return false;

var doesExist = await _context.Set<TEntity>().AnyAsync(entry => entry.Id.Equals(entity.Id));

if (doesExist) return false;

await _context.Set<TEntity>().AddAsync(entity);
return true;
}

public virtual async Task<bool> AddRangeAsync(IEnumerable<TEntity> entities)
{
if (entities is null) return false;

await _context.Set<TEntity>().AddRangeAsync(entities);
return true;
}

public virtual Task<IQueryable<TEntity>> GetAllAsync()
{
return Task.FromResult(_context.Set<TEntity>().AsQueryable());
}

public virtual async Task<TEntity?> GetAsync(TKeyType id)
{
return await _context.Set<TEntity>().FirstOrDefaultAsync(entry => entry.Id.Equals(id));
}

public virtual async Task<bool> RemoveAsync(TKeyType id)
{
TEntity entity = new() { Id = id };

var doesExist = await _context.Set<TEntity>().AnyAsync(entry => entry.Id.Equals(entity.Id));

if (!doesExist) return false;

_context.Set<TEntity>().Remove(entity);

return true;
}

public virtual async Task<bool> RemoveAsync(TEntity? entity)
{
if (entity is null) return false;

var doesExist = await _context.Set<TEntity>().AnyAsync(entry => entry.Id.Equals(entity.Id));
if (!doesExist) return false;

_context.Set<TEntity>().Remove(entity);

return true;
}

public virtual Task UpdateAsync(TEntity? entity)
{
if (entity is null) return Task.CompletedTask;

_context.Set<TEntity>().Update(entity);

return Task.CompletedTask;
}

public virtual Task UpdateRange(IEnumerable<TEntity> entities)
{
_context.Set<TEntity>().UpdateRange(entities);

return Task.CompletedTask;
}

public virtual async Task SaveChangesAsync()
{
await _context.SaveChangesAsync();
}
}
10 changes: 10 additions & 0 deletions src/HuppyService/HuppyService.Core/Abstraction/DbModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;

namespace HuppyService.Core.Abstraction
{

public class DbModel<TKey> where TKey : IConvertible
{
public virtual TKey Id { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/HuppyService/HuppyService.Core/Abstraction/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace HuppyService.Core.Abstraction
{
public interface IRepository<Tkey, TEntity> where Tkey : IConvertible where TEntity : DbModel<Tkey>
{
Task<TEntity?> GetAsync(Tkey id);
Task<IQueryable<TEntity>> GetAllAsync();
Task<bool> AddAsync(TEntity? entity);
Task<bool> AddRangeAsync(IEnumerable<TEntity> entities);
Task<bool> RemoveAsync(Tkey id);
Task<bool> RemoveAsync(TEntity? entity);
Task UpdateAsync(TEntity? entity);
Task UpdateRange(IEnumerable<TEntity> entities);
Task SaveChangesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace HuppyService.Kernel.Constants
namespace HuppyService.Core.Constants
{
public class GPTEndpoints
{
Expand Down
8 changes: 8 additions & 0 deletions src/HuppyService/HuppyService.Core/Entities/AiUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HuppyService.Core.Entities
{
public class AiUser
{
public string? Username;
public int Count;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace HuppyService.Service.Entities
namespace HuppyService.Core.Entities
{
public class GPTRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace HuppyService.Service.Entities
namespace HuppyService.Core.Entities
{
public class Choice
{
Expand Down
12 changes: 12 additions & 0 deletions src/HuppyService/HuppyService.Core/Entities/Options/GPTOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace HuppyService.Core.Entities.Options
{
public class GPTOptions
{
public string? BaseUrl { get; set; }
public string? ApiKey { get; set; }
public string? Orgranization { get; set; }
public int FreeMessageQuota { get; set; }
public string? AiContextMessage { get; set; }
public bool IsEnabled { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HuppyService.Core.Entities.Options
{
public class MiscellaneousOptions
{
public string? BotToken { get; set; }
public string? DatabaseConnectionString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HuppyService.Core.Entities.Options
{
public class UrbanApiOptions
{
public string? BaseUrl { get; set; }
public string? Host { get; set; }
public string? Key { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/HuppyService/HuppyService.Core/HuppyService.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
</ItemGroup>

<ItemGroup>
<Folder Include="Utilities\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using HuppyService.Core.Abstraction;
using HuppyService.Core.Entities;
using HuppyService.Core.Models;

namespace HuppyService.Core.Interfaces.IRepositories
{
public interface ICommandLogRepository : IRepository<int, CommandLog>
{
Task<int> GetCount();
Task<Dictionary<ulong, AiUser>> GetAiUsage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HuppyService.Core.Abstraction;
using HuppyService.Core.Models;

namespace HuppyService.Core.Interfaces.IRepositories
{
public interface IReminderRepository : IRepository<int, Reminder>
{
Task<Reminder?> GetAsync(ulong userId, int id);
Task RemoveRangeAsync(ICollection<int> reminderIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HuppyService.Core.Abstraction;
using HuppyService.Core.Models;

namespace HuppyService.Core.Interfaces.IRepositories
{
public interface IServerRepository : IRepository<ulong, Server>
{
Task<Server> GetOrCreateAsync(ulong guildId, ulong defaultChannel, string guildName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HuppyService.Core.Abstraction;
using HuppyService.Core.Models;

namespace HuppyService.Core.Interfaces.IRepositories
{
public interface ITicketRepository : IRepository<string, Ticket>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HuppyService.Core.Abstraction;
using HuppyService.Core.Models;

namespace HuppyService.Core.Interfaces.IRepositories
{
public interface IUserRepository : IRepository<ulong, User>
{
Task<Dictionary<ulong, string?>> GetUsersForCacheAsync();
}
}
25 changes: 25 additions & 0 deletions src/HuppyService/HuppyService.Core/Models/CommandLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using HuppyService.Core.Abstraction;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HuppyService.Core.Models;

public class CommandLog : DbModel<int>
{
[Key]
public override int Id { get; set; }
public string? CommandName { get; set; }
public string? ErrorMessage { get; set; }
public DateTime? Date { get; set; }
public bool IsSuccess { get; set; }
public long ExecutionTimeMs { get; set; }
public ulong ChannelId { get; set; }

[ForeignKey("UserId")]
public ulong UserId { get; set; }
public virtual User? User { get; set; }

[ForeignKey("GuildId")]
public ulong? GuildId { get; set; }
public virtual Server? Guild { get; set; }
}
17 changes: 17 additions & 0 deletions src/HuppyService/HuppyService.Core/Models/Reminder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using HuppyService.Core.Abstraction;
using System.ComponentModel.DataAnnotations.Schema;

namespace HuppyService.Core.Models
{
public class Reminder : DbModel<int>
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override int Id { get; set; }
public DateTime RemindDate { get; set; }
public string Message { get; set; } = null!;

[ForeignKey("UserId")]
public ulong UserId { get; set; }
public virtual User? User { get; set; } = null!;
}
}
20 changes: 20 additions & 0 deletions src/HuppyService/HuppyService.Core/Models/Server.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HuppyService.Core.Abstraction;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HuppyService.Core.Models
{
public class Server : DbModel<ulong>
{
[Key]
public override ulong Id { get; set; }
public string? ServerName { get; set; }
public bool UseGreet { get; set; }
public string? GreetMessage { get; set; }
public ulong RoleID { get; set; }

[ForeignKey("ServerRoomsId")]
public ulong? ServerRoomsID { get; set; }
public virtual ServerRooms? Rooms { get; set; }
}
}
Loading

0 comments on commit 9ea6731

Please sign in to comment.