Exemplo n.º 1
0
        internal async Task <ActionResult <EntryVM> > GetDataAsync(long entryID)
        {
            try
            {
                var data = await this.GetDataQuery()
                           .Where(x => x.EntryID == entryID)
                           .FirstOrDefaultAsync();

                var viewModel = EntryVM.Convert(data);

                if (data.PatternID.HasValue)
                {
                    var patternMessage = await this.GetService <Patterns.PatternsService>().GetPatternAsync(data.PatternID.Value);

                    viewModel.PatternRow = this.GetValue(patternMessage);
                }

                if (data.AccountID.HasValue)
                {
                    var accountMessage = await this.GetService <Accounts.AccountsService>().GetDataAsync(data.AccountID.Value);

                    viewModel.AccountRow = this.GetValue(accountMessage);
                }

                if (data.CategoryID.HasValue)
                {
                    var categoryMessage = await this.GetService <Categories.CategoriesService>().GetDataAsync(data.CategoryID.Value);

                    viewModel.CategoryRow = this.GetValue(categoryMessage);
                }

                return(this.OkResponse(viewModel));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemplo n.º 2
0
        internal async Task <ActionResult <EntryVM> > UpdateAsync(long entryID, bool editFutureRecurrencies, EntryVM viewModel)
        {
            try
            {
                // VALIDATE
                var validateMessage = await this.ValidateAsync(viewModel);

                var validateResult = this.GetValue(validateMessage);
                if (!validateResult)
                {
                    return(validateMessage.Result);
                }

                // LOCATE DATA
                var data = await this.GetDataQuery().Where(x => x.EntryID == entryID).FirstOrDefaultAsync();

                if (data == null)
                {
                    return(this.NotFoundResponse());
                }

                // REMOVE BALANCE
                await this.GetService <Balances.BalancesService>().RemoveAsync(data);

                // REMOVE PATTERN
                var newPatternID = await this.GetService <Patterns.PatternsService>().GetPatternIDAsync(viewModel);

                if (data.PatternID.HasValue && data.PatternID.Value != newPatternID)
                {
                    await this.GetService <Patterns.PatternsService>().RemoveAsync(data.PatternID.Value);
                }

                // APPLY CHANGES
                data.Text       = viewModel.Text;
                data.CategoryID = viewModel.CategoryID;
                data.DueDate    = viewModel.DueDate;
                data.EntryValue = Math.Abs(viewModel.EntryValue);
                data.Paid       = viewModel.Paid;
                if (viewModel.Paid && viewModel.PayDate.HasValue)
                {
                    data.PayDate = viewModel.PayDate;
                }
                else
                {
                    data.PayDate = null;
                }
                if (viewModel.AccountID.HasValue)
                {
                    data.AccountID = viewModel.AccountID;
                }

                // SEARCH DATE
                data.SearchDate = data.DueDate;
                if (data.Paid && data.PayDate.HasValue)
                {
                    data.SearchDate = data.PayDate.Value;
                }
                this.ApplySorting(data);

                // ADD PATTERN
                if (!data.PatternID.HasValue || data.PatternID.Value != newPatternID)
                {
                    data.PatternID = await this.GetService <Patterns.PatternsService>().AddAsync(viewModel);
                }

                // SAVE IT
                await this.dbContext.SaveChangesAsync();

                // ADD BALANCE
                await this.GetService <Balances.BalancesService>().AddAsync(data);

                // FIX BALANCE
                await this.GetService <Balances.BalancesService>().FixAsync(data);

                // EDIT FUTURE RECURRENCIES
                if (data.RecurrencyID.HasValue && data.RecurrencyID.Value > 0)
                {
                    if (editFutureRecurrencies)
                    {
                        await this.GetService <Recurrencies.RecurrenciesService>().UpdateAsync(data.RecurrencyID.Value, data.EntryID);
                    }
                    await this.GetService <Recurrencies.RecurrenciesService>().UpdatePortionsAsync(data.RecurrencyID.Value);
                }

                // RESULT
                var result = EntryVM.Convert(data);
                return(this.OkResponse(result));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemplo n.º 3
0
        internal async Task <ActionResult <EntryVM> > CreateAsync(EntryVM value)
        {
            try
            {
                DateTime startTime;

                // VALIDATE
                var validateMessage = await this.ValidateAsync(value);

                var validateResult = this.GetValue(validateMessage);
                if (!validateResult)
                {
                    return(validateMessage.Result);
                }

                // NEW MODEL
                var data = new EntryData()
                {
                    ResourceID = this.GetService <Helpers.User>().ResourceID,
                    Type       = (short)value.Type,
                    Text       = value.Text,
                    CategoryID = value.CategoryID,
                    DueDate    = value.DueDate,
                    EntryValue = Math.Abs(value.EntryValue),
                    Paid       = value.Paid,
                    RowStatus  = 1
                };
                if (value.Paid && value.PayDate.HasValue)
                {
                    data.PayDate = value.PayDate;
                }
                if (value.AccountID.HasValue)
                {
                    data.AccountID = value.AccountID;
                }
                if (!string.IsNullOrEmpty(value.TransferID))
                {
                    data.TransferID = value.TransferID;
                }

                // SEARCH DATE
                data.SearchDate = data.DueDate;
                if (data.Paid && data.PayDate.HasValue)
                {
                    data.SearchDate = data.PayDate.Value;
                }

                // PATTERN
                data.PatternID = await this.GetService <Patterns.PatternsService>().AddAsync(value);

                // RECURRENCY
                data.RecurrencyID = value.RecurrencyID;
                if (value.Recurrency != null && data.PatternID.HasValue)
                {
                    var recurrency = new Recurrencies.RecurrencyVM
                    {
                        PatternID  = data.PatternID.Value,
                        AccountID  = data.AccountID.Value,
                        EntryDate  = data.DueDate,
                        EntryValue = data.EntryValue,
                        Type       = value.Recurrency.Type,
                        Count      = value.Recurrency.Count
                    };
                    data.RecurrencyID = await this.GetService <Recurrencies.RecurrenciesService>().CreateAsync(recurrency);
                }

                // APPLY
                startTime = DateTime.Now;
                await this.dbContext.Entries.AddAsync(data);

                await this.dbContext.SaveChangesAsync();

                this.TrackMetric("Add new Entry", Math.Round(DateTime.Now.Subtract(startTime).TotalMilliseconds, 0));

                // SORTING
                startTime = DateTime.Now;
                this.ApplySorting(data);
                await this.dbContext.SaveChangesAsync();

                this.TrackMetric("Apply Sorting to new Entry", Math.Round(DateTime.Now.Subtract(startTime).TotalMilliseconds, 0));

                // ADD BALANCE
                await this.GetService <Balances.BalancesService>().AddAsync(data);

                // FIX BALANCE
                await this.GetService <Balances.BalancesService>().FixAsync(data);

                // RECURRENCY
                if (data.RecurrencyID.HasValue && data.RecurrencyID > 0)
                {
                    if (value.Recurrency != null)
                    {
                        await this.GetService <Recurrencies.RecurrenciesService>().AddEntriesAsync(data.RecurrencyID.Value);
                    }
                    await this.GetService <Recurrencies.RecurrenciesService>().UpdatePortionsAsync(data.RecurrencyID.Value);
                }

                // RESULT
                var result = EntryVM.Convert(data);
                return(this.CreatedResponse("entries", result.EntryID, result));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }