Last active
September 28, 2019 19:57
-
-
Save killnine/4e3f8c29074f61036718a2214f6608e3 to your computer and use it in GitHub Desktop.
Handling REST collections of child entities...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
When a user posts multiple songs to the controller, I want it to redirect back to | |
the Get endpoint. However, I don't believe asp.net's Routing handles that. | |
*/ | |
[Route("api/songbooks/{songbookId}/songcollections")] | |
public class SongCollectionController : Controller | |
{ | |
[HttpGet("{id}", Name = "GetSongCollection")] | |
public IActionResult GetSongCollection(Guid songbookId, [ModelBinder(BinderType = typeof(ArrayModelBinder))]IEnumerable<Guid> ids) | |
{ | |
//..SNIP.. | |
} | |
[HttpPost] | |
public IActionResult Post(Guid songbookId, [FromBody]IEnumerable<SongCreate> songCollection) | |
{ | |
//..SNIP.. | |
_repository.Save(songbook); | |
var idsAsString = string.Join(",", songs.Select(sb => sb.Id)); | |
return CreatedAtRoute("GetSongCollection", new { SongbookId = songbook.Id, Ids = idsAsString }, _mapper.Map<IEnumerable<SongResult>>(songs),); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment