public async Task <ActionResult> UpdateAlbum(int albumId, [FromBody] AlbumChangeDto album) { if (!ModelState.IsValid) { // Return the model errors return(new ApiResult(ModelState)); } var dbAlbum = await _storeContext.Albums.SingleOrDefaultAsync(a => a.AlbumId == albumId); if (dbAlbum == null) { return(new ApiResult { StatusCode = 404, Message = string.Format("The album with ID {0} was not found.", albumId) }); } // Save the changes to the DB SimpleMapper.Map(album, dbAlbum); await _storeContext.SaveChangesAsync(); // TODO: Handle missing record, key violations, concurrency issues, etc. return(new ApiResult { Message = "Album updated successfully." }); }
public async Task <ActionResult> CreateAlbum([FromBody] AlbumChangeDto album) { if (!ModelState.IsValid) { // Return the model errors return(BadRequest(ModelState)); } // Save the changes to the DB var dbAlbum = new Album(); _storeContext.Albums.Add(Mapper.Map(album, dbAlbum)); await _storeContext.SaveChangesAsync(); // TODO: Handle missing record, key violations, concurrency issues, etc. return(new ObjectResult(new { Data = dbAlbum.AlbumId, Message = "Album created successfully." })); }