Exemplo n.º 1
0
        public OTransaction saveTransaction(OTransaction transaction)
        {
            // Validations
            if (transaction == null)
            {
                throw new Exception("ERROR: Transaction null");
            }


            // Get model and modify properties
            Transaction model;
            Transaction modelBeforeEdited = null;

            if (transaction.id.HasValue)
            {
                model             = getByIdEdition(transaction.id.Value);
                modelBeforeEdited = model;
            }
            else
            {
                model = new Transaction();
            }
            model.tAmount      = transaction.amount;
            model.tCategoryId  = transaction.category.id.Value;
            model.tDate        = DateTime.ParseExact(transaction.date, "d'/'M'/'yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
            model.tDescription = transaction.description;
            model.tUserId      = transaction.userId;
            model.tVaultId     = transaction.vaultId;

            // Update or Add if is a new one
            if (transaction.id.HasValue)
            {
                context.Transactions.Update(model);
            }
            else
            {
                context.Transactions.Add(model);
            }
            context.SaveChanges();
            context.Entry(model).Reference(t => t.CategoryNavigation)
            .Query()
            .Include(c => c.categoryTypeNavigation).Load();

            // Update vault if necessary
            if (transaction.vaultId.HasValue)
            {
                VaultManager vaultManager = new VaultManager(context);
                vaultManager.updateVaultAmount(model, modelBeforeEdited);
            }

            return(convert(model));
        }
Exemplo n.º 2
0
        public bool delete(int transactionId)
        {
            bool deleted = false;

            Transaction transaction = getByIdEdition(transactionId);

            int cagegoryTypeId = transaction.CategoryNavigation.categoryTypeNavigation.ctId.Value;

            if (cagegoryTypeId == (int)CategoryTypesId.VaultsIncomes || cagegoryTypeId == (int)CategoryTypesId.VaultsExpenses)
            {
                VaultManager vaultManager = new VaultManager(context);
                vaultManager.updateVaultAmount(transaction, null, true);
            }

            context.Transactions.Remove(transaction);
            context.SaveChanges();

            deleted = true;
            return(deleted);
        }