public IActionResult UpdateArtist([FromBody] ArtistForUpdateDto artist)
        {
            try
            {
                if (artist == null)
                {
                    _logger.LogError("Artist object sent from client is null.");
                    return(BadRequest("Artist object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid Artist object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                bool succes = _logic.UpdateArtist(artist);

                if (succes == false)
                {
                    return(NotFound());
                }

                return(Ok("Artist is updated"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
        public void TestFullyUpdateANonExistingArtist(int artistId)
        {
            // Arrange
            ArtistForUpdateDto artist = new ArtistForUpdateDto
            {
                Name              = "Updated - Artist",
                City              = "Updated - City",
                BirthYear         = 1900,
                Birth             = new DateTime(1900, 01, 01),
                DeathYear         = 2017,
                Death             = new DateTime(2017, 12, 31),
                ShortDescription  = "Updated - Short Description.",
                LongDescription   = "Updated - Long Description.",
                ImageUrl          = "/images/Artists/Updated-Artist.jpg",
                ImageThumbnailUrl = "/images/Artists/Updated-Artist_tn.jpg",
            };

            // Act
            IActionResult actionResult = _cutArtists.UpdateArtist(artistId, artist);

            // Assert
            NotFoundResult result = actionResult as NotFoundResult;

            Assert.Equal(404, result.StatusCode);
        }
Example #3
0
        public bool UpdateArtist(ArtistForUpdateDto artist)
        {
            try
            {
                var artistDto = GetArtistById(artist.id);

                if (artistDto == null)
                {
                    return(false);
                }

                Artist DataEntity = _mapper.Map <Artist>(artistDto);

                _mapper.Map(artist, DataEntity);

                _repository.Update(DataEntity);
                _repository.Save();

                _logger.LogError($"Updated artist with id: {DataEntity.id}");

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateArtist action: {ex.Message}");
                throw new Exception();
            }
        }
Example #4
0
        public async Task UpdateArtist(int artistId, ArtistForUpdateDto a)
        {
            using (var mock = AutoMock.GetLoose())
            {
                var artist = new Artist {
                    ArtistId = artistId
                };
                var cityToCreate = new CityToCreateDto
                {
                    CityName = a.HomeUsCity,
                    StateId  = a.HomeUsStateId ?? 1
                };
                var city = new UsCityDto
                {
                    Id   = 1,
                    Name = a.HomeUsCity,
                };

                mock.Mock <IUtility>().Setup(x => x.CreateCityAsync(a.HomeUsCity, a.HomeUsStateId ?? 1)).Returns(Task.FromResult(1));
                mock.Mock <IUtility>().Setup(x => x.CreateZipCodeAsync(a.HomeUsZipcode, a.HomeUsCityId ?? 1)).Returns(Task.FromResult(1));
                mock.Mock <IArtistData>().Setup(x => x.GetByIdAsync(artistId)).Returns(Task.FromResult(artist));
                mock.Mock <IArtistData>().Setup(x => x.ContextUpdated()).Returns(true);
                mock.Mock <IArtistData>().Setup(x => x.SaveAllAsync()).Returns(Task.FromResult(true));

                var cls      = mock.Create <ArtistRepo>();
                var expected = true;
                var actual   = await cls.UpdateArtistAsync(artistId, a);

                Assert.Equal(expected, actual);
                // More Tests needed
            }
        }
Example #5
0
 public void Update(ArtistForUpdateDto a, Artist artist)
 {
     artist.ArtistName        = a.Name;
     artist.ArtistStatusId    = a.StatusId;
     artist.ArtistGroup       = a.Group;
     artist.UserId            = a.UserId;
     artist.HomeCountryId     = a.HomeCountryId;
     artist.HomeUsstateId     = a.HomeUsStateId;
     artist.HomeUscityId      = a.HomeUsCityId;
     artist.HomeUszipCodeId   = a.HomeUsZipCodeId;
     artist.HomeWorldRegionId = a.HomeWorldRegionId;
     artist.HomeWorldCityId   = a.HomeWorldCityId;
 }
Example #6
0
        public async Task <IActionResult> Save(int id, ArtistForUpdateDto artistForUpdateDto)
        {
            var result = await _artist.UpdateArtistAsync(id, artistForUpdateDto);

            if (result)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest("Error updating the artist"));
            }

            throw new Exception($"Updating artist {id} failed on save");
        }
        public IActionResult UpdateArtist(int artistId, [FromBody] ArtistForUpdateDto artist)
        {
            if (artist == null)
            {
                return(BadRequest());
            }

            if (artist.ShortDescription == artist.Name)
            {
                ModelState.AddModelError("Short Description", "The provided short description should be different from the name.");
            }

            if (artist.LongDescription == artist.Name)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the name.");
            }

            if (artist.LongDescription == artist.ShortDescription)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the short description.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var artistToUpdate = InMemoryDataStore.Current.Artists.FirstOrDefault(a => a.Id == artistId);

            if (artistToUpdate == null)
            {
                return(NotFound());
            }

            artistToUpdate.Name               = artist.Name;
            artistToUpdate.City               = artist.City;
            artistToUpdate.BirthYear          = artist.BirthYear;
            artistToUpdate.Birth              = artist.Birth;
            artistToUpdate.DeathYear          = artist.DeathYear;
            artistToUpdate.Death              = artist.Death;
            artistToUpdate.ShortDescription   = artist.ShortDescription;
            artistToUpdate.LongDescription    = artist.LongDescription;
            artistToUpdate.ImageUrl           = artist.ImageUrl;
            artistToUpdate.ImageThumbnailUrl  = artist.ImageThumbnailUrl;
            artistToUpdate.IsArtistOfTheMonth = (artist.Birth == null) ? false : (artist.Birth.Month == DateTime.Now.Month ? true : false);

            return(NoContent());
        }
Example #8
0
        public async Task <IActionResult> UpdateArtist(
            [FromRoute] Guid artistId,
            [FromBody] ArtistForUpdateDto artist,
            [FromHeader] string mediaType)
        {
            var artistFromRepo = await _artistRepository.GetArtistAsync(artistId);

            //  upserting
            if (artistFromRepo == null)
            {
                var artistEntity = Mapper.Map <Artist>(artist);
                artistEntity.Id = artistId;
                _artistRepository.Create(artistEntity);

                if (!await _artistRepository.SaveChangesAsync())
                {
                    _logger.LogError($"Upserting for artist {artistId} failed");
                }

                var artistToReturn = Mapper.Map <ArtistDto>(artistEntity);

                if (!string.IsNullOrWhiteSpace(mediaType) && mediaType == "application/vnd.BD.json+hateoas")
                {
                    var shapedArtist = _controllerHelper.ShapeAndAddLinkToObject(artistToReturn, "Artist", null);
                    return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, shapedArtist));
                }
                else
                {
                    return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, artistToReturn));
                }
            }

            Mapper.Map(artist, artistFromRepo);

            _artistRepository.Update(artistFromRepo);

            if (!await _artistRepository.SaveChangesAsync())
            {
                _logger.LogError($"updating for artist {artistId} failed");
            }

            return(NoContent());
        }
