コード例 #1
0
        public HttpResponseMessage Update([FromBody] Models.Transaction transaction)
        {
            try
            {
                if (transaction == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, ""));
                }

                using (var context = new EMContext())
                {
                    context.Configuration.LazyLoadingEnabled = false;

                    DB.Models.Transaction     existingTransaction = context.Transactions.Include(x => x.FromTransaction).Include(x => x.TransactionType).Where(x => x.TransactionID == transaction.TransactionID).FirstOrDefault();
                    DB.Models.TransactionType transactionType     = context.TransactionTypes.Where(x => x.TransactionTypeID == transaction.TransactionTypeID).FirstOrDefault();

                    if (existingTransaction != null && transactionType != null)
                    {
                        DB.Models.Transaction foundTransaction   = new DB.Models.Transaction();
                        Models.Transaction    createdTransaction = new Models.Transaction();

                        switch (transactionType.TransactionTypeName)
                        {
                        case "Income":
                            existingTransaction.TransactionTypeID = transaction.TransactionTypeID;
                            existingTransaction.FromAccountID     = transaction.FromAccountID;
                            existingTransaction.CategoryID        = transaction.CategoryID;
                            existingTransaction.Operator          = true;
                            existingTransaction.TransactionDate   = transaction.TransactionDate;
                            existingTransaction.TransactionTime   = transaction.TransactionTime;
                            existingTransaction.Amount            = transaction.Amount;
                            existingTransaction.Note        = transaction.Note;
                            existingTransaction.CreatedDate = DateTime.Now;

                            if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty)
                            {
                                existingTransaction.SubCategoryID = transaction.SubCategoryID;
                            }
                            else
                            {
                                existingTransaction.SubCategoryID = null;
                            }

                            context.SaveChanges();

                            foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault();

                            context.Entry(foundTransaction).Reference(x => x.FromAccount).Load();
                            context.Entry(foundTransaction).Reference(x => x.Category).Load();

                            if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty)
                            {
                                context.Entry(foundTransaction).Reference(x => x.SubCategory).Load();
                            }

                            createdTransaction = new Models.Transaction
                            {
                                TransactionID     = foundTransaction.TransactionID,
                                TransactionTypeID = foundTransaction.TransactionTypeID,
                                FromAccountID     = foundTransaction.FromAccountID,
                                CategoryID        = foundTransaction.CategoryID,
                                SubCategoryID     = foundTransaction.SubCategoryID,
                                Operator          = foundTransaction.Operator,
                                TransactionDate   = foundTransaction.TransactionDate,
                                TransactionTime   = foundTransaction.TransactionTime,
                                Amount            = foundTransaction.Amount,
                                Note        = foundTransaction.Note,
                                CreatedDate = foundTransaction.CreatedDate
                            };
                            break;

                        case "Expense":
                            existingTransaction.TransactionTypeID = transaction.TransactionTypeID;
                            existingTransaction.FromAccountID     = transaction.FromAccountID;
                            existingTransaction.CategoryID        = transaction.CategoryID;
                            existingTransaction.Operator          = false;
                            existingTransaction.TransactionDate   = transaction.TransactionDate;
                            existingTransaction.TransactionTime   = transaction.TransactionTime;
                            existingTransaction.Amount            = transaction.Amount;
                            existingTransaction.Note        = transaction.Note;
                            existingTransaction.CreatedDate = DateTime.Now;

                            if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty)
                            {
                                existingTransaction.SubCategoryID = transaction.SubCategoryID;
                            }
                            else
                            {
                                existingTransaction.SubCategoryID = null;
                            }

                            context.SaveChanges();

                            foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault();

                            context.Entry(foundTransaction).Reference(x => x.FromAccount).Load();
                            context.Entry(foundTransaction).Reference(x => x.Category).Load();

                            if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty)
                            {
                                context.Entry(foundTransaction).Reference(x => x.SubCategory).Load();
                            }

                            createdTransaction = new Models.Transaction
                            {
                                TransactionID     = foundTransaction.TransactionID,
                                TransactionTypeID = foundTransaction.TransactionTypeID,
                                FromAccountID     = foundTransaction.FromAccountID,
                                CategoryID        = foundTransaction.CategoryID,
                                SubCategoryID     = foundTransaction.SubCategoryID,
                                Operator          = foundTransaction.Operator,
                                TransactionDate   = foundTransaction.TransactionDate,
                                TransactionTime   = foundTransaction.TransactionTime,
                                Amount            = foundTransaction.Amount,
                                Note        = foundTransaction.Note,
                                CreatedDate = foundTransaction.CreatedDate
                            };
                            break;

                        case "Transfer":
                            existingTransaction.TransactionTypeID = transaction.TransactionTypeID;
                            existingTransaction.FromAccountID     = transaction.FromAccountID;
                            existingTransaction.ToAccountID       = transaction.ToAccountID;
                            existingTransaction.Operator          = false;
                            existingTransaction.TransactionDate   = transaction.TransactionDate;
                            existingTransaction.TransactionTime   = transaction.TransactionTime;
                            existingTransaction.Amount            = transaction.Amount;
                            existingTransaction.Note        = transaction.Note;
                            existingTransaction.CreatedDate = DateTime.Now;

                            context.SaveChanges();

                            DB.Models.Transaction existingToTransaction = context.Transactions.Where(x => x.FromTransactionID == existingTransaction.TransactionID).FirstOrDefault();

                            if (existingToTransaction != null)
                            {
                                existingToTransaction.TransactionTypeID = transaction.TransactionTypeID;
                                existingToTransaction.FromAccountID     = transaction.ToAccountID ?? Guid.Empty;
                                existingToTransaction.ToAccountID       = transaction.FromAccountID;
                                existingToTransaction.Operator          = true;
                                existingToTransaction.TransactionDate   = transaction.TransactionDate;
                                existingToTransaction.TransactionTime   = transaction.TransactionTime;
                                existingToTransaction.Amount            = transaction.Amount;
                                existingToTransaction.Note        = transaction.Note;
                                existingToTransaction.CreatedDate = DateTime.Now;

                                context.SaveChanges();
                            }

                            foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault();

                            context.Entry(foundTransaction).Reference(x => x.FromAccount).Load();
                            context.Entry(foundTransaction).Reference(x => x.ToAccount).Load();

                            createdTransaction = new Models.Transaction
                            {
                                TransactionID     = foundTransaction.TransactionID,
                                TransactionTypeID = foundTransaction.TransactionTypeID,
                                FromAccountID     = foundTransaction.FromAccountID,
                                ToAccountID       = foundTransaction.ToAccountID,
                                Operator          = foundTransaction.Operator,
                                TransactionDate   = foundTransaction.TransactionDate,
                                TransactionTime   = foundTransaction.TransactionTime,
                                Amount            = foundTransaction.Amount,
                                Note        = foundTransaction.Note,
                                CreatedDate = foundTransaction.CreatedDate
                            };
                            break;

                        default:
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, ""));
                        }

                        APIResponse <Models.Transaction> apiResponse = new APIResponse <Models.Transaction>()
                        {
                            StatusCode   = (int)(createdTransaction == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK),
                            StatusRemark = "",
                            Content      = (createdTransaction ?? null),
                        };

                        return(Request.CreateResponse((createdTransaction == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), apiResponse));
                    }

                    return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, ""));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

                if (HttpContext.Current.IsDebuggingEnabled)
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, ""));
                }
            }
        }