Beispiel #1
0
        public double GetMonthExpenses(int month, int year, AccountViewModel account)
        {
            double balance = 0.00;
            List <LiveTilesPaymentInfo> allpayment = new List <LiveTilesPaymentInfo>();
            List <PaymentViewModel>     payments   = crudService.ReadManyNoTracked <PaymentViewModel>()
                                                     .Where(x => x.ChargedAccountId == account.Id)
                                                     .ToList();

            foreach (PaymentViewModel item in payments)
            {
                if (item.IsRecurring)
                {
                    if (item.Type != PaymentType.Income)
                    {
                        allpayment.AddRange(GetRecurrence(item));
                    }
                }
                else if (item.Type != PaymentType.Income)
                {
                    CreateLiveTileInfos(item, allpayment, item.Date.Date);
                }
            }

            List <LiveTilesPaymentInfo> tiles = allpayment
                                                .Where(x => x.Date.Date.Month == month && x.Date.Date.Year == year)
                                                .ToList();

            foreach (LiveTilesPaymentInfo item in tiles)
            {
                balance += item.Amount;
            }

            allpayment.Clear();
            return(balance);
        }
 /// <inheritdoc />
 public async Task <double> GetTotalBalance()
 {
     return(await crudServices.ReadManyNoTracked <AccountViewModel>()
            .AreNotExcluded()
            .SumAsync(x => x.CurrentBalance)
            );
 }
        protected override async Task SaveCategory()
        {
            if (string.IsNullOrEmpty(SelectedCategory.Name))
            {
                await dialogService.ShowMessage(Strings.MandatoryFieldEmptyTitle, Strings.NameRequiredMessage)
                ;

                return;
            }

            if (await crudServices.ReadManyNoTracked <AccountViewModel>().AnyWithNameAsync(SelectedCategory.Name)
                )
            {
                await dialogService.ShowMessage(Strings.DuplicatedNameTitle, Strings.DuplicateCategoryMessage)
                ;

                return;
            }

            await crudServices.CreateAndSaveAsync(SelectedCategory, "ctor(2)")
            ;

            if (!crudServices.IsValid)
            {
                await dialogService.ShowMessage(Strings.GeneralErrorTitle, crudServices.GetAllErrors())
                ;
            }

            await NavigationService.Close(this)
            ;
        }
Beispiel #4
0
        /// <summary>
        /// When a user subscribes to an artist, we want to store the artist in our database.
        /// An artist may already exist if another user has already subscribed to them.
        /// When a user subscribes they will usually subscribe to many artists in one go.
        /// This method is optimised for that scenario, allowing for the fact that some artists may already exist.
        /// </summary>
        public async Task <GetOrCreateManyOutput> GetOrCreateMany(GetOrCreateManyInput input)
        {
            if (!input.Artists.Any())
            {
                throw new ArgumentException("Artists must contain at least one artist", "Artists");
            }

            var spotifyArtistIds = input.Artists.Select(a => a.Id).ToList();

            try
            {
                var output = new GetOrCreateManyOutput();

                var existingArtists = await _crudServices.ReadManyNoTracked <ArtistDto>()
                                      .Where(a => spotifyArtistIds.Contains(a.SpotifyId))
                                      .ToListAsync();

                foreach (var spotifyArtist in input.Artists)
                {
                    var existingArtist = existingArtists.FirstOrDefault(a => a.SpotifyId == spotifyArtist.Id);
                    if (existingArtist == null)
                    {
                        existingArtist = await _crudServices.CreateAndSaveAsync(new ArtistDto
                        {
                            Name      = spotifyArtist.Name,
                            SpotifyId = spotifyArtist.Id
                        });
                    }

                    output.Artists.Add(existingArtist);
                }

                return(output);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "SpotifyArtistIds: " + String.Join(',', spotifyArtistIds));
                return(new GetOrCreateManyOutput
                {
                    ErrorMessage = ex.Message
                });
            }
        }
Beispiel #5
0
        protected override async Task SaveAccount()
        {
            if (await crudService.ReadManyNoTracked <AccountViewModel>()
                .AnyWithNameAsync(SelectedAccount.Name))
            {
                await dialogService.ShowMessage(Strings.MandatoryFieldEmptyTitle, Strings.NameRequiredMessage);

                return;
            }

            await crudService.CreateAndSaveAsync(SelectedAccount, "ctor(4)");

            if (!crudService.IsValid)
            {
                await dialogService.ShowMessage(Strings.GeneralErrorTitle, crudService.GetAllErrors());
            }

            NavigationService.GoBack();
        }
Beispiel #6
0
        /// <summary>
        ///     Constructor
        /// </summary>
        public PaymentListViewActionViewModel(int accountId,
                                              ICrudServicesAsync crudServices,
                                              ISettingsFacade settingsFacade,
                                              IDialogService dialogService,
                                              IBalanceViewModel balanceViewModel,
                                              INavigationService navigationService)
        {
            this.accountId = accountId;

            this.crudServices      = crudServices;
            this.settingsFacade    = settingsFacade;
            this.dialogService     = dialogService;
            this.balanceViewModel  = balanceViewModel;
            this.navigationService = navigationService;

            var accountCount = crudServices.ReadManyNoTracked <AccountViewModel>().Count();

            IsTransferAvailable   = accountCount >= 2;
            IsAddIncomeAvailable  = accountCount >= 1;
            IsAddExpenseAvailable = accountCount >= 1;
        }