Example #9
0
        private async Task USCheckForNewLocations(ArtistForUpdateDto a)
        {
            if (!a.HomeUsCity.Name.IsNullOrEmpty() &&
                (a.HomeUsCity.Id < 0 || a.HomeUsCityId == null)
                )
            {
                // Check if new city exists already with null id for State; if not, create new city
                a.HomeUsCityId = await _utility.LookForExistingCityInState(a.HomeUsCity.Name, a.HomeUsStateId.Value);

                a.HomeUsZipCodeId = null;
            }

            if (!a.HomeUsZipcode.ZipCode.IsNullOrEmpty() &&
                (a.HomeUsZipcode.Id < 0 || a.HomeUsZipCodeId == null)
                )
            {
                a.HomeUsZipCodeId = await _utility.LookForExistingZipCodeInCity(a.HomeUsZipcode.ZipCode, a.HomeUsCityId.Value);
            }
        }
        public async Task <IActionResult> UpdateArtist(int id, ArtistForUpdateDto artistForUpdateDto)
        {
            //if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            //    return Unauthorized();

            artistForUpdateDto.Name        = artistForUpdateDto.Name.ToLower();
            artistForUpdateDto.ContactName = artistForUpdateDto.ContactName.ToLower();

            var artistFromRepo = await _repo.GetArtist(id);

            _mapper.Map(artistForUpdateDto, artistFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
Example #11
0
        // private async Task CheckForNullValues(ArtistForUpdateDto a){
        //     if (a.HomeUsCity.Id == -1){
        //         a.HomeUsCityId = null;
        //     }if (a.HomeUsZipCodeid == -1){
        //         a.HomeUsCityId = null;
        //     }
        // }

        // private async Task UpdateWorldArtist(ArtistForUpdateDto a)
        // {
        //     if (a.HomeWorldRegionId == null && a.HomeWorldRegion != null)
        //     {
        //         // a.HomeWorldRegionId = await _utility.GetNewWorldRegionId(a.HomeWorldRegion, a.HomeCountryId);
        //     }
        //     if (a.HomeWorldCityId == null && a.HomeWorldCity != null)
        //     {
        //         // a.HomeWorldCityId = await _utility.GetNewWorldCityId(a.HomeWorldCity, a.HomeWorldRegionId);
        //     }
        // }

        private bool ArtistAttributesValid(ArtistForUpdateDto a)
        {
            if (a.HomeCountryId == 1)
            {
                a.HomeWorldRegionId = null;
                a.HomeWorldCityId   = null;
                return(true);
            }

            if (a.HomeCountryId > 1)
            {
                a.HomeUsStateId   = null;
                a.HomeUsCityId    = null;
                a.HomeUsZipCodeId = null;
                // a.HomeUsCity = null;
                // a.HomeUsZipcode = null;
                return(true);
            }

            return(false);
        }
Example #12
0
        public async Task <bool> UpdateArtistAsync(int artistId, ArtistForUpdateDto a)
        {
            var validValues = ArtistAttributesValid(a);

            if (validValues)
            {
                if (a.HomeCountryId == 1)
                {
                    await USCheckForNewLocations(a);
                }
                else
                {
                    // await UpdateWorldArtist(a);
                }

                var artist = await _artist.GetByIdAsync(artistId);

                _map.Update(a, artist);

                if (_artist.ContextUpdated())
                {
                    if (await _artist.SaveAllAsync())
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }

            return(false);
        }
Example #13
0
        public async Task <IActionResult> PartiallyUpdateArtist(
            [FromRoute] Guid artistId,
            [FromBody] JsonPatchDocument <ArtistForUpdateDto> patchDoc,
            [FromHeader] string mediaType)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var artistFromRepo = await _artistRepository.GetArtistAsync(artistId);

            //  upserting
            if (artistFromRepo == null)
            {
                var artistForUpdateDto = new ArtistForUpdateDto();

                patchDoc.ApplyTo(artistForUpdateDto, ModelState);

                if (!TryValidateModel(artistForUpdateDto))
                {
                    return(new ErrorProcessingEntityObjectResult(ModelState));
                }

                var artistToAdd = Mapper.Map <Artist>(artistForUpdateDto);
                artistToAdd.Id = artistId;

                _artistRepository.Create(artistToAdd);

                if (!await _artistRepository.SaveChangesAsync())
                {
                    _logger.LogError($"Upserting for artist {artistId} failed");
                }

                var artistToReturn = Mapper.Map <ArtistDto>(artistToAdd);


                if (!string.IsNullOrWhiteSpace(mediaType) && mediaType == "application/vnd.BD.json+hateoas")
                {
                    var shapedArtist = _controllerHelper.ShapeAndAddLinkToObject(artistToReturn, "Artist", null);
                    return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, shapedArtist));
                }
                else
                {
                    return(CreatedAtRoute("GetArtist", new { artistId = artistToReturn.Id }, artistToReturn));
                }
            }

            var artistToPatch = Mapper.Map <ArtistForUpdateDto>(artistFromRepo);

            patchDoc.ApplyTo(artistToPatch, ModelState);

            if (!TryValidateModel(artistToPatch))
            {
                return(new ErrorProcessingEntityObjectResult(ModelState));
            }

            Mapper.Map(artistToPatch, artistFromRepo);

            _artistRepository.Update(artistFromRepo);

            if (!await _artistRepository.SaveChangesAsync())
            {
                _logger.LogError($"Updating for artist {artistId} failed");
            }

            return(NoContent());
        }
        public IActionResult PartiallyUpdateArtist(int artistId, [FromBody] JsonPatchDocument <ArtistForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var artistToUpdate = InMemoryDataStore.Current.Artists.FirstOrDefault(a => a.Id == artistId);

            if (artistToUpdate == null)
            {
                return(NotFound());
            }

            var artistToPatch = new ArtistForUpdateDto()
            {
                Name               = artistToUpdate.Name,
                City               = artistToUpdate.City,
                BirthYear          = artistToUpdate.BirthYear,
                Birth              = artistToUpdate.Birth,
                DeathYear          = artistToUpdate.DeathYear,
                Death              = artistToUpdate.Death,
                ShortDescription   = artistToUpdate.ShortDescription,
                LongDescription    = artistToUpdate.LongDescription,
                ImageUrl           = artistToUpdate.ImageUrl,
                ImageThumbnailUrl  = artistToUpdate.ImageThumbnailUrl,
                IsArtistOfTheMonth = artistToUpdate.IsArtistOfTheMonth
            };

            patchDoc.ApplyTo(artistToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (artistToPatch.ShortDescription == artistToPatch.Name)
            {
                ModelState.AddModelError("Short Description", "The provided short description should be different from the name.");
            }

            if (artistToPatch.LongDescription == artistToPatch.Name)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the name.");
            }

            if (artistToPatch.LongDescription == artistToPatch.ShortDescription)
            {
                ModelState.AddModelError("Long Description", "The provided long description should be different from the short description.");
            }

            TryValidateModel(artistToPatch);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            artistToUpdate.Name               = artistToPatch.Name;
            artistToUpdate.City               = artistToPatch.City;
            artistToUpdate.BirthYear          = artistToPatch.BirthYear;
            artistToUpdate.Birth              = artistToPatch.Birth;
            artistToUpdate.DeathYear          = artistToPatch.DeathYear;
            artistToUpdate.Death              = artistToPatch.Death;
            artistToUpdate.ShortDescription   = artistToPatch.ShortDescription;
            artistToUpdate.LongDescription    = artistToPatch.LongDescription;
            artistToUpdate.ImageUrl           = artistToPatch.ImageUrl;
            artistToUpdate.ImageThumbnailUrl  = artistToPatch.ImageThumbnailUrl;
            artistToUpdate.IsArtistOfTheMonth = (artistToPatch.Birth == null) ? false : (artistToPatch.Birth.Month == DateTime.Now.Month ? true : false);

            return(NoContent());
        }