Skip to content

Commit

Permalink
Added Saving to Core functionality, It's a Basic list of parties whic…
Browse files Browse the repository at this point in the history
…h are lists of characters, Added newtonsoft to forms to it works. sander1095#6
  • Loading branch information
herrozerro committed Oct 11, 2019
1 parent 4b89c86 commit 3333636
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1 deletion.
File renamed without changes.
31 changes: 31 additions & 0 deletions src/DnDCombatTracker.Core/Data/FileIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace DnDCombatTracker.Core.Data
{
public static class FileIO
{
public static string GetFileString(string fileName, string folder)
{
if (!File.Exists(string.Join("\\", folder, fileName)))
{
return null;
}

var path = Path.GetFullPath(string.Join("\\", folder, fileName));


var str = File.ReadAllText(string.Join("\\", folder, fileName));

return str;
}

public static void SaveFileString(string fileName, string folder, string content)
{
File.WriteAllText(string.Join("\\", folder, fileName), content);
}
}
}
6 changes: 5 additions & 1 deletion src/DnDCombatTracker.Core/DnDCombatTracker.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/DnDCombatTracker.Core/PartyManagement/Party.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DnDCombatTracker.Core
{
public class Party
{
public Guid partyId { get; set; }
public string Name { get; set; }
public List<Character> Characters { get; set; }
public string Notes { get; set; }
}
}
49 changes: 49 additions & 0 deletions src/DnDCombatTracker.Core/PartyManagement/PartyManagerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DnDCombatTracker.Core
{
public class PartyManagerService
{

public List<Party> GetParties(string filename, string folder)
{
var partyStr = Data.FileIO.GetFileString(filename, folder);

if (partyStr == null)
{
return new List<Party>();
}
var parties = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Party>>(partyStr);

return parties;
}

public void SaveParty(string filename, string folder, Party party)
{
var parties = GetParties(filename, folder);

var PartyToUpdate = parties.FirstOrDefault(x => x.partyId == party.partyId);

if (PartyToUpdate == null)
{
parties.Add(party);
}
else
{
PartyToUpdate = party;
}

SaveParties(filename, folder, parties);
}

public void SaveParties(string filename, string folder, List<Party> parties)
{
var partiesString = Newtonsoft.Json.JsonConvert.SerializeObject(parties);

Data.FileIO.SaveFileString(filename, folder, partiesString);
}
}
}
4 changes: 4 additions & 0 deletions src/DnDCombatTracker.Forms/DnDCombatTracker.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -109,6 +112,7 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
4 changes: 4 additions & 0 deletions src/DnDCombatTracker.Forms/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
</packages>

0 comments on commit 3333636

Please sign in to comment.