public ActionResult <SongResult> Post(Guid songbookId, [FromBody] SongCreate song) { if (song == null) { return(BadRequest()); } _logger.LogDebug("SongController.Post called to create new song for songbook {@songbookId}: {@songbook}", songbookId, song); var songbook = _repository.GetById(songbookId); if (songbook == null) { _logger.LogWarning("Songbook.Post failed to add song {@song} to songbook {@songbookId}. Songbook was not found.", song, songbookId); return(NotFound()); } var dto = _mapper.Map <SongDto>(song); var newSong = Song.From(dto); //Perform validation if (!newSong.IsValid) { newSong.Validate().AddToModelState(ModelState, null); _logger.LogWarning("{method} failed model validation (ModelState: {@modelState}), returning Unprocessable Entity", nameof(Post), ModelState.Values.SelectMany(v => v.Errors)); return(InvalidModelStateResponseFactory.GenerateResponseForInvalidModelState(ModelState, HttpContext)); } songbook.Songs.Add(newSong); _repository.Save(songbook); return(CreatedAtRoute("GetSong", new { songbookId = newSong.SongbookId, id = newSong.Id }, newSong)); }
public ActionResult <IEnumerable <SongbookResult> > Post([FromBody] IEnumerable <SongbookCreate> songbookCollection) { if (songbookCollection == null) { return(BadRequest()); } _logger.LogDebug("SongbookCollectionController.Post called to add a new collection of songbooks {@songbookCollection}", songbookCollection); var songbooks = _mapper.Map <IEnumerable <SongbookDto> >(songbookCollection).Select(Songbook.From).ToList(); //Perform validation foreach (var songbook in songbooks) { if (!songbook.IsValid) { songbook.Validate().AddToModelState(ModelState, $"({songbook.Title})"); _logger.LogWarning("{method} failed model validation (ModelState: {@modelState}), returning Unprocessable Entity", nameof(Post), ModelState.Values.SelectMany(v => v.Errors)); } } if (!ModelState.IsValid) { return(InvalidModelStateResponseFactory.GenerateResponseForInvalidModelState(ModelState, HttpContext)); } foreach (var songbook in songbooks) { _repository.Save(songbook); } var idsAsString = string.Join(",", songbooks.Select(sb => sb.Id)); return(CreatedAtRoute("GetSongbookCollection", new { ids = idsAsString }, _mapper.Map <IEnumerable <SongbookResult> >(songbooks).Select(CreateLinksForSongbook))); }
public ActionResult <SongbookResult> Post([FromBody] SongbookCreate songbook) { if (songbook == null) { return(BadRequest()); } _logger.LogDebug("SongbookController.Post called to create new songbook: {@songbook}", songbook); var dto = _mapper.Map <SongbookDto>(songbook); var newSongbook = Songbook.From(dto); //Perform validation if (!newSongbook.IsValid) { newSongbook.Validate().AddToModelState(ModelState, null); _logger.LogWarning("{method} failed model validation (ModelState: {@modelState}), returning Unprocessable Entity", nameof(Post), ModelState.Values.SelectMany(v => v.Errors)); return(InvalidModelStateResponseFactory.GenerateResponseForInvalidModelState(ModelState, HttpContext)); } _repository.Save(newSongbook); return(CreatedAtRoute("GetSongbook", new { id = newSongbook.Id }, newSongbook)); }