-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on the api a bit more, rename migrations to what they are suppos…
…ed to be for each db.
- Loading branch information
1 parent
3c6068b
commit 16dd856
Showing
11 changed files
with
204 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
using Mewdeko.Database; | ||
using Mewdeko.Database.Extensions; | ||
using Mewdeko.Api.Services; | ||
using Mewdeko.Database.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Serilog; | ||
|
||
namespace Mewdeko.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class GuildConfig(DbService service) : Controller | ||
[Route("api/[controller]/{guildId}")] | ||
public class GuildConfigController(GuildSettingsService service) : Controller | ||
{ | ||
[HttpGet("{guildId}")] | ||
[HttpGet] | ||
public async Task<IActionResult> GetGuildConfig(ulong guildId) | ||
{ | ||
try | ||
{ | ||
await using var uow = service.GetDbContext(); | ||
var guildConfig = await uow.ForGuildId(guildId); | ||
|
||
if (guildConfig == null) | ||
return NotFound(); | ||
|
||
return Ok(guildConfig); | ||
var config = await service.GetGuildConfig(guildId); | ||
return Ok(config); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "Error getting guild config"); | ||
return StatusCode(500); | ||
} | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> UpdateGuildConfig(ulong guildId, [FromBody] GuildConfig model) | ||
{ | ||
try | ||
{ | ||
await service.UpdateGuildConfig(guildId, model); | ||
return Ok(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "Error updating guild config"); | ||
return StatusCode(500); | ||
} | ||
} | ||
} |
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,50 @@ | ||
using LinqToDB; | ||
using LinqToDB.EntityFrameworkCore; | ||
using Mewdeko.Database; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Mewdeko.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class SuggestionsController(DbService dbService) : Controller | ||
{ | ||
[HttpGet("{guildId}/{userId?}")] | ||
public async Task<IActionResult> GetSuggestions(ulong guildId, ulong userId = 0) | ||
{ | ||
await using var uow = dbService.GetDbContext(); | ||
var suggestions = await uow.Suggestions.ToLinqToDB().ToListAsync(); | ||
if (suggestions.Count == 0) | ||
return NotFound("No suggestions in database at all."); | ||
|
||
var guildSuggestions = suggestions.Where(x => x.GuildId == guildId); | ||
|
||
if (!guildSuggestions.Any()) | ||
return NotFound("No suggestions for this guild."); | ||
|
||
if (userId != 0) | ||
{ | ||
var userSuggestions = guildSuggestions.Where(x => x.UserId == userId); | ||
if (!userSuggestions.Any()) | ||
return NotFound("No suggestions for this user."); | ||
return Ok(userSuggestions); | ||
} | ||
|
||
return Ok(guildSuggestions); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteSuggestion(int id) | ||
{ | ||
await using var uow = dbService.GetDbContext(); | ||
var suggestion = await uow.Suggestions.FirstOrDefaultAsync(x => x.Id == id); | ||
|
||
if (suggestion == null) | ||
return NotFound(); | ||
|
||
uow.Suggestions.Remove(suggestion); | ||
await uow.SaveChangesAsync(); | ||
|
||
return Ok(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Mewdeko.Database; | ||
using Mewdeko.Database.Extensions; | ||
using Mewdeko.Database.Models; | ||
|
||
namespace Mewdeko.Api.Services; | ||
|
||
/// <summary> | ||
/// Service for managing guild settings. | ||
/// </summary> | ||
public class GuildSettingsService( | ||
DbService db, | ||
RedisCache cache) | ||
{ | ||
/// <summary> | ||
/// Gets the guild configuration for the specified guild ID. | ||
/// </summary> | ||
public async Task<GuildConfig> GetGuildConfig(ulong guildId) | ||
{ | ||
var configExists = await cache.GetGuildConfigCache(guildId); | ||
if (configExists != null) | ||
return configExists; | ||
|
||
await using var uow = db.GetDbContext(); | ||
var toLoad = uow.GuildConfigs.IncludeEverything().FirstOrDefault(x => x.GuildId == guildId); | ||
if (toLoad is null) | ||
{ | ||
await uow.ForGuildId(guildId); | ||
toLoad = uow.GuildConfigs.IncludeEverything().FirstOrDefault(x => x.GuildId == guildId); | ||
} | ||
|
||
await cache.SetGuildConfigCache(guildId, toLoad!); | ||
return toLoad!; | ||
} | ||
|
||
/// <summary> | ||
/// Updates the guild configuration. | ||
/// </summary> | ||
public async Task UpdateGuildConfig(ulong guildId, GuildConfig toUpdate) | ||
{ | ||
await using var uow = db.GetDbContext(); | ||
var exists = await cache.GetGuildConfigCache(guildId); | ||
|
||
if (exists is not null) | ||
{ | ||
var properties = typeof(GuildConfig).GetProperties(); | ||
foreach (var property in properties) | ||
{ | ||
var oldValue = property.GetValue(exists); | ||
var newValue = property.GetValue(toUpdate); | ||
|
||
if (newValue != null && !newValue.Equals(oldValue)) | ||
{ | ||
property.SetValue(exists, newValue); | ||
} | ||
} | ||
|
||
await cache.SetGuildConfigCache(guildId, exists); | ||
uow.GuildConfigs.Update(exists); | ||
await uow.SaveChangesAsync(); | ||
} | ||
else | ||
{ | ||
var config = uow.GuildConfigs.IncludeEverything().FirstOrDefault(x => x.Id == toUpdate.Id); | ||
|
||
if (config != null) | ||
{ | ||
var properties = typeof(GuildConfig).GetProperties(); | ||
foreach (var property in properties) | ||
{ | ||
var oldValue = property.GetValue(config); | ||
var newValue = property.GetValue(toUpdate); | ||
|
||
if (newValue != null && !newValue.Equals(oldValue)) | ||
{ | ||
property.SetValue(config, newValue); | ||
} | ||
} | ||
|
||
await cache.SetGuildConfigCache(guildId, config); | ||
uow.GuildConfigs.Update(config); | ||
await uow.SaveChangesAsync(); | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/Mewdeko.Database/Migrations/PostgreSql/AddXpBanner.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace Mewdeko.Database.Models; | ||
|
||
public class FilterWordsChannelIds : DbEntity | ||
{ | ||
public ulong ChannelId { get; set; } | ||
|
||
[ForeignKey("GuildConfigId")] | ||
public int GuildConfigId { get; set; } | ||
|
||
public override bool Equals(object obj) => | ||
obj is FilterWordsChannelIds f | ||
&& f.ChannelId == ChannelId; | ||
|
||
public override int GetHashCode() => ChannelId.GetHashCode(); | ||
} |