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)
            ;
        }
        public async Task <ActionResult <CreateTodoHybridDto> > PostAsync(CreateTodoHybridDto item, [FromServices] ICrudServicesAsync service)
        {
            var result = await service.CreateAndSaveAsync(item);

            //NOTE: to get this to work you MUST set the name of the HttpGet, e.g. [HttpGet("{id}", Name= "GetSingleTodo")],
            //on the Get you want to call, then then use the Name value in the Response.
            //Otherwise you get a "No route matches the supplied values" error.
            //see https://stackoverflow.com/questions/36560239/asp-net-core-createdatroute-failure for more on this
            return(service.Response(this, "GetSingleHybridTodo", new { id = result.Id }, item));
        }
Example #3
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
                });
            }
        }
Example #4
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();
        }
Example #5
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
                });
            }
        }
Example #6
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
                });
            }
        }
Example #7
0
        public async Task <ActionResult <CreateBookDto> > Post(CreateBookDto item, [FromServices] ICrudServicesAsync service)
        {
            var result = await service.CreateAndSaveAsync(item, nameof(Book.CreateBook));

            return(service.Response(this, "GetSingleBook", new { id = result.BookId }, item));
        }