コード例 #1
0
        public ActionResult <AlbumDTO> CreateAlbumForBand(Guid bandId, [FromBody] AlbumCreateDTO album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }
            var albumEntity = _mapper.Map <Entities.Album>(album);

            _bandAlbumRepository.AddAlbum(bandId, albumEntity);
            _bandAlbumRepository.Save();
            var albumReturn = _mapper.Map <AlbumDTO>(albumEntity);

            return(CreatedAtRoute("GetAlbum", new { bandId = bandId, albumId = albumReturn.Id }, albumReturn));
        }
コード例 #2
0
        public ActionResult <AlbumDto> CreateAlbumForBand(Guid bandID, [FromBody] AlbumForCreatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandID))
            {
                return(NotFound());
            }

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

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

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

            return(CreatedAtRoute("GetAlbumsForBand", new { bandID = bandID, albumID = returnAlbum.ID }, returnAlbum));
        }
コード例 #3
0
        public ActionResult <AlbumDto> CreateAlbumForBand(Guid bandId, [FromBody] AlbumForCreatingDto album)
        {
            var albumEntity = _mapper.Map <Entities.Album>(album);

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

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

            return(CreatedAtRoute("GetAlbumForBand", new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn));
        }
コード例 #4
0
        public async Task <ActionResult <AlbumDto> > CreateNewAlbum(Guid bandId, [FromBody] AlbumForCreatingDto album)
        {
            // Checking if the band Exist
            if (!await _bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

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

            _bandAlbumRepository.AddAlbum(bandId, albumEntity);
            await _bandAlbumRepository.Save();

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

            return(CreatedAtRoute(
                       "GetAlbumForBand",
                       new { bandId = bandId, albumId = albumToReturn.Id },
                       albumToReturn
                       ));
        }
コード例 #5
0
        public ActionResult <AlbumDto> PostAlbum(Guid bandId, AlbumCreationDto creationDto)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(StatusCode(StatusCodes.Status404NotFound, new { error = "Band does not exist " }));
            }

            if (!ModelState.IsValid)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ModelState));
            }

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

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

            var albumDto = _mapper.Map <AlbumDto>(album);

            return(CreatedAtRoute("GetAlbumForBand", new { bandId, albumId = albumDto.BandId }, albumDto));
        }
コード例 #6
0
        public ActionResult <AlbumDto> CreateForAlbumForBand(Guid bandId, [FromBody] AlbumForCreatingDto _album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }
            var AlbumEntity = _mapper.Map <Album>(_album);

            _bandAlbumRepository.AddAlbum(bandId, AlbumEntity);
            _bandAlbumRepository.Save();
            var AlbumToReturn = _mapper.Map <AlbumDto>(AlbumEntity);

            return(CreatedAtRoute("GetAlbumForBand", new { bandId = AlbumToReturn.BandId, albumId = AlbumToReturn.Id }, AlbumToReturn));
        }
コード例 #7
0
        public ActionResult<AlbumDto> CreateAlbumForBand(Guid bandId, [FromBody] AlbumForCreatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return NotFound();
            }

            Album albumEntity = _mapper.Map<Album>(album);
            _bandAlbumRepository.AddAlbum(bandId, albumEntity);
            _bandAlbumRepository.Save();

            AlbumDto albumToReturn = _mapper.Map<AlbumDto>(albumEntity);

            return CreatedAtRoute("GetAlbumForBand", new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn);
        }
コード例 #8
0
        public ActionResult <AlbumDto> CreateAlbum(CreateAlbumDto albumDto)
        {
            if (!_repository.IsBandExists(albumDto.BandId))
            {
                return(NotFound());
            }
            var album = _mapper.Map <Album>(albumDto);

            _repository.AddAlbum(album);
            _repository.Save();

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

            return(CreatedAtRoute("GetAlbum", new { albumId = albumToReturn.Id }, albumToReturn));
        }
コード例 #9
0
        public ActionResult <AlbumDto> CreatingAlbum(Guid bandId, [FromBody] CreatingAlbumForDto album)
        {
            if (!_repo.BandExist(bandId))
            {
                return(NotFound());
            }

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

            _repo.AddAlbum(bandId, albumEntity);
            _repo.Save();

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

            return(CreatedAtRoute("GetAlbumForBand", new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn));
        }
コード例 #10
0
        public ActionResult <AlbumsDto> CreateAlbumForBand(Guid bandId, [FromBody] AlbumForCreatingDto album)
        {
            if (!_bandAlbumRepository.BandExists(bandId))
            {
                return(NotFound());
            }

            var albumEntity = _mapper.Map <Entities.Album>(album); //map to a object the database understands

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

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

            return(CreatedAtRoute("GetAlbumForBand",
                                  new { bandId = bandId, albumId = albumToReturn.Id }, albumToReturn)); //return a location header and 201 created response
        }
コード例 #11
0
ファイル: AlbumsController.cs プロジェクト: AliRafay/Projects
        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));
        }
コード例 #12
0
ファイル: BandsController.cs プロジェクト: AliRafay/Projects
        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));
        }