Exemple #1
0
        public async Task <GetOrCreateSubscriberOutput> GetOrCreate(GetOrCreateSubscriberInput input)
        {
            try
            {
                SubscriberDto subscriberDto   = null;
                bool          newSubscriber   = false;
                string        normalisedEmail = StringUtils.NormaliseEmailAddress(input.EmailAddress);

                subscriberDto = await _crudServices.ReadSingleAsync <SubscriberDto>(s => s.EmailAddress == normalisedEmail);

                if (subscriberDto == null)
                {
                    string emailVerifyCode = !input.EmailAddressVerified
                        ? Guid.NewGuid().ToString("N").ToLowerInvariant().Truncate(Subscriber.MAX_LENGTH_EMAIL_VERIFY_CODE)
                        : null;

                    subscriberDto = await _crudServices.CreateAndSaveAsync(new SubscriberDto
                    {
                        EmailAddress         = normalisedEmail,
                        EmailAddressVerified = input.EmailAddressVerified,
                        EmailVerifyCode      = emailVerifyCode
                    });

                    if (!_crudServices.IsValid)
                    {
                        return(new GetOrCreateSubscriberOutput
                        {
                            ErrorMessage = _crudServices.GetAllErrors()
                        });
                    }

                    newSubscriber = true;
                }

                return(new GetOrCreateSubscriberOutput
                {
                    Subscriber = subscriberDto,
                    CreatedNewSubscriber = newSubscriber,
                    ErrorMessage = subscriberDto == null ? $"Error creating subscriber with email '{normalisedEmail}' in database" : null
                });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "EmailAddress: " + input.EmailAddress);
                return(new GetOrCreateSubscriberOutput
                {
                    ErrorMessage = ex.Message
                });
            }
        }
        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)
            ;
        }
        protected override async Task SaveAccount()
        {
            await crudServices.UpdateAndSaveAsync(SelectedAccount);

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

            CancelCommand.Execute(null);
        }
Exemple #4
0
        protected override async Task SaveCategory()
        {
            await crudServices.UpdateAndSaveAsync(SelectedCategory)
            .ConfigureAwait(true);

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

            await CancelCommand.ExecuteAsync().ConfigureAwait(true);
        }
Exemple #5
0
        protected override async Task SavePayment()
        {
            var result = await paymentService.UpdatePayment(SelectedPayment)
                         .ConfigureAwait(true);

            if (!result.Success)
            {
                await dialogService.ShowMessage(Strings.GeneralErrorTitle, crudServices.GetAllErrors())
                .ConfigureAwait(true);
            }

            await NavigationService.Close(this).ConfigureAwait(true);
        }
Exemple #6
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();
        }
Exemple #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
                });
            }
        }
Exemple #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
                });
            }
        }