Beispiel #7
0
        /// <summary>
        /// When we notify a subscriber about an album, we want to store the album in our database so we know
        /// that we've handled the album and don't send more notifications for it in future.
        /// </summary>
        public async Task <CreateAlbumsOutput> CreateAlbums(CreateAlbumsInput input)
        {
            try
            {
                foreach (var inputAlbum in input.Albums)
                {
                    //If the album is attributed to more than one artist, there's a chance the album already exists
                    var existingAlbum = await _crudServices.ReadManyNoTracked <Album>()
                                        .Where(a => a.SpotifyId == inputAlbum.SpotifyId)
                                        .Include(a => a.Artists)
                                        .FirstOrDefaultAsync();

                    if (existingAlbum != null)
                    {
                        //Check if we need to add this artist to the list of artists for the album
                        if (!existingAlbum.Artists.Any(a => a.ArtistId == input.Artist.Id))
                        {
                            Logger.LogInformation("Adding SpotifyArtistId: {0} to existing SpotifyAlbumId: {1} in database", input.Artist.SpotifyId, inputAlbum.SpotifyId);

                            var artistAlbum = new ArtistAlbum
                            {
                                ArtistId = input.Artist.Id,
                                AlbumId  = existingAlbum.Id
                            };

                            await _crudServices.CreateAndSaveAsync(artistAlbum);
                        }
                    }
                    else
                    {
                        Logger.LogInformation("Saving SpotifyAlbumId: {0} to database", inputAlbum.SpotifyId);

                        var newAlbum = new Album
                        {
                            Name        = inputAlbum.Name,
                            SpotifyId   = inputAlbum.SpotifyId,
                            ReleaseDate = inputAlbum.ReleaseDate,
                            Artists     = new List <ArtistAlbum>
                            {
                                new ArtistAlbum
                                {
                                    ArtistId = input.Artist.Id
                                }
                            }
                        };

                        await _crudServices.CreateAndSaveAsync(newAlbum);
                    }
                }

                return(new CreateAlbumsOutput
                {
                    ErrorMessage = _crudServices.IsValid ? null : _crudServices.GetAllErrors()
                });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "");
                return(new CreateAlbumsOutput
                {
                    ErrorMessage = ex.Message
                });
            }
        }
Beispiel #8
0
        public async Task <SubscribeToArtistsOutput> SubscribeToArtists(SubscribeToArtistsInput input)
        {
            if (input.Subscriber == null)
            {
                throw new ArgumentException("Subscriber must not be null", "Subscriber");
            }

            if (!input.Artists.Any())
            {
                throw new ArgumentException("Artists must contain at least one artist", "Artists");
            }

            try
            {
                //The subscriber may have existing subscriptions
                var existingSubscriptions = await _crudServices.ReadManyNoTracked <Subscription>()
                                            .Where(s => s.SubscriberId == input.Subscriber.Id)
                                            .ToListAsync();

                int  existingSubscriptionsCount = existingSubscriptions.Count;
                int  subscriptionsCount         = existingSubscriptionsCount;
                bool limitReached = subscriptionsCount >= Subscription.MaxPerSubscriber;

                if (subscriptionsCount < Subscription.MaxPerSubscriber)
                {
                    foreach (var artist in input.Artists)
                    {
                        if (!existingSubscriptions.Any(s => s.ArtistId == artist.Id))
                        {
                            //Don't use _crudServices.CreateAndSaveAsync, because we only want to call Save once for performance reasons
                            _crudServices.Context.Add(new Subscription
                            {
                                ArtistId     = artist.Id,
                                SubscriberId = input.Subscriber.Id
                            });

                            subscriptionsCount++;
                            if (subscriptionsCount >= Subscription.MaxPerSubscriber)
                            {
                                //Reached the maximum limit, don't create any more subscriptions for this Subscriber
                                limitReached = true;
                                break;
                            }
                        }
                    }

                    await _crudServices.Context.SaveChangesAsync();
                }

                var output = new SubscribeToArtistsOutput
                {
                    ErrorMessage = _crudServices.IsValid ? null : _crudServices.GetAllErrors()
                };

                output.SetStatusMessage(existingSubscriptionsCount, subscriptionsCount - existingSubscriptionsCount, limitReached);
                output.RequiresEmailVerification = !input.Subscriber.EmailAddressVerified;

                return(output);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "SubscriberId: " + input.Subscriber);
                return(new SubscribeToArtistsOutput
                {
                    ErrorMessage = ex.Message
                });
            }
        }
Beispiel #9
0
 public async Task <ActionResult <WebApiMessageAndResult <List <Review> > > > GetReviewsManyAsync([FromServices] ICrudServicesAsync service)
 {
     return(service.Response(await service.ReadManyNoTracked <Review>().ToListAsync()));
 }