Exemplo n.º 1
0
        public async Task UpdateAsync(AlbumForUpdatingDto dto)
        {
            var albumEntity = await context.Albums
                              .FirstOrDefaultAsync(a => a.Id == dto.Id);

            mapper.Map(dto, albumEntity);
        }
Exemplo n.º 2
0
        public ActionResult PartiallyUpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] JsonPatchDocument<AlbumForUpdatingDto> patchDocument)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return NotFound();
            }

            Album albumEntity = _bandAlbumRepository.GetAlbum(bandId, albumId);

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

            AlbumForUpdatingDto albumToPatch = _mapper.Map<AlbumForUpdatingDto>(albumEntity);
            patchDocument.ApplyTo(albumToPatch, ModelState);

            if (!TryValidateModel(albumToPatch))
                return ValidationProblem(ModelState);

            _mapper.Map(albumToPatch, albumEntity);
            _bandAlbumRepository.UpdateAlbum(albumEntity);
            _bandAlbumRepository.Save();

            return NoContent();
        }
Exemplo n.º 3
0
        public ActionResult UpdateAlbumForBand(Guid bandID, Guid albumID, [FromBody] AlbumForUpdatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandID))
            {
                return(NotFound());
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandID, albumID);

            if (albumFromRepo is null)
            {
                var albumToAdd = _mapper.Map <Entities.Album>(album);
                _bandAlbumRepository.AddAlbum(bandID, albumToAdd);
                _bandAlbumRepository.Save();

                var returnAlbum = _mapper.Map <AlbumDto>(albumToAdd);

                return(CreatedAtRoute("GetAlbumsForBand", new { bandID = bandID, albumID = returnAlbum.ID }, returnAlbum));
            }

            _mapper.Map(album, albumFromRepo);

            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            _bandAlbumRepository.Save();

            return(NoContent());
        }
Exemplo n.º 4
0
        public ActionResult UpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] AlbumForUpdatingDto album)

        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)

            {
                var albumToAdd = _mapper.Map <Entities.Album>(album);
                albumToAdd.Id = albumId;
                _bandAlbumRepository.AddAlbum(bandId, albumToAdd);
                _bandAlbumRepository.Save();

                var albumToReturn = _mapper.Map <AlbumsDto>(albumToAdd);

                return(CreatedAtRoute("GetAlbumForBand", new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn));
            }

            _mapper.Map(album, albumFromRepo);
            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            _bandAlbumRepository.Save();


            return(NoContent());
        }
Exemplo n.º 5
0
        public ActionResult UpdateAlbum(Guid albumId, Guid bandId, AlbumForUpdatingDto albumToUpdateDto)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(StatusCode(StatusCodes.Status404NotFound, new { error = "band doesn't exist" }));
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)
            {
                var albumToCreate = _mapper.Map <Album>(albumToUpdateDto);
                albumToCreate.Id = albumId;

                _bandAlbumRepository.AddAlbum(bandId, albumToCreate);
                _bandAlbumRepository.Save();

                var albumToReturn = _mapper.Map <AlbumDto>(albumToCreate);

                return(CreatedAtRoute("GetAlbumForBand", new { albumId, bandId }, albumToReturn));
            }

            _mapper.Map(albumToUpdateDto, albumFromRepo);

            _bandAlbumRepository.Save();

            return(StatusCode(StatusCodes.Status204NoContent));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> UpdateeSong(
            int id,
            [FromBody] AlbumForUpdatingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var album = await unitOfWork.Albums.GetAsync(id);

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

                if (album.Id != id)
                {
                    return(BadRequest());
                }

                var isAuthorized = await authorizationService
                                   .AuthorizeAsync(User, album.OwenerId, AutherazationOperations.OwenResourse);

                if (!isAuthorized.Succeeded)
                {
                    return(Unauthorized());
                }

                await unitOfWork.Albums.UpdateAsync(dto);

                await unitOfWork.SaveAsync();

                album = await unitOfWork.Albums.GetAsync(id);

                return(Ok(album));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while updating album.", dto);
                return(StatusCode(500));
            }
        }
