Skip to content

Commit

Permalink
Merge pull request #1 from davewalker5/medication-actions
Browse files Browse the repository at this point in the history
Show simple per-medication actions in the output
  • Loading branch information
davewalker5 authored Nov 29, 2024
2 parents bcce952 + 68e0033 commit 82d414a
Show file tree
Hide file tree
Showing 31 changed files with 312 additions and 59 deletions.
Binary file modified diagrams/Add-Stock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Delete-Medication.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Dose-Required.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Empty-Medications-File.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/New-Medication.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Review-Stock-Levels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Set-Dose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Set-Stock-Level.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Take-Dose-For-All-Medications.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified diagrams/Take-Single-Medication-Dose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>MedicineTracker.BusinessLogic</PackageId>
<PackageVersion>2.0.0.0</PackageVersion>
<PackageVersion>2.1.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) 2023, 2024 Dave Walker</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,9 +16,13 @@
<PackageProjectUrl>https://github.com/davewalker5/MedicineTracker</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>2.0.0.0</ReleaseVersion>
<ReleaseVersion>2.1.0.0</ReleaseVersion>
</PropertyGroup>

<!-- <ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx" />
</ItemGroup> -->

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
Expand Down
21 changes: 21 additions & 0 deletions src/MedicineTracker.BusinessLogic/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="TakeDoseAction" xml:space="preserve">
<value>Take dose</value>
</data>
<data name="OrderMoreAction" xml:space="preserve">
<value>Order more</value>
</data>
</root>
46 changes: 46 additions & 0 deletions src/MedicineTracker.BusinessLogic/Stock/ActionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using MedicineTracker.Entities.Tracker;
using MedicineTracker.Entities.Interfaces;
using MedicineTracker.Entities.Configuration;
using System.Resources;
using System.Reflection;

namespace MedicineTracker.BusinessLogic.Stock
{
public class ActionGenerator : IActionGenerator
{
private readonly ResourceManager _resources = new("MedicineTracker.BusinessLogic.Properties.Resources", Assembly.GetExecutingAssembly());
private readonly ApplicationSettings _settings;

public ActionGenerator(ApplicationSettings settings)
=> _settings = settings;

/// <summary>
/// Return a list of actions for a medication
/// </summary>
/// <param name="medication"></param>
/// <returns></returns>
public IList<string> GetActions(Medication medication)
{
var actions = new List<string>();

if (medication.LastTaken < MedicineTrackerDateUtils.TodayWithoutTime())
{
string action = _resources.GetString("TakeDoseAction");
actions.Add(action);
}

if (medication.DaysRemaining() <= _settings.WarningDays)
{
string action = _resources.GetString("OrderMoreAction");
actions.Add(action);
}

return actions;
}
}
}





Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MedicineTracker.BusinessLogic.Stock
{
public static class MedicineTrackerDateUtils
{
/// <summary>
/// Return a date with the time components set to 0
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime DateWithoutTime(DateTime date)
=> new(date.Year, date.Month, date.Day, 0, 0, 0, 0);

/// <summary>
/// Return todays date with the time components set to 0
/// </summary>
public static DateTime TodayWithoutTime()
=> DateWithoutTime(DateTime.Now);
}
}
14 changes: 3 additions & 11 deletions src/MedicineTracker.BusinessLogic/Stock/StockUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void Increment(IEnumerable<Medication> medications, int doses)
public void FastForward(IEnumerable<Medication> medications, int index)
{
var medication = medications.ElementAt(index);
var now = DateWithoutTime(DateTime.Now);
var stockDate = DateWithoutTime(medication.LastTaken);
var now = MedicineTrackerDateUtils.TodayWithoutTime();
var stockDate = MedicineTrackerDateUtils.DateWithoutTime(medication.LastTaken);
var doses = (int)(now - stockDate).TotalDays;
if (doses > 0)
{
Expand Down Expand Up @@ -156,7 +156,7 @@ private static DateTime CalculateStockDate(DateTime stockDate, int doses)
// 2: "doses" is < 0, so we're subtracting tablets from the stock i.e. "taking" and need to roll forward the date
// 3: "doses" is 0, so we're skipping and need to roll forward the date by one day
int daysToAdvance = doses != 0 ? -doses : 1;
var date = DateWithoutTime(stockDate.AddDays(daysToAdvance));
var date = MedicineTrackerDateUtils.DateWithoutTime(stockDate.AddDays(daysToAdvance));

// If the resulting date is greater than now, then this is attempting to take doses beyond
// today, which isn't allowed
Expand All @@ -168,13 +168,5 @@ private static DateTime CalculateStockDate(DateTime stockDate, int doses)

return date;
}

/// <summary>
/// Return a date with the time components set to 0
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private static DateTime DateWithoutTime(DateTime date)
=> new(date.Year, date.Month, date.Day, 0, 0, 0, 0);
}
}
9 changes: 9 additions & 0 deletions src/MedicineTracker.Entities/Interfaces/IActionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MedicineTracker.Entities.Tracker;

