Example #1
0
        public static JournalTransaction FromDbEntity(SqlEntities.JournalTransaction sqlTransaction)
        {
            var db = new ApplicationDomainContext();

            var users       = db.Select <SqlEntities.User>().ToArray();
            var attachments = db.Select <SqlEntities.Attachment>().Where(a => a.TransactionId == sqlTransaction.Id);
            var journal     =
                Journal.FromDbEntity(db.Select <SqlEntities.Journal>().First(j => j.Id == sqlTransaction.JournalId));
            var createdBy  = User.FromDbEntity(users.First(u => u.Id == sqlTransaction.CreatedBy));
            var resolvedBy = sqlTransaction.ResolvedBy.HasValue ?
                             User.FromDbEntity(users.FirstOrDefault(u => u.Id == sqlTransaction.ResolvedBy.Value)) : null;

            var entries = db.Select <SqlEntities.JournalEntry>().Where(e => e.TransactionId == sqlTransaction.Id).Select(JournalEntry.FromDbEntity).ToList();

            return(new JournalTransaction
            {
                Id = sqlTransaction.Id,
                CreateDate = sqlTransaction.CreateDate,
                Description = sqlTransaction.Description,
                ResolveDate = sqlTransaction.ResolveDate,
                Journal = journal,
                Type = (TransactionType)sqlTransaction.Type,
                ResolvedBy = resolvedBy,
                CreatedBy = createdBy,
                Entries = entries,
                Status = (TransactionStatusType)sqlTransaction.Status,
                Attachments = attachments.Select(Attachment.FromDbEntity).ToList()
            });
        }
        public void AddTransaction([FromBody] JournalTransaction transaction)
        {
            var dbContext = GetDbContext();

            var sqlTransaction = new SqlEntities.JournalTransaction
            {
                CreatedBy   = transaction.CreatedBy.Id,
                CreateDate  = DateTime.Now,
                Description = transaction.Description,
                Type        = (int)transaction.Type,
                Status      = (int)transaction.Status
            };

            int transactionId = dbContext.Insert(sqlTransaction);

            if (transaction.Attachments != null)
            {
                var sqlAttachments = transaction.Attachments.Select(a => new SqlEntities.Attachment
                {
                    TransactionId = transactionId,
                    Name          = a.Name,
                    Path          = a.Path
                });

                foreach (var attachment in sqlAttachments)
                {
                    dbContext.Insert(attachment);
                }
            }



            var sqlEntries = transaction.Entries.Select(entry => new SqlEntities.JournalEntry
            {
                Amount        = entry.Amount,
                TransactionId = transactionId,
                Side          = (int)entry.Side,
                AccountId     = entry.Account.Id
            });

            foreach (var entry in sqlEntries)
            {
                dbContext.Insert(entry);
            }
        }