Beispiel #1
0
        public IActionResult CreateBandsCollection([FromBody]
                                                   IEnumerable <BandCreationDTO> bandsCollection)
        {
            var bandsCollectionMapped = _mapper.Map <IEnumerable <Band> >(bandsCollection);

            foreach (var band in bandsCollectionMapped)
            {
                _bandAlbumRepository.AddBand(band);
            }
            _bandAlbumRepository.SaveChanges();
            return(Ok());
        }
Beispiel #2
0
        public ActionResult <AlbumDTO> CreateAlbumForBand(int bandID,
                                                          [FromBody] AlbumCreationDTO albumCreationDTO)
        {
            if (!_bandAlbumRepository.BandExists(bandID))
            {
                return(NotFound());
            }

            var albumEntityMapped = _mapper.Map <Album>(albumCreationDTO);

            _bandAlbumRepository.AddAlbum(bandID, albumEntityMapped);
            _bandAlbumRepository.SaveChanges();

            var albumDTOMapped = _mapper.Map <AlbumDTO>(albumEntityMapped);

            return(CreatedAtRoute("GetAlbumForABand", new { bandID = bandID, albumID = albumDTOMapped.AlbumID },
                                  albumDTOMapped));
        }
Beispiel #3
0
        public ActionResult <BandDTO> CreateBand([FromBody] BandCreationDTO bandCreationDTO)
        {
            //No need to null check as bad req. will be thrown by API automatically if null.
            var bandEntityMapped = _mapper.Map <Band>(bandCreationDTO);

            _bandAlbumRepository.AddBand(bandEntityMapped);

            //Adding this functionality on my own because the instructor doesn't mention how to add
            //albums as well into the database. Not including this code will only save the band and not albums in dbset.
            foreach (var album in bandEntityMapped.Albums)
            {
                _bandAlbumRepository.AddAlbum(bandEntityMapped.ID, album);
            }

            _bandAlbumRepository.SaveChanges();
            //Now we need to show the saved band details so mapping the entity with DTO.
            var mappedWithDTO = _mapper.Map <BandDTO>(bandEntityMapped);

            return(CreatedAtRoute("GetBand", new { bandID = mappedWithDTO.ID }, mappedWithDTO));
        }