public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var songSet = new SongSet[] { new SongSet { SongId = request.Id, SetId = request.SongSet[0].Set.Id, } }; var songPerformer = new SongPerformer[] { new SongPerformer { SongId = request.Id, PerformerId = request.SongPerformer[0].Performer.Id, } }; var song = new Song { Id = request.Id, Name = request.Name, Genre = request.Genre, Text = request.Text, SongSet = songSet, SongPerformer = songPerformer, }; context.Songs.Add(song); //var user = await context.Users.SingleOrDefaultAsync(x => x.UserName == userAccessor.GetCurrentUsername()); //var set = await context.Sets.SingleOrDefaultAsync(x => x.Id.Equals("271515c0-45e8-4a74-8fa5-01b4bd85fc3a")); /*var songSet = new SongSet * { * Set = request.SongSet[0].Set, * }; * * context.SongSet.Add(songSet); */ //sca() returns number of saved changes var success = await context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem with saving changes!"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var song = await context.Songs.FindAsync(request.Id); var songArray = song.SongPerformer.ToArray(); var songSetArray = song.SongSet.ToArray(); if (song == null) { throw new RestException(HttpStatusCode.NotFound, new { Song = "Not found!" }); } //optional edits song.Name = request.Name ?? song.Name; song.Genre = request.Genre; song.Text = request.Text ?? song.Text; if (songArray[0].PerformerId != request.SongPerformer[0].Performer.Id) { var removeObject = song.SongPerformer.ToArray().ElementAt(0); song.SongPerformer.Remove(removeObject); var songPerformer = new SongPerformer { SongId = song.Id, PerformerId = request.SongPerformer[0].Performer.Id, }; song.SongPerformer.Add(songPerformer); } if (songSetArray[0].SetId != request.SongSet[0].Set.Id) { var removeObject = song.SongSet.ToArray().ElementAt(0); song.SongSet.Remove(removeObject); var songSet = new SongSet { SongId = song.Id, SetId = request.SongSet[0].Set.Id, }; song.SongSet.Add(songSet); } //song.SongPerformer = request.SongPerformer ?? song.SongPerformer; //song.SongSet = request.SongSet ?? song.SongSet; //sca() returns number of saved changes var success = await context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem with saving changes!"); }