Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *** Mapper class (with utility collection-mapping functions) ***
- public class AudioMapping
- {
- public static IEnumerable<BLAudio> MapToBL(IEnumerable<Audio> audios) =>
- audios.Select(x => MapToBL(x));
- public static BLAudio MapToBL(Audio audio) =>
- new BLAudio
- {
- Id = audio.Id,
- Title = audio.Title,
- GenreId = audio.GenreId,
- Duration = audio.Duration,
- Url = audio.Url,
- };
- public static IEnumerable<Audio> MapToDAL(IEnumerable<BLAudio> blAudios) =>
- blAudios.Select(x => MapToDAL(x));
- public static Audio MapToDAL(BLAudio audio) =>
- new Audio
- {
- Id = audio.Id ?? 0,
- Title = audio.Title,
- GenreId = audio.GenreId,
- Duration = audio.Duration,
- Url = audio.Url,
- };
- }
- // *** POST action that uses Mapper class ***
- [HttpPost()]
- public ActionResult<Audio> Post(BLAudio audio)
- {
- try
- {
- if (!ModelState.IsValid)
- return BadRequest(ModelState);
- var dbAudio = AudioMapping.MapToDAL(audio);
- _dbContext.Audios.Add(dbAudio);
- _dbContext.SaveChanges();
- audio = AudioMapping.MapToBL(dbAudio);
- return Ok(audio);
- }
- catch (Exception ex)
- {
- return StatusCode(
- StatusCodes.Status500InternalServerError,
- "There has been a problem while fetching the data you requested");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement