Skip to content

Commit

Permalink
Merge pull request #113 from M-Zuber/Development
Browse files Browse the repository at this point in the history
Code cleanup and bug fixes
  • Loading branch information
M-Zuber committed Dec 29, 2015
2 parents 4bdde2b + 86672f7 commit 6d32e40
Show file tree
Hide file tree
Showing 57 changed files with 442 additions and 424 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#28/12/2015

- Update CategoryService dictionaries to use enum. [code](https://github.com/M-Zuber/MyHome/commit/6d5b1cdeff084b5040194a0dd3e17d22b212048f)
- Cleanup CategoryService code to be more idoimatic (also use some `C#`isms) [code](https://github.com/M-Zuber/MyHome/commit/cf5f5ea0627b640926425d08afbf6e5fc2001230)
- General code cleanup [code](https://github.com/M-Zuber/MyHome/commit/c01be5c423f40dea61e00f6c0b651a461bec63ac) [code](https://github.com/M-Zuber/MyHome/commit/fed97602c2fc28e0122c77b5b30a86f7770c812a) [code](https://github.com/M-Zuber/MyHome/commit/167da4c53ebf62abd43b58f96bfa47dce76f18c8)
- Fix [#109](https://github.com/M-Zuber/MyHome/issues/109) [code](https://github.com/M-Zuber/MyHome/commit/70b1799cad5df57961df978f9a1c5665ab961b32)
- Fix [#110](https://github.com/M-Zuber/MyHome/issues/110) [code](https://github.com/M-Zuber/MyHome/commit/87d674687c11bf88abaafc885d5abc02391d1b6c)
2 changes: 0 additions & 2 deletions FrameWork/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork
{
Expand Down
18 changes: 9 additions & 9 deletions FrameWork/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ public class Log
public Log(string strLogFileName)
{
// Intializes the instance of the log file
this.LogFile = new FileInfo(strLogFileName);
LogFile = new FileInfo(strLogFileName);

if (!this.LogFile.Directory.Exists)
if (!LogFile.Directory.Exists)
{
// Creates the directory of the log file if does not exist
Directory.CreateDirectory(this.LogFile.Directory.FullName);
Directory.CreateDirectory(LogFile.Directory.FullName);
}


if (!this.LogFile.Exists)
if (!LogFile.Exists)
{
// If the log file does not exist, creates the file -adding a creation timestamp to the top
using (StreamWriter stwrAppend = this.LogFile.AppendText())
using (StreamWriter stwrAppend = LogFile.AppendText())
{
stwrAppend.WriteLine("The file was created on: " + DateTime.Now.ToString());
}
Expand All @@ -55,7 +55,7 @@ public Log(string strLogFileName)
/// <param name="strMessage">The message to be logged</param>
public void AddMessage(string strMessage)
{
using (StreamWriter stwrAppend = this.LogFile.AppendText())
using (StreamWriter stwrAppend = LogFile.AppendText())
{
stwrAppend.WriteLine(strMessage);
}
Expand All @@ -67,7 +67,7 @@ public void AddMessage(string strMessage)
/// <param name="Messages">The messages to be logged</param>
public void AddMessages(params string[] Messages)
{
using (StreamWriter logWriter = this.LogFile.AppendText())
using (StreamWriter logWriter = LogFile.AppendText())
{
foreach (string message in Messages)
{
Expand All @@ -89,7 +89,7 @@ public void AddMessages(params string[] Messages)
/// <param name="dtErrorTime">The time of the error</param>
public void AddError(int nErrNo, string strMessage, DateTime dtErrorTime)
{
using (StreamWriter stwrAppend = this.LogFile.AppendText())
using (StreamWriter stwrAppend = LogFile.AppendText())
{
stwrAppend.WriteLine(nErrNo.ToString() + "\t" +
strMessage + "\t" +
Expand All @@ -105,7 +105,7 @@ public void AddError(int nErrNo, string strMessage, DateTime dtErrorTime)
/// <param name="dtErrorTime">The time of the error</param>
public void AddError(Globals.ErrorCodes enErrorNumber, string strMessage, DateTime dtErrorTime)
{
using (StreamWriter stwrAppend = this.LogFile.AppendText())
using (StreamWriter stwrAppend = LogFile.AppendText())
{
stwrAppend.WriteLine(((int)enErrorNumber).ToString() + "\t" +
strMessage + "\t" +
Expand Down
1 change: 0 additions & 1 deletion FrameWork/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
14 changes: 7 additions & 7 deletions FrameWork/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public class SettingsManager
public SettingsManager(string settingsFileName)
{
// Intializes the instance of the setting file
this.SettingsFile = new FileInfo(settingsFileName);
SettingsFile = new FileInfo(settingsFileName);

if (!this.SettingsFile.Directory.Exists)
if (!SettingsFile.Directory.Exists)
{
// Creates the directory of the setting file if does not exist
Directory.CreateDirectory(this.SettingsFile.Directory.FullName);
Directory.CreateDirectory(SettingsFile.Directory.FullName);
}

if (!this.SettingsFile.Exists)
if (!SettingsFile.Exists)
{
// If the file doesnt exist, creates it.
// No actual value is written in, this is just the simplest way to create
// the file without locking up the resource
using (StreamWriter stwrAppend = this.SettingsFile.AppendText()){}
using (StreamWriter stwrAppend = SettingsFile.AppendText()){}
}
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public Dictionary<string, string> GetAllSettings()
{
var allSettings = new Dictionary<string, string>();

using (StreamReader settingsReader = new StreamReader(this.SettingsFile.FullName))
using (StreamReader settingsReader = new StreamReader(SettingsFile.FullName))
{
while (!settingsReader.EndOfStream)
{
Expand All @@ -100,7 +100,7 @@ public void SaveSettings(Dictionary<string, string> allSettings)
{
// Opens the file and cleans it from all previous data
using (StreamWriter settingsWriter =
new StreamWriter(this.SettingsFile.Open(FileMode.Create)))
new StreamWriter(SettingsFile.Open(FileMode.Create)))
{
// Goes over each key:value in the settings dictionary
foreach (KeyValuePair<string, string> CurrSetting in allSettings)
Expand Down
17 changes: 13 additions & 4 deletions MyHome.DataClasses/Expense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public Expense(decimal amount, DateTime date, ExpenseCategory expenseCategory,
Method = paymentMethod;
}

public Expense(decimal amount, DateTime date, int categoryID, int methodID, string comments)
{
Amount = amount;
CategoryId = categoryID;
Comments = comments;
Date = date;
PaymentMethodId = methodID;
}

///// <summary>
///// The amount of the expense
///// </summary>
Expand All @@ -29,9 +38,9 @@ public Expense(decimal amount, DateTime date, ExpenseCategory expenseCategory,
///// </summary>
public new DateTime Date { get; set; }

public int CategoryId { get; set; }
public new int CategoryId { get; set; }

public int PaymentMethodId { get; set; }
public new int PaymentMethodId { get; set; }

/// <summary>
/// Category of the expense
Expand All @@ -58,11 +67,11 @@ public override bool Equals(object obj)
var expenseComparing = (Expense) obj;

return ((Amount == expenseComparing.Amount) &&
(Category.Equals(expenseComparing.Category)) &&
((Category == null && expenseComparing.Category == null) || Category.Equals(expenseComparing.Category)) &&
(Comments == expenseComparing.Comments) &&
(Date == expenseComparing.Date) &&
(Id == expenseComparing.Id) &&
(Method.Equals(expenseComparing.Method)));
((Method == null && expenseComparing.Method == null) || Method.Equals(expenseComparing.Method)));
}

public override int GetHashCode()
Expand Down
6 changes: 4 additions & 2 deletions MyHome.DataClasses/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static class Extensions
public static Expense Copy(this Expense orginalExpense)
{
return new Expense(orginalExpense.Amount, orginalExpense.Date,
orginalExpense.Category, orginalExpense.Method, orginalExpense.Comments, orginalExpense.Id);
orginalExpense.Category, orginalExpense.Method, orginalExpense.Comments, orginalExpense.Id)
{ CategoryId = orginalExpense.CategoryId, PaymentMethodId = orginalExpense.PaymentMethodId};
}

#endregion
Expand All @@ -30,7 +31,8 @@ public static Expense Copy(this Expense orginalExpense)
public static Income Copy(this Income orginalIncome)
{
return new Income(orginalIncome.Amount, orginalIncome.Date,
orginalIncome.Category, orginalIncome.Method, orginalIncome.Comments, orginalIncome.Id);
orginalIncome.Category, orginalIncome.Method, orginalIncome.Comments, orginalIncome.Id)
{ CategoryId = orginalIncome.CategoryId, PaymentMethodId = orginalIncome.PaymentMethodId};
}

#endregion
Expand Down
17 changes: 13 additions & 4 deletions MyHome.DataClasses/Income.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class Income : Transaction
/// </summary>
public new int Id { get; set; }

public int CategoryId { get; set; }
public int PaymentMethodId { get; set; }
public new int CategoryId { get; set; }
public new int PaymentMethodId { get; set; }

#endregion

Expand All @@ -58,6 +58,15 @@ public Income(decimal amount, DateTime date, IncomeCategory incomeCategory,
Method = paymentMethod;
}

public Income(decimal amount, DateTime date, int categoryID, int methodID, string comments)
{
Amount = amount;
CategoryId = categoryID;
Comments = comments;
Date = date;
PaymentMethodId = methodID;
}

#endregion

#region Override Methods
Expand All @@ -67,11 +76,11 @@ public override bool Equals(object obj)
Income incomeComparing = (Income)obj;

return ((Amount == incomeComparing.Amount) &&
(Category.Equals(incomeComparing.Category)) &&
((Category == null && incomeComparing.Category == null) || Category.Equals(incomeComparing.Category)) &&
(Comments == incomeComparing.Comments) &&
(Date == incomeComparing.Date) &&
(Id == incomeComparing.Id) &&
(Method.Equals(incomeComparing.Method)));
((Method == null && incomeComparing.Method == null) || Method.Equals(incomeComparing.Method)));
}

public override int GetHashCode()
Expand Down
1 change: 0 additions & 1 deletion MyHome.DataClasses/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
8 changes: 5 additions & 3 deletions MyHome.DataClasses/Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyHome.DataClasses
{
public class Transaction
{
public Category Category { get; set; }

public int CategoryId { get; set; }

public decimal Amount { get; set; }

public DateTime Date { get; set; }

public PaymentMethod Method { get; set; }

public int PaymentMethodId { get; set; }

public int Id { get; set; }

public string Comments { get; set; }
}
}
16 changes: 15 additions & 1 deletion MyHome.DataRepositories/ExpenseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IEnumerable<Expense> GetForMonthAndYear(int month, int year)

public void Remove(int id)
{
var expense =_context.Expenses.FirstOrDefault(e => e.Id == id);
var expense = _context.Expenses.FirstOrDefault(e => e.Id == id);
_context.Expenses.Remove(expense);
_context.SaveChanges();
}
Expand All @@ -59,6 +59,7 @@ public void Save(Expense expense)

public void Update(Expense expense)
{
CleanUpForEF(expense);
_context.Expenses.Attach(expense);
_context.SaveChanges();
}
Expand All @@ -69,8 +70,21 @@ public void Create(Expense expense)
{
return;
}
CleanUpForEF(expense);
_context.Expenses.Add(expense);
_context.SaveChanges();
}

private void CleanUpForEF(Expense expense)
{
if (expense.CategoryId > 0)
{
expense.Category = null;
}
if (expense.PaymentMethodId > 0)
{
expense.Method = null;
}
}
}
}
14 changes: 14 additions & 0 deletions MyHome.DataRepositories/IncomeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void Save(Income income)
public void Update(Income income)
{
//TODO -there should be tests that cover this, but what happens if the item is a new item?
CleanUpForEF(income);
_context.Incomes.Attach(income);
_context.SaveChanges();
}
Expand All @@ -71,8 +72,21 @@ public void Create(Income income)
{
return;
}
CleanUpForEF(income);
_context.Incomes.Add(income);
_context.SaveChanges();
}

private void CleanUpForEF(Income income)
{
if (income.CategoryId > 0)
{
income.Category = null;
}
if (income.PaymentMethodId > 0)
{
income.Method = null;
}
}
}
}
1 change: 0 additions & 1 deletion MyHome.DataRepositories/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
4 changes: 1 addition & 3 deletions MyHome.Persistence/AccountingDataContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity;
using MyHome.DataClasses;
using MyHome.Persistence.Configurations;

Expand Down
Loading

0 comments on commit 6d32e40

Please sign in to comment.