Exemplo n.º 7
0
        public ActionResult UpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] AlbumForUpdatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)
            {
                return(NotFound());
            }
            _mapper.Map(album, albumFromRepo);
            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            _bandAlbumRepository.Save();

            return(NoContent());
        }
Exemplo n.º 8
0
        public ActionResult PartiallyUpdateAlbumForBand(Guid bandID, Guid albumID, [FromBody] JsonPatchDocument <AlbumForUpdatingDto> patchDocument)
        {
            if (!_bandAlbumRepository.BandExists(bandID))
            {
                return(NotFound());
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandID, albumID);

            if (albumFromRepo is null)
            {
                var albumDto = new AlbumForUpdatingDto();
                patchDocument.ApplyTo(albumDto);
                var albumToAdd = _mapper.Map <Entities.Album>(albumDto);
                albumToAdd.ID = albumID;

                _bandAlbumRepository.AddAlbum(bandID, albumToAdd);
                _bandAlbumRepository.Save();

                var returnAlbum = _mapper.Map <AlbumDto>(albumToAdd);

                return(CreatedAtRoute("GetAlbumsForBand", new { bandID = bandID, albumID = returnAlbum.ID }, returnAlbum));
            }

            var albumToPatch = _mapper.Map <AlbumForUpdatingDto>(albumFromRepo);

            patchDocument.ApplyTo(albumToPatch, ModelState);

            if (!TryValidateModel(albumToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(albumToPatch, albumFromRepo);

            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            _bandAlbumRepository.Save();

            return(NoContent());
        }
Exemplo n.º 9
0
        public ActionResult UpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] AlbumForUpdatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            var albumFromRepo = _bandAlbumRepository.GetAlbum(bandId, albumId);

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

            //pass the album values to albumFromRepo this is the object in the DBcontext
            //albumFromRepo is updated and its in a modified state inside of the Dbcontext
            _mapper.Map(album, albumFromRepo); //the context is updated here, leave off type annotation on Map because put should update all of the properties
            //_bandAlbumRepository.UpdateAlbum(albumFromRepo); //not implemented in repository
            _bandAlbumRepository.Save();

            return(NoContent());
        }
Exemplo n.º 10
0
        public async Task Update_Album_Should_Persiste()
        {
            using (var factory = new MusicStoreContextFactory())
            {
                var albumId = 0;
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = new AlbumEntity
                    {
                        Name        = "Name",
                        Description = "Description"
                    };
                    context.Albums.Add(album);
                    context.SaveChanges();

                    albumId = album.Id;
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var albumForUpdateDto = new AlbumForUpdatingDto
                    {
                        Id          = albumId,
                        Name        = "Updated",
                        Description = "Updated"
                    };
                    var unitOfWork = new UnitOfWork(context, _mapper);
                    await unitOfWork.Albums.UpdateAsync(albumForUpdateDto);

                    await unitOfWork.SaveAsync();
                }
                using (var context = factory.CreateMusicStoreContext())
                {
                    var album = context.Albums.First();
                    Assert.Equal("Updated", album.Name);
                    Assert.Equal("Updated", album.Description);
                }
            }
        }
        public ActionResult PartiallyUpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] JsonPatchDocument <AlbumForUpdatingDto> patchDocument)
        {
            if (!_repository.BandExists(bandId))
            {
                return(NotFound());
            }
            var albumFromRepo = _repository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)
            {
                var albumDto = new AlbumForUpdatingDto();
                patchDocument.ApplyTo(albumDto);
                var albumToAdd = _mapper.Map <Album>(albumDto);
                albumToAdd.Id = albumId;

                _repository.AddAlbum(bandId, albumToAdd);
                _repository.Save();

                var albumToReturn = _mapper.Map <AlbumDto>(albumToAdd);
                return(CreatedAtRoute("GetAlbum", new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn));
            }

            var albumToPatch = _mapper.Map <AlbumForUpdatingDto>(albumFromRepo);

            patchDocument.ApplyTo(albumToPatch);

            if (!TryValidateModel(albumToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(albumToPatch, albumFromRepo);
            _repository.UpdateAlbum(albumFromRepo);
            _repository.Save();

            return(NoContent());
        }
Exemplo n.º 12
0
        public async Task <ActionResult <AlbumDto> > UpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] AlbumForUpdatingDto album)
        {
            if (!await _bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            //if (!await _bandAlbumRepository.AlbumExists(albumId))
            //    return NotFound();


            var albumFromRepo = await _bandAlbumRepository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)
            {
                var albumToAdd = _mapper.Map <Album>(album);
                albumToAdd.Id = albumId;
                _bandAlbumRepository.AddAlbum(bandId, albumToAdd);

                await _bandAlbumRepository.Save();

                var upsertingAlbumToReturn = _mapper.Map <AlbumDto>(albumToAdd);

                return(CreatedAtRoute(
                           "GetAlbumForBand",
                           new { bandId = bandId, albumId = upsertingAlbumToReturn.Id },
                           upsertingAlbumToReturn
                           ));
            }

            //var albumEntity = _mapper.Map<Album>(album);

            _mapper.Map(album, albumFromRepo); // replace the albumFromRepo by album

            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            await _bandAlbumRepository.Save();

            var albumToReturn = _mapper.Map <AlbumDto>(albumFromRepo);

            return(CreatedAtRoute(
                       "GetAlbumForBand",
                       new { bandId = bandId, albumId = albumToReturn.Id },
                       albumToReturn
                       ));
        }
