Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions RackPeek.Domain/Api/UpsertInventoryUseCase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Text.Json.Serialization;
using RackPeek.Domain.Persistence;
using RackPeek.Domain.Persistence.Yaml;
using RackPeek.Domain.Resources;
using RackPeek.Domain.Resources.Connections;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

Expand Down Expand Up @@ -49,24 +51,14 @@ public async Task<ImportYamlResponse> ExecuteAsync(ImportYamlRequest request) {
rawJson,
_jsonOptions)
?? throw new ValidationException("Invalid JSON structure.");
// Generate YAML only for persistence layer
ISerializer yamlSerializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new StorageSizeYamlConverter())
.WithTypeConverter(new NotesStringYamlConverter())
.ConfigureDefaultValuesHandling(
DefaultValuesHandling.OmitNull |
DefaultValuesHandling.OmitEmptyCollections)
.Build();

yamlInput = yamlSerializer.Serialize(incomingRoot);

yamlInput = YamlResourceCollection.SerializeRootAsync(incomingRoot);
}

if (incomingRoot.Resources == null)
throw new ValidationException("Missing 'resources' section.");

// 2️Compute Diff

List<Resource>? incomingResources = incomingRoot.Resources;
IReadOnlyList<Resource> currentResources = await repo.GetAllOfTypeAsync<Resource>();

Expand Down
12 changes: 9 additions & 3 deletions RackPeek.Domain/Persistence/Yaml/YamlResourceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ private async Task BackupOriginalAsync(string originalYaml) {
}

private async Task SaveRootAsync(YamlRoot? root) {
var contents = SerializeRootAsync(root);
await fileStore.WriteAllTextAsync(filePath, contents);
}

public static string SerializeRootAsync(YamlRoot? root) {
ISerializer serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new StorageSizeYamlConverter())
Expand All @@ -377,10 +382,11 @@ private async Task SaveRootAsync(YamlRoot? root) {
["connections"] = root.Connections ?? new List<Connection>()
};

await fileStore.WriteAllTextAsync(filePath, serializer.Serialize(payload));
return serializer.Serialize(payload);
}

private string GetKind(Resource resource) {

private static string GetKind(Resource resource) {
return resource switch {
Server => "Server",
Switch => "Switch",
Expand All @@ -396,7 +402,7 @@ private string GetKind(Resource resource) {
};
}

private OrderedDictionary SerializeResource(Resource resource) {
public static OrderedDictionary SerializeResource(Resource resource) {
var map = new OrderedDictionary {
["kind"] = GetKind(resource)
};
Expand Down