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
 public async Task <ActionResult <EntryVM> > UpdateAsync(long id, [FromBody] EntryVM value, bool editFutureRecurrencies = false)
 {
     if (value == null)
     {
         return(this.BadRequest(this.ModelState));
     }
     return(await this.GetService <EntriesService>().UpdateAsync(id, editFutureRecurrencies, value));
 }
Exemplo n.º 3
0
 public async Task <ActionResult <EntryVM> > CreateAsync([FromBody] EntryVM value)
 {
     if (value == null)
     {
         return(this.BadRequest(this.ModelState));
     }
     return(await this.GetService <EntriesService>().CreateAsync(value));
 }
Exemplo n.º 4
0
        private async Task <ActionResult <bool> > ValidateAsync(EntryVM value)
        {
            try
            {
                // VALIDATE DATES
                if (value.DueDate == null || value.DueDate == DateTime.MinValue)
                {
                    return(this.WarningResponse(this.GetTranslation("ENTRIES_DUEDATE_INVALID_WARNING")));
                }
                if (value.Paid && (!value.PayDate.HasValue || value.PayDate.Value == null || value.PayDate.Value == DateTime.MinValue))
                {
                    return(this.WarningResponse(this.GetTranslation("ENTRIES_PAYDATE_INVALID_WARNING")));
                }

                // VALIDATE VALUE
                if (Math.Round(value.EntryValue, 2) == 0)
                {
                    return(this.WarningResponse(this.GetTranslation("ENTRIES_ENTRYVALUE_INVALID_WARNING")));
                }

                // VALIDATE CONTEXT
                if (value.CategoryID.HasValue)
                {
                    var categoryFound = await this.GetService <Categories.CategoriesService>().GetDataQuery().Where(x => x.CategoryID == value.CategoryID.Value).AnyAsync();

                    if (!categoryFound)
                    {
                        return(this.WarningResponse(this.GetTranslation("ENTRIES_CATEGORY_NOT_FOUND_WARNING")));
                    }
                }

                // VALIDATE ACCOUNT
                var accountFound = await this.GetService <Accounts.AccountsService>().GetDataQuery().Where(x => x.AccountID == value.AccountID).AnyAsync();

                if (!accountFound)
                {
                    return(this.WarningResponse(this.GetTranslation("ENTRIES_ACCOUNT_NOT_FOUND_WARNING")));
                }

                // RESULT
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemplo n.º 5
0
        internal static EntryVM Convert(EntryData value)
        {
            var result = new EntryVM
            {
                EntryID         = value.EntryID,
                Type            = (Categories.enCategoryType)value.Type,
                Text            = value.Text,
                CategoryID      = value.CategoryID,
                DueDate         = value.DueDate,
                EntryValue      = value.EntryValue,
                Paid            = value.Paid,
                PayDate         = value.PayDate,
                AccountID       = value.AccountID,
                PatternID       = value.PatternID,
                RecurrencyID    = value.RecurrencyID,
                RecurrencyItem  = value.RecurrencyItem,
                RecurrencyTotal = value.RecurrencyTotal,
                TransferID      = value.TransferID
            };

            return(result);
        }
Exemplo n.º 6
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.º 7
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)); }
        }