-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace ModCore.HansTagImport | ||
{ | ||
public struct HansTag | ||
{ | ||
[JsonProperty("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonProperty("guild")] | ||
public ulong Guild { get; set; } | ||
|
||
[JsonProperty("channel")] | ||
public ulong? Channel { get; set; } | ||
|
||
// unknown | ||
[JsonProperty("kind")] | ||
public int Kind { get; set; } | ||
|
||
[JsonProperty("owner")] | ||
public ulong Owner { get; set; } | ||
|
||
[JsonProperty("hidden")] | ||
public bool Hidden { get; set; } | ||
|
||
[JsonProperty("latestRevision")] | ||
public DateTimeOffset LatestRevision { get; set; } | ||
|
||
[JsonProperty("aliases")] | ||
public string[] Aliases { get; set; } | ||
|
||
[JsonProperty("revisions")] | ||
public HansTagRevision[] Revisions { get; set; } | ||
} | ||
|
||
public struct HansTagRevision | ||
{ | ||
[JsonProperty("contents")] | ||
public string Contents { get; set; } | ||
|
||
[JsonProperty("created")] | ||
public DateTimeOffset Created { get; set; } | ||
|
||
[JsonProperty("user")] | ||
public ulong User { get; set; } | ||
} | ||
} |
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,57 @@ | ||
using ModCore.Database; | ||
using ModCore.Database.DatabaseEntities; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ModCore.HansTagImport | ||
{ | ||
public class Importer | ||
{ | ||
private List<HansTag> _loadedTags = new List<HansTag>(); | ||
|
||
public void LoadTags(Stream tagStream) | ||
{ | ||
var serializer = new JsonSerializer(); | ||
using var streamReader = new StreamReader(tagStream); | ||
using var jsonReader = new JsonTextReader(streamReader); | ||
_loadedTags = serializer.Deserialize<List<HansTag>>(jsonReader); | ||
} | ||
|
||
public async Task DumpToDatabase(DatabaseContext database) | ||
{ | ||
if(_loadedTags == default(List<HansTag>)) | ||
{ | ||
throw new InvalidOperationException("No tags were loaded!"); | ||
} | ||
|
||
foreach(var tag in _loadedTags) | ||
{ | ||
var latestRevision = tag.Revisions.FirstOrDefault(x => x.Created == tag.LatestRevision); | ||
|
||
// check if tag exists | ||
var existsTag = database.Tags.Any(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0)); | ||
|
||
var modcoreTag = existsTag? database.Tags.FirstOrDefault(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0)) | ||
: new DatabaseTag(); | ||
|
||
modcoreTag.Name = tag.Name; | ||
modcoreTag.ChannelId = (long)(tag.Channel ?? 0); | ||
modcoreTag.Contents = latestRevision.Contents; | ||
modcoreTag.CreatedAt = latestRevision.Created.DateTime; | ||
modcoreTag.GuildId = (long)tag.Guild; | ||
modcoreTag.OwnerId = (long)tag.Owner; | ||
|
||
if(existsTag) | ||
database.Tags.Update(modcoreTag); | ||
else | ||
database.Tags.Add(modcoreTag); | ||
} | ||
|
||
await database.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