mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-11-15 11:19:26 +00:00
implemented edit/update functionality
This commit is contained in:
parent
b8aaf85604
commit
60024d86f9
9 changed files with 161 additions and 85 deletions
|
@ -55,7 +55,14 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
|
||||
public void SavePosition(Position position)
|
||||
{
|
||||
_db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position));
|
||||
if (position.ID == null)
|
||||
{
|
||||
InsertNewPosition(position);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateExistingPosition(position);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeletePortfolioById(int portfolioId)
|
||||
|
@ -96,6 +103,25 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
_db.Delete(POSITION_TABLE_NAME, "id = " + positionId, null);
|
||||
}
|
||||
|
||||
public Position GetPositionById(long positionId)
|
||||
{
|
||||
Position position = null;
|
||||
|
||||
var cursor = _db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " id = " + positionId, null, null, null, null);
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
while (cursor.MoveToNext())
|
||||
{
|
||||
position= new Position(cursor.GetInt(0));
|
||||
position.Ticker = cursor.GetString(1);
|
||||
position.Shares = Convert.ToDecimal(cursor.GetFloat(2));
|
||||
position.PricePerShare = Convert.ToDecimal(cursor.GetFloat(3));
|
||||
}
|
||||
}
|
||||
if (!cursor.IsClosed) cursor.Close();
|
||||
return position;
|
||||
}
|
||||
|
||||
public IList<Position> GetAllPositions(long portfolioId)
|
||||
{
|
||||
var list = new List<Position>();
|
||||
|
@ -139,6 +165,17 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
Log.E("InsertNewPortfolio", "Portfolios inserted: " + _db.Insert(PORTFOLIO_TABLE_NAME, null, GetPortfolioContentValues(portfolio)));
|
||||
}
|
||||
|
||||
private void UpdateExistingPosition(Position position)
|
||||
{
|
||||
var positionID = position.ID ?? -1;
|
||||
Log.E("UpdateExistingPosition", "Positions updated: " + _db.Update(POSITION_TABLE_NAME, GetPositionContentValues(position), "id = " + positionID, null));
|
||||
}
|
||||
|
||||
private void InsertNewPosition(Position position)
|
||||
{
|
||||
Log.E("InsertNewPosition", "Positions inserted: " + _db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)));
|
||||
}
|
||||
|
||||
private static ContentValues GetPortfolioContentValues(Portfolio portfolio)
|
||||
{
|
||||
var contentValues = new ContentValues();
|
||||
|
|
|
@ -13,5 +13,6 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
void SavePosition(Position position);
|
||||
Portfolio GetPortfolioByName(string portfolioName);
|
||||
void DeletePositionById(long positionId);
|
||||
Position GetPositionById(long positionId);
|
||||
}
|
||||
}
|
|
@ -48,7 +48,7 @@ namespace MonoStockPortfolio.Core.Services
|
|||
var remainingItemsToGet = items.Except(stockItems.Keys);
|
||||
stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData));
|
||||
|
||||
var positionResults = new PositionResultsViewModel(position.ID);
|
||||
var positionResults = new PositionResultsViewModel(position.ID ?? -1);
|
||||
positionResults.Items = stockItems;
|
||||
|
||||
results.Add(positionResults);
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace MonoStockPortfolio.Entities
|
|||
public Position() { }
|
||||
public Position(long id) { ID = id; }
|
||||
|
||||
public long ID { get; private set; }
|
||||
public long? ID { get; private set; }
|
||||
public string Ticker { get; set; }
|
||||
public decimal Shares { get; set; }
|
||||
public decimal PricePerShare { get; set; }
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
using System;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Widget;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Entities;
|
||||
using MonoStockPortfolio.Framework;
|
||||
|
||||
namespace MonoStockPortfolio.Activites
|
||||
{
|
||||
[Activity(Label = "Add Position", MainLauncher = false)]
|
||||
public partial class AddPositionActivity : Activity
|
||||
{
|
||||
[IoC] private IPortfolioRepository _repo;
|
||||
|
||||
protected override void OnCreate(Bundle bundle)
|
||||
{
|
||||
base.OnCreate(bundle);
|
||||
|
||||
SetContentView(Resource.layout.addposition);
|
||||
|
||||
SaveButton.Click += saveButton_Click;
|
||||
}
|
||||
|
||||
void saveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
var position = new Position();
|
||||
if(Validate(position))
|
||||
{
|
||||
_repo.SavePosition(position);
|
||||
|
||||
var intent = new Intent();
|
||||
SetResult(Result.Ok, intent);
|
||||
Finish();
|
||||
}
|
||||
}
|
||||
|
||||
private bool Validate(Position position)
|
||||
{
|
||||
var result = ValidationRules.Apply();
|
||||
|
||||
if (result == string.Empty)
|
||||
{
|
||||
position.Shares = decimal.Parse(SharesTextBox.Text.ToString());
|
||||
position.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString());
|
||||
position.Ticker = TickerTextBox.Text.ToString();
|
||||
position.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
||||
return true;
|
||||
}
|
||||
|
||||
Toast.MakeText(this, result, ToastLength.Long).Show();
|
||||
return false;
|
||||
}
|
||||
|
||||
private FormValidator ValidationRules
|
||||
{
|
||||
get
|
||||
{
|
||||
var validator = new FormValidator();
|
||||
validator.AddRequired(TickerTextBox, "Please enter a ticker");
|
||||
validator.AddValidPositiveDecimal(SharesTextBox, "Please enter a valid, positive number of shares");
|
||||
validator.AddValidPositiveDecimal(PriceTextBox, "Please enter a valid, positive price per share");
|
||||
return validator;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
104
MonoStockPortfolio/Activites/EditPositionActivity.cs
Normal file
104
MonoStockPortfolio/Activites/EditPositionActivity.cs
Normal file
|
@ -0,0 +1,104 @@
|
|||
using System;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Widget;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Entities;
|
||||
using MonoStockPortfolio.Framework;
|
||||
|
||||
namespace MonoStockPortfolio.Activites
|
||||
{
|
||||
[Activity(Label = "Add Position", MainLauncher = false)]
|
||||
public partial class EditPositionActivity : Activity
|
||||
{
|
||||
[IoC] private IPortfolioRepository _repo;
|
||||
|
||||
protected override void OnCreate(Bundle bundle)
|
||||
{
|
||||
base.OnCreate(bundle);
|
||||
|
||||
SetContentView(Resource.layout.addposition);
|
||||
|
||||
var positionId = Intent.GetLongExtra(Extra_PositionID, -1);
|
||||
if (positionId != -1)
|
||||
{
|
||||
this.Title = "Edit Position";
|
||||
PopulateForm(positionId);
|
||||
}
|
||||
|
||||
WireUpEvents();
|
||||
}
|
||||
|
||||
private void PopulateForm(long positionId)
|
||||
{
|
||||
var position = _repo.GetPositionById(positionId);
|
||||
this.TickerTextBox.Text = position.Ticker;
|
||||
this.PriceTextBox.Text = position.PricePerShare.ToString();
|
||||
this.SharesTextBox.Text = position.Shares.ToString();
|
||||
}
|
||||
|
||||
private void WireUpEvents()
|
||||
{
|
||||
SaveButton.Click += saveButton_Click;
|
||||
}
|
||||
|
||||
void saveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(Validate())
|
||||
{
|
||||
var positionToSave = GetPositionToSave();
|
||||
_repo.SavePosition(positionToSave);
|
||||
|
||||
var intent = new Intent();
|
||||
SetResult(Result.Ok, intent);
|
||||
Finish();
|
||||
}
|
||||
}
|
||||
|
||||
private bool Validate()
|
||||
{
|
||||
var result = ValidationRules.Apply();
|
||||
|
||||
if (result == string.Empty)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
Toast.MakeText(this, result, ToastLength.Long).Show();
|
||||
return false;
|
||||
}
|
||||
|
||||
private FormValidator ValidationRules
|
||||
{
|
||||
get
|
||||
{
|
||||
var validator = new FormValidator();
|
||||
validator.AddRequired(TickerTextBox, "Please enter a ticker");
|
||||
validator.AddValidPositiveDecimal(SharesTextBox, "Please enter a valid, positive number of shares");
|
||||
validator.AddValidPositiveDecimal(PriceTextBox, "Please enter a valid, positive price per share");
|
||||
return validator;
|
||||
}
|
||||
}
|
||||
|
||||
private Position GetPositionToSave()
|
||||
{
|
||||
Position positionToSave;
|
||||
var positionId = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
||||
if (positionId != -1)
|
||||
{
|
||||
positionToSave = new Position(positionId);
|
||||
}
|
||||
else
|
||||
{
|
||||
positionToSave = new Position();
|
||||
}
|
||||
|
||||
positionToSave.Shares = decimal.Parse(SharesTextBox.Text.ToString());
|
||||
positionToSave.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString());
|
||||
positionToSave.Ticker = TickerTextBox.Text.ToString();
|
||||
positionToSave.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
||||
return positionToSave;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,10 +2,11 @@ using Android.Widget;
|
|||
|
||||
namespace MonoStockPortfolio.Activites
|
||||
{
|
||||
public partial class AddPositionActivity
|
||||
public partial class EditPositionActivity
|
||||
{
|
||||
public static string ClassName { get { return "monostockportfolio.activites.AddPositionActivity"; } }
|
||||
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
|
||||
public static string ClassName { get { return "monostockportfolio.activites.EditPositionActivity"; } }
|
||||
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPositionActivity.PortfolioID"; } }
|
||||
public static string Extra_PositionID { get { return "monoStockPortfolio.EditPositionActivity.PositionID"; } }
|
||||
|
||||
protected EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
|
||||
protected EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }
|
|
@ -70,13 +70,14 @@ namespace MonoStockPortfolio.Activites
|
|||
if (item.Title.ToS() == "Edit")
|
||||
{
|
||||
// Edit
|
||||
// var intent = new Intent();
|
||||
// intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
||||
// intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, (long)item.ItemId);
|
||||
// StartActivityForResult(intent, 0);
|
||||
// return true;
|
||||
var intent = new Intent();
|
||||
intent.SetClassName(this, EditPositionActivity.ClassName);
|
||||
intent.PutExtra(EditPositionActivity.Extra_PositionID, (long)item.ItemId);
|
||||
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
|
||||
StartActivityForResult(intent, 0);
|
||||
return true;
|
||||
}
|
||||
else if (item.Title.ToS() == "Delete")
|
||||
if (item.Title.ToS() == "Delete")
|
||||
{
|
||||
// Delete
|
||||
_repo.DeletePositionById(item.ItemId);
|
||||
|
@ -195,8 +196,8 @@ namespace MonoStockPortfolio.Activites
|
|||
void addPositionButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
var intent = new Intent();
|
||||
intent.SetClassName(this, AddPositionActivity.ClassName);
|
||||
intent.PutExtra(AddPositionActivity.Extra_PortfolioID, ThisPortofolioId);
|
||||
intent.SetClassName(this, EditPositionActivity.ClassName);
|
||||
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
|
||||
StartActivityForResult(intent, 0);
|
||||
}
|
||||
|
||||
|
|
|
@ -48,8 +48,8 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Activites\AddPositionActivity.designer.cs">
|
||||
<DependentUpon>AddPositionActivity.cs</DependentUpon>
|
||||
<Compile Include="Activites\EditPositionActivity.designer.cs">
|
||||
<DependentUpon>EditPositionActivity.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Activites\EditPortfolioActivity.cs" />
|
||||
<Compile Include="Activites\EditPortfolioActivity.designer.cs">
|
||||
|
@ -63,7 +63,7 @@
|
|||
</Compile>
|
||||
<Compile Include="Activites\PortfolioActivity.cs" />
|
||||
<Compile Include="Activites\MainActivity.cs" />
|
||||
<Compile Include="Activites\AddPositionActivity.cs" />
|
||||
<Compile Include="Activites\EditPositionActivity.cs" />
|
||||
<Compile Include="Activites\PortfolioActivity.designer.cs">
|
||||
<DependentUpon>PortfolioActivity.cs</DependentUpon>
|
||||
</Compile>
|
||||
|
|
Loading…
Reference in a new issue