diff --git a/MonoStockPortfolio.Core/PortfolioRepositories/AndroidSqlitePortfolioRepository.cs b/MonoStockPortfolio.Core/PortfolioRepositories/AndroidSqlitePortfolioRepository.cs index 0c1fd84..ee22a09 100644 --- a/MonoStockPortfolio.Core/PortfolioRepositories/AndroidSqlitePortfolioRepository.cs +++ b/MonoStockPortfolio.Core/PortfolioRepositories/AndroidSqlitePortfolioRepository.cs @@ -56,6 +56,11 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories return null; } + public void SavePosition(Position position) + { + _db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)); + } + public void DeletePortfolio(Portfolio portfolio) { @@ -112,6 +117,16 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories return contentValues; } + private ContentValues GetPositionContentValues(Position position) + { + var positionValues = new ContentValues(); + positionValues.Put("PricePerShare", (double)position.PricePerShare); + positionValues.Put("Ticker", position.Ticker); + positionValues.Put("Shares", (double)position.Shares); + positionValues.Put("ContainingPortfolioID", position.ContainingPortfolioID); + return positionValues; + } + private class OpenHelper : SQLiteOpenHelper { public OpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version) diff --git a/MonoStockPortfolio.Core/PortfolioRepositories/IPortfolioRepository.cs b/MonoStockPortfolio.Core/PortfolioRepositories/IPortfolioRepository.cs index 249c732..e04467e 100644 --- a/MonoStockPortfolio.Core/PortfolioRepositories/IPortfolioRepository.cs +++ b/MonoStockPortfolio.Core/PortfolioRepositories/IPortfolioRepository.cs @@ -10,5 +10,6 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories void DeletePortfolio(Entities.Portfolio portfolio); IList GetAllPositions(long portfolioId); Portfolio GetPortfolioById(long portfolioId); + void SavePosition(Position position); } } \ No newline at end of file diff --git a/MonoStockPortfolio.Core/Services/IPortfolioService.cs b/MonoStockPortfolio.Core/Services/IPortfolioService.cs index 8d9db0d..571f0bf 100644 --- a/MonoStockPortfolio.Core/Services/IPortfolioService.cs +++ b/MonoStockPortfolio.Core/Services/IPortfolioService.cs @@ -6,7 +6,6 @@ namespace MonoStockPortfolio.Core.Services public interface IPortfolioService { IList GetAllPortfolios(); - IEnumerable> GetDetailedItems(Portfolio portfolio, IEnumerable items); IEnumerable> GetDetailedItems(long portfolioID, IEnumerable items); Portfolio GetPortolioById(long portfolioId); } diff --git a/MonoStockPortfolio.Core/Services/PortfolioService.cs b/MonoStockPortfolio.Core/Services/PortfolioService.cs index 67b5465..1a3a9fd 100644 --- a/MonoStockPortfolio.Core/Services/PortfolioService.cs +++ b/MonoStockPortfolio.Core/Services/PortfolioService.cs @@ -32,35 +32,20 @@ namespace MonoStockPortfolio.Core.Services public IEnumerable> GetDetailedItems(long portfolioID, IEnumerable items) { - IDictionary dict = new Dictionary(); - dict.Add(StockDataItem.Ticker, "DENN"); - dict.Add(StockDataItem.LastTradePrice, "4.99"); - dict.Add(StockDataItem.Time, "3:59pm"); + var positions = _portRepo.GetAllPositions(portfolioID); + var tickers = positions.Select(p => p.Ticker); + var stockData = _stockRepo.GetStockQuotes(tickers); - IDictionary dict2 = new Dictionary(); - dict2.Add(StockDataItem.Ticker, "XIN"); - dict2.Add(StockDataItem.LastTradePrice, "3.02"); - dict2.Add(StockDataItem.Time, "4:00pm"); - - var list = new List>(); - list.Add(dict); - list.Add(dict2); - return list; - -// var positions = _portRepo.GetAllPositions(portfolioID); -// var tickers = positions.Select(p => p.Ticker); -// var stockData = _stockRepo.GetStockQuotes(tickers); -// -// foreach (var position in positions) -// { -// var ticker = position.Ticker; -// var tickerStockData = stockData.Single(stock => stock.Ticker == ticker); -// var stockItems = GetStockItems(items, tickerStockData); -// var remainingItemsToGet = items.Except(stockItems.Keys); -// stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData)); -// -// yield return stockItems; -// } + foreach (var position in positions) + { + var ticker = position.Ticker; + var tickerStockData = stockData.Single(stock => stock.Ticker == ticker); + var stockItems = GetStockItems(items, tickerStockData); + var remainingItemsToGet = items.Except(stockItems.Keys); + stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData)); + + yield return stockItems; + } } public Portfolio GetPortolioById(long portfolioId) @@ -68,24 +53,6 @@ namespace MonoStockPortfolio.Core.Services return _portRepo.GetPortfolioById(portfolioId); } - public IEnumerable> GetDetailedItems(Portfolio portfolio, IEnumerable items) - { -// var tickers = portfolio.Positions.Select(p => p.Ticker); -// var stockData = _stockRepo.GetStockQuotes(tickers); -// -// foreach (var position in portfolio.Positions) -// { -// var ticker = position.Ticker; -// var tickerStockData = stockData.Single(stock => stock.Ticker == ticker); -// var stockItems = GetStockItems(items, tickerStockData); -// var remainingItemsToGet = items.Except(stockItems.Keys); -// stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData)); -// -// yield return stockItems; -// } - throw new NotImplementedException(); - } - private IDictionary GetStockItems(IEnumerable items, StockQuote quote) { var dict = new Dictionary(); diff --git a/MonoStockPortfolio/AddPositionActivity.cs b/MonoStockPortfolio/AddPositionActivity.cs index d94a203..0eb442e 100644 --- a/MonoStockPortfolio/AddPositionActivity.cs +++ b/MonoStockPortfolio/AddPositionActivity.cs @@ -1,7 +1,10 @@ using System; using Android.App; +using Android.Content; using Android.OS; using Android.Widget; +using MonoStockPortfolio.Core.PortfolioRepositories; +using MonoStockPortfolio.Core.Services; using MonoStockPortfolio.Entities; namespace MonoStockPortfolio @@ -12,6 +15,7 @@ namespace MonoStockPortfolio public AddPositionActivity(IntPtr handle) : base(handle) { } public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } } + private IPortfolioRepository _repo; protected override void OnCreate(Bundle bundle) { @@ -28,9 +32,12 @@ namespace MonoStockPortfolio var position = new Position(); if(Validate(position)) { - // save it - // go back - Toast.MakeText(this, "Saved!", ToastLength.Long).Show(); + _repo = new AndroidSqlitePortfolioRepository(this); + _repo.SavePosition(position); + + var intent = new Intent(); + SetResult(Result.Ok, intent); + Finish(); } } diff --git a/MonoStockPortfolio/PortfolioActivity.cs b/MonoStockPortfolio/PortfolioActivity.cs index c9218ab..f03f0e7 100644 --- a/MonoStockPortfolio/PortfolioActivity.cs +++ b/MonoStockPortfolio/PortfolioActivity.cs @@ -14,7 +14,10 @@ namespace MonoStockPortfolio [Activity(Label = "Portfolio")] public class PortfolioActivity : Activity { - public PortfolioActivity(IntPtr handle) : base(handle) { } + public PortfolioActivity(IntPtr handle) : base(handle) + { + _svc = new PortfolioService(this); + } public static string ClassName { get { return "monoStockPortfolio.PortfolioActivity"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } } @@ -32,18 +35,20 @@ namespace MonoStockPortfolio var addPositionButton = FindViewById