namespace MedicineTracker.Entities.Interfaces
{
public interface IActionGenerator
{
IList<string> GetActions(Medication medication);
}
}
4 changes: 2 additions & 2 deletions src/MedicineTracker.Entities/MedicineTracker.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>MedicineTracker.Entities</PackageId>
<PackageVersion>2.0.0.0</PackageVersion>
<PackageVersion>2.1.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) 2023, 2024 Dave Walker</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,7 +16,7 @@
<PackageProjectUrl>https://github.com/davewalker5/MedicineTracker</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>2.0.0.0</ReleaseVersion>
<ReleaseVersion>2.1.0.0</ReleaseVersion>
</PropertyGroup>

</Project>
82 changes: 82 additions & 0 deletions src/MedicineTracker.Tests/ActionGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using MedicineTracker.BusinessLogic.Stock;
using MedicineTracker.Entities.Configuration;
using MedicineTracker.Entities.Tracker;

namespace MedicineTracker.Tests
{
[TestClass]
public class ActionGeneratorTest
{
private const int WarningDays = 14;
private const string TakeDoseAction = "Take dose";
private const string OrderMoreAction = "Order more";

private ActionGenerator _generator;

[TestInitialize]
public void Initialise()
{
_generator = new(new ApplicationSettings{WarningDays = WarningDays});
}

[TestMethod]
public void NoRelevantActionsTest()
{
var medication = new Medication
{
LastTaken = MedicineTrackerDateUtils.TodayWithoutTime(),
DailyDose = 1,
Stock = WarningDays + 1
};

var actions = _generator.GetActions(medication);
Assert.AreEqual(0, actions.Count);
}

[TestMethod]
public void TakeDoseTest()
{
var medication = new Medication
{
LastTaken = MedicineTrackerDateUtils.TodayWithoutTime().AddDays(-1),
DailyDose = 1,
Stock = WarningDays + 1
};

var actions = _generator.GetActions(medication);
Assert.AreEqual(1, actions.Count);
Assert.IsTrue(actions.Contains(TakeDoseAction, StringComparer.OrdinalIgnoreCase));
}

[TestMethod]
public void OrderMoreTest()
{
var medication = new Medication
{
LastTaken = MedicineTrackerDateUtils.TodayWithoutTime(),
DailyDose = 1,
Stock = WarningDays
};

var actions = _generator.GetActions(medication);
Assert.AreEqual(1, actions.Count);
Assert.IsTrue(actions.Contains(OrderMoreAction, StringComparer.OrdinalIgnoreCase));
}

[TestMethod]
public void MultipleActionTest()
{
var medication = new Medication
{
LastTaken = MedicineTrackerDateUtils.TodayWithoutTime().AddDays(-1),
DailyDose = 1,
Stock = WarningDays
};

var actions = _generator.GetActions(medication);
Assert.AreEqual(2, actions.Count);
Assert.IsTrue(actions.Contains(TakeDoseAction, StringComparer.OrdinalIgnoreCase));
Assert.IsTrue(actions.Contains(OrderMoreAction, StringComparer.OrdinalIgnoreCase));
}
}
}
5 changes: 3 additions & 2 deletions src/MedicineTracker.Tests/MedicationEntityTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using MedicineTracker.Entities.Tracker;
using MedicineTracker.BusinessLogic.Stock;
using MedicineTracker.Entities.Tracker;

