Ejemplo n.º 1
0
        internal async Task <long?> CreateAsync(Recurrencies.RecurrencyVM value)
        {
            var startTime = DateTime.Now;

            try
            {
                // VALIDATION
                if (value == null)
                {
                    return(null);
                }
                if (value.RecurrencyID != 0)
                {
                    return(value.RecurrencyID);
                }

                // ADD NEW
                var data = new RecurrencyData
                {
                    ResourceID  = this.GetService <Helpers.User>().ResourceID,
                    PatternID   = value.PatternID,
                    AccountID   = value.AccountID,
                    InitialDate = value.EntryDate,
                    EntryDate   = value.EntryDate,
                    EntryValue  = value.EntryValue,
                    Type        = (short)value.Type,
                    Count       = value.Count,
                    RowStatus   = 1
                };
                await this.dbContext.Recurrencies.AddAsync(data);

                await this.dbContext.SaveChangesAsync();

                // RESULT
                return(data.RecurrencyID);
            }
            catch (Exception) { throw; }
            finally { this.TrackMetric("Create Recurrency", Math.Round(DateTime.Now.Subtract(startTime).TotalMilliseconds, 0)); }
        }
Ejemplo n.º 2
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)); }
        }