public async Task UpdateArtistAsyncShouldSwitchPhotoLinkToDefaultImgaeIfPreviousIsRemoved(string photoLink)
        {
            var artist = new Artist
            {
                FullName  = "artist1",
                Biography = "biography1",
                BirthDate = DateTime.Parse("25 July 2019"),
                PhotoLink = "photo1",
            };
            await dbContext.Artists.AddAsync(artist);

            await dbContext.SaveChangesAsync();

            var input = new UpdateArtistInputModel
            {
                Id        = dbContext.Artists.First().Id,
                FullName  = "artist2",
                Biography = "biography2",
                BirthDate = DateTime.Parse("24 July 2019"),
                PhotoLink = photoLink,
            };

            var artistService = new ArtistService(dbContext, mapper);

            var actualResult = await artistService.UpdateArtistAsync(input);

            Assert.True(actualResult);
            Assert.True(dbContext.Artists.Count() == 1);
            Assert.True(dbContext.Artists.First().FullName == "artist2");
            Assert.True(dbContext.Artists.First().Biography == "biography2");
            Assert.True(dbContext.Artists.First().BirthDate == DateTime.Parse("24 July 2019"));
            Assert.True(dbContext.Artists.First().PhotoLink == GlobalConstants.noArtistImage);
        }
        public async Task UpdateArtistAsyncShouldReturnFalseIfIdIsInvalidOrDbIsEmpty()
        {
            var artist = new Artist
            {
                FullName  = "artist1",
                Biography = "biography1",
                BirthDate = DateTime.Parse("25 July 2019"),
                PhotoLink = "photo1",
            };
            await dbContext.Artists.AddAsync(artist);

            await dbContext.SaveChangesAsync();

            var input = new UpdateArtistInputModel
            {
                Id        = "invalid",
                FullName  = "artist2",
                Biography = "biography2",
                BirthDate = DateTime.Parse("24 July 2019"),
                PhotoLink = "photo2",
            };

            var artistService = new ArtistService(dbContext, mapper);

            var actualResult = await artistService.UpdateArtistAsync(input);

            Assert.False(actualResult);
        }
        public async Task UpdateArtistAsyncShouldUpdateArtistProperlyIfValidDataIsEntered()
        {
            var artist = new Artist
            {
                FullName  = "artist1",
                Biography = "biography1",
                BirthDate = DateTime.Parse("25 July 2019"),
                PhotoLink = "photo1",
            };
            await dbContext.Artists.AddAsync(artist);

            await dbContext.SaveChangesAsync();

            var input = new UpdateArtistInputModel
            {
                Id        = dbContext.Artists.First().Id,
                FullName  = "artist2",
                Biography = "biography2",
                BirthDate = DateTime.Parse("24 July 2019"),
                PhotoLink = "photo2",
            };

            var artistService = new ArtistService(dbContext, mapper);

            var actualResult = await artistService.UpdateArtistAsync(input);

            Assert.True(actualResult);
            Assert.True(dbContext.Artists.Count() == 1);
            Assert.True(dbContext.Artists.First().FullName == "artist2");
            Assert.True(dbContext.Artists.First().Biography == "biography2");
            Assert.True(dbContext.Artists.First().BirthDate == DateTime.Parse("24 July 2019"));
            Assert.True(dbContext.Artists.First().PhotoLink == "photo2");
        }
        public async Task <IActionResult> Update(UpdateArtistInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(View(input));
            }

            if (!await artistService.UpdateArtistAsync(input))
            {
                return(View(input));
            }

            return(Redirect(redirectArtistsDetails + input.Id));
        }
        public async Task <bool> UpdateArtistAsync(UpdateArtistInputModel input)
        {
            if (!await dbContext.Artists.AnyAsync(artist => artist.Id == input.Id))
            {
                return(false);
            }

            var artistFromDb = await dbContext.Artists.FindAsync(input.Id);

            artistFromDb.FullName  = input.FullName;
            artistFromDb.BirthDate = input.BirthDate;
            artistFromDb.Biography = input.Biography;
            artistFromDb.PhotoLink = (string.IsNullOrEmpty(input.PhotoLink) || string.IsNullOrWhiteSpace(input.PhotoLink)) ? GlobalConstants.noArtistImage : input.PhotoLink;

            dbContext.Update(artistFromDb);
            await dbContext.SaveChangesAsync();

            return(true);
        }