Exemplo n.º 13
0
        public async Task <ActionResult> PartiallyUpdateAlbumForBand(Guid bandId, Guid albumId, [FromBody] JsonPatchDocument <AlbumForUpdatingDto> patchDocument)
        {
            if (!await _bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            var albumFromRepo = await _bandAlbumRepository.GetAlbum(bandId, albumId);

            if (albumFromRepo == null)
            {
                var albumDto = new AlbumForUpdatingDto();
                patchDocument.ApplyTo(albumDto, ModelState); // Aplly to Dto so we can use the restriction set in Dto

                var albumToAdd = _mapper.Map <Album>(albumDto);

                albumToAdd.Id = albumId;
                _bandAlbumRepository.AddAlbum(bandId, albumToAdd);

                await _bandAlbumRepository.Save();

                var upsertingAlbumToReturn = _mapper.Map <AlbumDto>(albumToAdd);

                return(CreatedAtRoute(
                           "GetAlbumForBand",
                           new { bandId = bandId, albumId = upsertingAlbumToReturn.Id },
                           upsertingAlbumToReturn
                           ));
            }

            /*
             * The Strategy doesn't override the current from repo directly.
             * 1. Get the CurrentData
             * 2. Transform CurrentData to Dto
             * 3. Overriding the Dto (Since the Dto has updating restriction)
             * 4. Using the Dto to override the CurrentData in the DB
             * 5. Save the Changes
             */

            var albumToPatch = _mapper.Map <AlbumForUpdatingDto>(albumFromRepo); // Firstly, transform the one in DB to the Dto

            patchDocument.ApplyTo(albumToPatch, ModelState);                     // Then we can apply Changes to current Dto

            if (!TryValidateModel(albumToPatch))
            {
                return(ValidationProblem(ModelState));
            }


            _mapper.Map(albumToPatch, albumFromRepo); // Using the updatedDto to override the current from repo
            _bandAlbumRepository.UpdateAlbum(albumFromRepo);
            await _bandAlbumRepository.Save();

            //return NoContent();

            var albumToReturn = _mapper.Map <AlbumDto>(albumFromRepo);

            return(CreatedAtRoute(
                       "GetAlbumForBand",
                       new { bandId = bandId, albumId = albumToReturn.Id },
                       albumToReturn
                       ));
        }