public IActionResult CreateMusiacian(CreateMusicianDTORequest request) { Track track; if (!_context.Tracks.Any(t => t.TrackName == request.TrackDTO.TrackName)) { track = new Track { TrackName = request.TrackDTO.TrackName, Duration = request.TrackDTO.duration }; } else { track = _context.Tracks.Where(t => t.TrackName == request.TrackDTO.TrackName && t.Duration == request.TrackDTO.duration).First(); } var musician = new Musician { FirstName = request.FirstName, LastName = request.LastName, Nickname = request.NickName }; var musicianTrack = new Musician_Track { Musician = musician, Track = track }; return(Created(Uri.EscapeDataString(""), musician)); }
public IActionResult CreateNewMusician(CreateMusicianRequest request) { if (_context.Musicians.Any(m => m.FirstName == request.FirstName && m.LastName == request.LastName && m.Nickname == request.Nickname)) { return(BadRequest("Such musician already exists")); } Track track; if (!_context.Tracks.Any(t => t.TrackName == request.Track.TrackName && t.Duration == request.Track.Duration)) { track = new Track { TrackName = request.Track.TrackName, Duration = request.Track.Duration, }; } else { track = _context.Tracks.Where(t => t.TrackName == request.Track.TrackName && t.Duration == request.Track.Duration).Single(); } var newMusician = new Musician { FirstName = request.FirstName, LastName = request.LastName, Nickname = request.Nickname, }; var musician_track = new Musician_Track { Musician = newMusician, Track = track, }; _context.SaveChanges(); var response = new CreateMusicianResponseData { FirstName = newMusician.FirstName, LastName = newMusician.LastName, Nickname = newMusician.Nickname, }; return(Created("/" + newMusician.IdMusician.ToString(), response)); }