public async Task AddUserAsync(long telegramUserId, string lastFmUsername)
        {
            await context.Users.AddAsync(new User
            {
                TelegramUserId = telegramUserId,
                LastfmUsername = lastFmUsername,
            });

            await context.SaveChangesAsync();
        }
    public async Task <SomeEntity> UpdateSomeEntity(SomeEntity updatedSomeEntity)
    {
        _myDbContext.UpdateGraph(updatedSomeEntity, map => map.OwnedCollection(p => p.SomeChildCollection));
        await _myDbContext.SaveChangesAsync();

        return(updatedSomeEntity);
    }
Beispiel #3
0
        public async Task <IActionResult> InsertUser([FromBody] InsertUser user)
        {
            var entity = new User()
            {
                FirstName      = user.FirstName,
                LastName       = user.LastName,
                Email          = user.Email,
                IsActive       = user.IsActive,
                IsInternalUser = user.IsInternalUser,
            };

            entity.InvitationUrl = !user.IsInternalUser ? _invitationUrl.CreateInvittionUrl(entity) : string.Empty;

            _context.User.Add(entity);

            await _context.SaveChangesAsync(new CancellationToken());

            return(Ok());
        }
Beispiel #4
0
        public async Task <Artist> AddArtistAsync(string artistName)
        {
            var artist = await context.Artists.FirstOrDefaultAsync(a => a.Name.Equals(artistName));

            if (artist != null)
            {
                return(artist);
            }

            artist = new Artist()
            {
                Name = artistName
            };

            await context.Artists.AddAsync(artist);

            await context.SaveChangesAsync();

            return(artist);
        }
        public async Task AddSpotifyTrackAsync(Artist artist, string track, string url)
        {
            if (artist == null || string.IsNullOrEmpty(url) || string.IsNullOrEmpty(track))
            {
                return;
            }

            string formattedTrack = track.Length > 255
                ? track.Substring(0, 255)
                : track;

            var existingTrack = await context.SpotifyTracks.FirstOrDefaultAsync(t =>
                                                                                t.Track == formattedTrack &&
                                                                                t.Artist.Name == artist.Name);

            if (existingTrack != null)
            {
                return;
            }

            var spotifyTrack = new SpotifyTrack
            {
                Url    = url,
                Artist = artist,
                Track  = formattedTrack
            };

            await context.SpotifyTracks.AddAsync(spotifyTrack);

            try
            {
                await context.SaveChangesAsync();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }
        }
 public async Task <int> RemoveAsync(IBooks entity)
 {
     DbContext.Books.Remove(await DbContext.Books.FindAsync(entity.BookId));
     return(await DbContext.SaveChangesAsync());
 }
Beispiel #7
0
        /// <summary>
        /// Add a new instrument
        /// </summary>
        /// <param name="instrument"></param>
        /// <param name="saveChanges">Set to true if saving to db should be done.</param>
        /// <returns>True if the insertion or update succeeded. False if it did not.</returns>
        /// <exception cref="ArgumentException"></exception>
        public async Task <Instrument> AddInstrument(Instrument instrument, bool saveChanges = true)
        {
            //Check if an instrument with these unique constraints already exists
            var existingInstrument = Context.Instruments.SingleOrDefault(x =>
                                                                         (x.ID == instrument.ID) ||
                                                                         (x.Symbol == instrument.Symbol &&
                                                                          x.DatasourceID == instrument.DatasourceID &&
                                                                          x.ExchangeID == instrument.ExchangeID &&
                                                                          x.Expiration == instrument.Expiration &&
                                                                          x.Type == instrument.Type));

            if (existingInstrument != null)
            {
                //throw new ArgumentException("Unique constraint violation");
            }

            ValidateInstrument(instrument);

            //All this stuff is detached, so we need to get the attached objects
            instrument.Datasource      = Context.GetAttachedEntity(instrument.Datasource);
            instrument.Exchange        = Context.GetAttachedEntity(instrument.Exchange);
            instrument.PrimaryExchange = Context.GetAttachedEntity(instrument.PrimaryExchange);
            instrument.Tags            = instrument.Tags != null
                ? new List <Tag>(instrument.Tags.Select(Context.GetAttachedEntity).ToList())
                : new List <Tag>();

            //If necessary, load sessions from teplate or exchange
            if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange != null)
            {
                instrument.Sessions = instrument.Exchange.Sessions.Select(x => x.ToInstrumentSession()).ToList();
            }
            else if (instrument.SessionsSource == SessionsSource.Exchange && instrument.Exchange == null)
            {
                instrument.SessionsSource = SessionsSource.Custom;
                instrument.Sessions       = new List <InstrumentSession>();
            }
            else if (instrument.SessionsSource == SessionsSource.Template)
            {
                instrument.Sessions = new List <InstrumentSession>();
                var template = Context.SessionTemplates.Include(x => x.Sessions).FirstOrDefault(x => x.ID == instrument.SessionTemplateID);
                if (template != null)
                {
                    foreach (TemplateSession s in template.Sessions)
                    {
                        instrument.Sessions.Add(s.ToInstrumentSession());
                    }
                }
            }

            //Continuous future requires a bit of a workaround
            ContinuousFuture tmpCf = null;

            if (instrument.IsContinuousFuture)
            {
                tmpCf = instrument.ContinuousFuture; //EF can't handle circular references, so we hack around it
                instrument.ContinuousFuture   = null;
                instrument.ContinuousFutureID = null;
            }

            Context.Instruments.Add(instrument);
            if (saveChanges)
            {
                await Context.SaveChangesAsync().ConfigureAwait(false);
            }

            if (tmpCf != null)
            {
                tmpCf.UnderlyingSymbol = Context.GetAttachedEntity(tmpCf.UnderlyingSymbol);

                instrument.ContinuousFuture              = tmpCf;
                instrument.ContinuousFuture.Instrument   = instrument;
                instrument.ContinuousFuture.InstrumentID = instrument.ID.Value;
                if (saveChanges)
                {
                    await Context.SaveChangesAsync().ConfigureAwait(false);
                }
            }

            _logger.Info($"Instrument Manager: successfully added instrument {instrument}");

            return(instrument);
        }
Beispiel #8
0
 public async Task <int> RemoveAsync(IElectronics entity)
 {
     DbContext.Electronics.Remove(await DbContext.Electronics.FindAsync(entity.ElectronicPartId));
     return(await DbContext.SaveChangesAsync());
 }
 public async Task <int> RemoveAsync(ISport entity)
 {
     DbContext.Sports.Remove(await DbContext.Sports.FindAsync(entity.SportItemId));
     return(await DbContext.SaveChangesAsync());
 }
Beispiel #10
0
 public async Task <int> AddAsync(TEntity entity)
 {
     Context.Set <TEntity>().Add(entity);
     return(await Context.SaveChangesAsync());
 }
Beispiel #11
0
 public async Task <int> RemoveAsync(IMusic entity)
 {
     DbContext.Musics.Remove(await DbContext.Musics.FindAsync(entity.MusicPartId));
     return(await DbContext.SaveChangesAsync());
 }