public async Task <ActionResult <bool> > CreateAsync([FromBody] TransferVM value) { if (value == null) { return(this.BadRequest(this.ModelState)); } return(await this.GetService <TransfersService>().CreateAsync(value)); }
public async Task <ActionResult <TransferVM> > UpdateAsync(string id, [FromBody] TransferVM value) { if (value == null) { return(this.BadRequest(this.ModelState)); } return(await this.GetService <TransfersService>().UpdateAsync(id, value)); }
internal async Task <ActionResult <TransferVM> > GetDataAsync(string transferID) { try { // LOAD ENTRIES var user = this.GetService <Helpers.User>(); var entries = await this.GetDataQuery() .Where(x => x.TransferID == transferID) .ToListAsync(); if (entries == null || entries.Count != 2) { return(this.WarningResponse("TRANSFERS_RECORD_NOT_FOUND_WARNING")); } // INIT VIEW MODEL var viewModel = new TransferVM { TransferID = transferID, TransferDate = entries[0].DueDate, TransferValue = Math.Abs(entries[0].EntryValue) }; // EXPENSE var expenseEntry = entries.Where(x => x.Type == (short)Categories.enCategoryType.Expense).FirstOrDefault(); if (expenseEntry == null) { return(this.WarningResponse("TRANSFERS_RECORD_NOT_FOUND_WARNING")); } viewModel.ExpenseEntryID = expenseEntry.EntryID; if (expenseEntry.AccountID.HasValue) { var accountMessage = await this.GetService <Accounts.AccountsService>().GetDataAsync(expenseEntry.AccountID.Value); viewModel.ExpenseAccountRow = this.GetValue(accountMessage); viewModel.ExpenseAccountID = expenseEntry.AccountID.Value; } // INCOME var incomeEntry = entries.Where(x => x.Type == (short)Categories.enCategoryType.Income).FirstOrDefault(); if (incomeEntry == null) { return(this.WarningResponse("TRANSFERS_RECORD_NOT_FOUND_WARNING")); } viewModel.IncomeEntryID = incomeEntry.EntryID; viewModel.IncomeAccountID = incomeEntry.AccountID.Value; if (expenseEntry.AccountID.HasValue) { var accountMessage = await this.GetService <Accounts.AccountsService>().GetDataAsync(incomeEntry.AccountID.Value); viewModel.IncomeAccountRow = this.GetValue(accountMessage); viewModel.IncomeAccountID = incomeEntry.AccountID.Value; } return(this.OkResponse(viewModel)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }
internal async Task <ActionResult <bool> > CreateAsync(TransferVM value) { try { var entriesService = this.GetService <Entries.EntriesService>(); var transferID = Guid.NewGuid().ToString("N"); // VALIDATE // var validateMessage = await this.ValidateAsync(viewModel); // var validateResult = this.GetValue(validateMessage); // if (!validateResult) { return validateMessage.Result; } // ACCOUNTS var accountIDs = new long[] { value.ExpenseAccountID, value.IncomeAccountID }; var accounts = await this.dbContext.Accounts.Where(x => accountIDs.Contains(x.AccountID)).ToListAsync(); // EXPENSE var expenseAccount = accounts.Where(x => x.AccountID == value.ExpenseAccountID).Select(x => x.Text).FirstOrDefault(); var expenseEntry = new Entries.EntryVM { TransferID = transferID, DueDate = value.TransferDate, EntryValue = value.TransferValue, Paid = true, PayDate = value.TransferDate, AccountID = value.ExpenseAccountID, Type = Categories.enCategoryType.Expense }; // INCOME var incomeAccount = accounts.Where(x => x.AccountID == value.IncomeAccountID).Select(x => x.Text).FirstOrDefault(); var incomeEntry = new Entries.EntryVM { TransferID = transferID, DueDate = value.TransferDate, EntryValue = value.TransferValue, Paid = true, PayDate = value.TransferDate, AccountID = value.IncomeAccountID, Type = Categories.enCategoryType.Income }; // DESCRIPTION expenseEntry.Text = this.GetTranslation("TRANSFERS_TO_ACCOUNT_DESCRIPTION").Replace("{accountText}", incomeAccount); incomeEntry.Text = this.GetTranslation("TRANSFERS_FROM_ACCOUNT_DESCRIPTION").Replace("{accountText}", expenseAccount); // APPLY await entriesService.CreateAsync(expenseEntry); await entriesService.CreateAsync(incomeEntry); // RESULT return(this.OkResponse(true)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }
internal async Task <ActionResult <TransferVM> > UpdateAsync(string transferID, TransferVM viewModel) { try { var entriesService = this.GetService <Entries.EntriesService>(); // VALIDATE // var validateMessage = await this.ValidateAsync(viewModel); // var validateResult = this.GetValue(validateMessage); // if (!validateResult) { return validateMessage.Result; } // EXPENSE DATA var expenseMessage = await entriesService.GetDataAsync(viewModel.ExpenseEntryID); var expenseEntry = this.GetValue(expenseMessage); if (expenseEntry == null) { return(this.WarningResponse("TRANSFERS_RECORD_NOT_FOUND_WARNING")); } expenseEntry.AccountID = viewModel.ExpenseAccountID; expenseEntry.DueDate = viewModel.TransferDate; expenseEntry.PayDate = viewModel.TransferDate; expenseEntry.EntryValue = Math.Abs(viewModel.TransferValue); // INCOME DATA var incomeMessage = await entriesService.GetDataAsync(viewModel.IncomeEntryID); var incomeEntry = this.GetValue(incomeMessage); if (incomeEntry == null) { return(this.WarningResponse("TRANSFERS_RECORD_NOT_FOUND_WARNING")); } incomeEntry.AccountID = viewModel.IncomeAccountID; incomeEntry.DueDate = viewModel.TransferDate; incomeEntry.PayDate = viewModel.TransferDate; incomeEntry.EntryValue = Math.Abs(viewModel.TransferValue); // APPLY CHANGES await entriesService.UpdateAsync(expenseEntry.EntryID, false, expenseEntry); await entriesService.UpdateAsync(incomeEntry.EntryID, false, incomeEntry); // RESULT return(this.OkResponse(viewModel)); } catch (Exception ex) { return(this.ExceptionResponse(ex)); } }