Example #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            LogInfo(_isEditMode ? "Updating transaction..." : "Adding transaction...");

            string errorMessage = null;

            var transaction = new Transaction {
                ID = _isEditMode ? _transaction.ID : 0,
                Description = txtDescription.Text.Trim(),
                Amount = nudAmount.Value,
                Date = dtpDate.Value,
                Tags = cboTags.Text.Trim().Split(' ')
            };

            try {
                bool success = _isEditMode
                    ? _repo.EditTransaction(transaction, out errorMessage)
                    : _repo.InsertTransaction(transaction, out errorMessage);

                string parameters = String.Format("'{0}', {1:N}, {2:yyyy-MM-dd}, '{3}'", transaction.Description, transaction.Amount, transaction.Date, String.Join(" ", transaction.Tags));
                LogInfo(success
                    ? String.Format("Transaction successfully {1} ({0}).", parameters, _isEditMode ? "updated" : "added")
                    : String.Format("Transaction failed ({0}). Error message: {1}", parameters, errorMessage));

                SetControls();
            }
            catch (Exception ex) {
                LogInfo("An unexpected error ocurred. Exception Message: " + ex.Message);
            }
        }
Example #2
0
 public TransactionForm(Transaction transaction)
 {
     InitializeComponent();
     _repo = new TransactionRepository();
     _transaction = transaction;
     _isEditMode = transaction != null;
 }
Example #3
0
        private void EditTransaction(ListViewItem item)
        {
            var transaction = new Transaction {
                ID = long.Parse(item.SubItems[0].Text),
                Description = item.SubItems[1].Text,
                Amount = decimal.Parse(item.SubItems[2].Text),
                Date = DateTime.Parse(item.SubItems[3].Text),
                Tags = item.SubItems[4].Text.Split(' ')
            };

            var editForm = new TransactionForm(transaction);
            editForm.ShowDialog();
        }
Example #4
0
        private void removeTransactionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LogInfo("Deleting transaction...");
            ListViewItem item = lstvTransactions.SelectedItems[0];

            var transaction = new Transaction {
                ID = long.Parse(item.SubItems[0].Text),
                Description = item.SubItems[1].Text,
                Amount = decimal.Parse(item.SubItems[2].Text),
                Date = DateTime.Parse(item.SubItems[3].Text),
                Tags = item.SubItems[4].Text.Split(' ')
            };

            string errorMessage = null;

            try {
                bool success = _repo.DeleteTransaction(transaction.ID, out errorMessage);

                string parameters = String.Format("'{0}', {1:N}, {2:yyyy-MM-dd}, '{3}'", transaction.Description, transaction.Amount, transaction.Date, transaction.TagsJoined);
                LogInfo(success
                        ? String.Format("Transaction successfully deleted ({0}).", parameters)
                        : String.Format("Transaction failed ({0}). Error message: {1}", parameters, errorMessage));

                LoadAndFillTransactionsList();
            }
            catch (Exception ex) {
                LogInfo("An unexpected error ocurred. Exception Message: " + ex.Message);
            }
        }
        public bool InsertTransaction(Transaction transaction, out string errorMessage)
        {
            string url = String.Format("http://www.moneytrackin.com/api/rest/insertTransaction?project=&description={0}&amount={1}&date={2:yyyy-MM-dd}&tags={3}",
                transaction.Description, transaction.Amount, transaction.Date, string.Join(" ", transaction.Tags));

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.Headers.Add("Authorization", String.Format("Basic {0}", ConfigurationManager.AppSettings["ApiAuthorizationKey"]));

            bool success = false;
            errorMessage = null;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
                string xmlResponse;

                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    xmlResponse = reader.ReadToEnd();
                }

                var doc = new XmlDocument();
                doc.LoadXml(xmlResponse);

                var xdoc = doc.ToXDocument();
                var code = (string)xdoc.Root.Attribute("code");

                var query = from c in xdoc.Descendants("error")
                            select new {
                                ErrorMessage = (string)c.Value
                            };

                success = code.ToLower() == "done";
                errorMessage = query.FirstOrDefault() != null ? query.FirstOrDefault().ErrorMessage : "Undefined error.";
            }

            return success;
        }
 public Transaction GetTransaction(Transaction transaction)
 {
     var transactions = GetTransactions(from: transaction.Date, to: transaction.Date);
     return transactions != null
         ? transactions.Where(t => t.ID == transaction.ID).FirstOrDefault()
         : null;
 }