Exemple #1
0
        public async Task <int> Post([FromBody] SetWithSongs value)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                Set set = new Set()
                {
                    Venue = value.Venue,
                    Date  = value.Date
                };
                await _context.Sets.AddAsync(set);

                await _context.SaveChangesAsync();

                var relationships = from song in value.Songs
                                    select new SetSongRelationship()
                {
                    Order  = value.Songs.IndexOf(song),
                    SetId  = set.Id,
                    SongId = song.Id
                };
                await _context.SetSongRelationships.AddRangeAsync(relationships);

                await _context.SaveChangesAsync();

                transaction.Commit();
                return(set.Id);
            }
        }
Exemple #2
0
        public async Task Put(int id, [FromBody] SetWithSongs value)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                Set set = new Set()
                {
                    Id    = id,
                    Venue = value.Venue,
                    Date  = value.Date
                };
                var dbSongs = _context.SetSongRelationships.Where(x => x.SetId == id);
                _context.SetSongRelationships.RemoveRange(dbSongs);
                var relationships = from song in value.Songs
                                    select new SetSongRelationship()
                {
                    SetId  = id,
                    SongId = song.Id,
                    Order  = value.Songs.IndexOf(song)
                };
                await _context.SaveChangesAsync();

                _context.Sets.Update(set);
                await _context.SetSongRelationships.AddRangeAsync(relationships);

                await _context.SaveChangesAsync();

                transaction.Commit();
            }
        }
Exemple #3
0
        public async Task <SetWithSongs> Get(int id)
        {
            var set = await _context.LoadSet(id);

            var setWithSongs = new SetWithSongs
            {
                Id    = id,
                Date  = set.Date,
                Venue = set.Venue,
                Songs = await _context.LoadSongsForSet(id)
            };

            return(setWithSongs);
        }