namespace MedicineTracker.Tests
{
[TestClass]
public class MedicationEntityTest
{
private Medication _medication;
private readonly DateTime _initialStockDate = TestUtils.NowWithoutTime();
private readonly DateTime _initialStockDate = MedicineTrackerDateUtils.TodayWithoutTime();

[TestInitialize]
public void TestInitialise()
Expand Down
2 changes: 1 addition & 1 deletion src/MedicineTracker.Tests/MedicationUpdaterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Initialise()
public void AddMedicationTest()
{
var medications = new List<Medication>();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_updater.AddMedication(medications, MedicationName);

Assert.AreEqual(1, medications.Count);
Expand Down
5 changes: 3 additions & 2 deletions src/MedicineTracker.Tests/MedicationWriterTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MedicineTracker.BusinessLogic.Storage;
using MedicineTracker.BusinessLogic.Stock;
using MedicineTracker.BusinessLogic.Storage;
using MedicineTracker.Entities.Tracker;

namespace MedicineTracker.Tests
Expand All @@ -9,7 +10,7 @@ public class MedicationWriterTest
private const string MedicationName = "Some Medication";
private const int DailyDose = 2;
private const int Stock = 36;
private readonly DateTime _initialStockDate = TestUtils.NowWithoutTime().AddDays(-1);
private readonly DateTime _initialStockDate = MedicineTrackerDateUtils.TodayWithoutTime().AddDays(-1);

[TestMethod]
public void WriteMedicationsTest()
Expand Down
12 changes: 6 additions & 6 deletions src/MedicineTracker.Tests/MedicineManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MedicineManagerTest
private const string SecondMedicationName = "Another Medication";
private const int DailyDose = 2;
private const int Stock = 36;
private readonly DateTime _initialStockDate = TestUtils.NowWithoutTime().AddDays(-1);
private readonly DateTime _initialStockDate = MedicineTrackerDateUtils.TodayWithoutTime().AddDays(-1);

private string _dataFilePath;
private IMedicineManager _manager;
Expand Down Expand Up @@ -121,7 +121,7 @@ public void SetStockTest()
public void TakeDoseForSingleMedicationTest()
{
_manager.Read();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_manager.TakeDose("1");

Assert.AreEqual(Stock - DailyDose, _manager.Medications.ElementAt(0).Stock);
Expand All @@ -132,7 +132,7 @@ public void TakeDoseForSingleMedicationTest()
public void TakeDoseForAllMedicationsTest()
{
_manager.Read();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_manager.TakeDose("*");

Assert.AreEqual(Stock - DailyDose, _manager.Medications.ElementAt(0).Stock);
Expand Down Expand Up @@ -165,7 +165,7 @@ public void UntakeDoseForAllMedicationsTest()
public void FastForwardForSingleMedicationTest()
{
_manager.Read();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_manager.FastForward("1");

Assert.AreEqual(Stock - DailyDose, _manager.Medications.ElementAt(0).Stock);
Expand All @@ -176,7 +176,7 @@ public void FastForwardForSingleMedicationTest()
public void FastForwardForAllMedicationsTest()
{
_manager.Read();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_manager.FastForward("*");

Assert.AreEqual(Stock - DailyDose, _manager.Medications.ElementAt(0).Stock);
Expand All @@ -196,7 +196,7 @@ public void SetDoseTest()
public void AddMedicationTest()
{
_manager.Read();
var expectedDate = TestUtils.NowWithoutTime();
var expectedDate = MedicineTrackerDateUtils.TodayWithoutTime();
_manager.AddMedication(SecondMedicationName);
Assert.AreEqual(2, _manager.Medications.Count);

Expand Down
33 changes: 33 additions & 0 deletions src/MedicineTracker.Tests/MedicineTrackerDateUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using MedicineTracker.BusinessLogic.Stock;

namespace MedicineTracker.Tests
{
[TestClass]
public class MedicineTrackerDateUtilsTest
{
[TestMethod]
public void DateWithoutTimeTest()
{
var date = MedicineTrackerDateUtils.DateWithoutTime(new(2023, 4, 6, 13, 56, 22));
Assert.AreEqual(2023, date.Year);
Assert.AreEqual(4, date.Month);
Assert.AreEqual(6, date.Day);
Assert.AreEqual(0, date.Hour);
Assert.AreEqual(0, date.Minute);
Assert.AreEqual(0, date.Second);
}

[TestMethod]
public void TodayWithoutTimeTest()
{
var today = DateTime.Now;
var date = MedicineTrackerDateUtils.TodayWithoutTime();
Assert.AreEqual(today.Year, date.Year);
Assert.AreEqual(today.Month, date.Month);
Assert.AreEqual(today.Day, date.Day);
Assert.AreEqual(0, date.Hour);
Assert.AreEqual(0, date.Minute);
Assert.AreEqual(0, date.Second);
}
}
}
Loading

0 comments on commit 82d414a

Please sign in to comment.