Esempio n. 1
0
        // GET api/EntryHasAny
        public async Task<int> Get()
        {
            var currentUser = await this.GetUserInfoAsync();

            using (var ctx = new LinquaContext())
            {
                var result = await ctx.Entries.CountAsync(x => (x.UserId == currentUser.UserEntity.Id || x.ClientAppSpecificUserId == currentUser.AppSpecificMicrosoftUserId) && !x.Deleted);

                return result;
            }
        }
Esempio n. 2
0
        // POST api/EntryLookup
        public async Task<Entry> Post(string entryText, string excludeId)
        {
            //Services.Log.Info("Hello from custom controller!");

            using (var ctx = new LinquaContext())
            {
                var foundEntries = await ctx.Entries
                                            .Where(x => x.Text == entryText && x.Definition != null && (string.IsNullOrEmpty(excludeId) || !Equals(x.Id, excludeId)))
                                            .ToListAsync();

                if (foundEntries != null && foundEntries.Count > 0)
                {
                    return foundEntries[0];
                }
            }

            return null;
        }
Esempio n. 3
0
        // GET api/RandomEntry
        public async Task<IEnumerable<Entry>> Get(int number)
        {
            var currentUser = await this.GetUserInfoAsync();

            var indexGenerator = new Random((int)DateTime.UtcNow.Ticks);

            using (var ctx = new LinquaContext())
            {
                var foundEntries = await ctx.Entries
                                            .Where(x => (x.UserId == currentUser.UserEntity.Id || x.ClientAppSpecificUserId == currentUser.AppSpecificMicrosoftUserId) && !x.Deleted && !x.IsLearnt)
                                            .ToListAsync();

                if (foundEntries != null && foundEntries.Count > 0)
                {
                    var randomEntries = new List<Entry>();
                    var excludeIndices = new HashSet<int>();

                    number = Math.Min(number, foundEntries.Count);

                    for (var i = 0; i < number; i++)
                    {
                        int randomIndex;
                        do
                        {
                            randomIndex = indexGenerator.Next(0, foundEntries.Count - 1);
                        } while (excludeIndices.Contains(randomIndex));

                        excludeIndices.Add(randomIndex);

                        var randomEntry = foundEntries[randomIndex];

                        randomEntries.Add(randomEntry);
                    }

                    var result = randomEntries.ToArray();

                    return result;
                }
            }

            return null;
        }
Esempio n. 4
0
        private static User GetUserEntity(ProviderCredentials creds)
        {
            var userId = creds.UserClaims.Single(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var cacheKey = UserCacheKeyPrefix + userId;

            User user = (User)Cache.Get(cacheKey);

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

            lock (UserLoadLock)
            {
                using (var dbContext = new LinquaContext())
                {
                    user = dbContext.Users.SingleOrDefault(x => x.MicrosoftAccountId == userId);

                    const int AttemptsCount = 3;
                    int attempt = 1;

                    while (user == null)
                    {
                        user = new User(Guid.NewGuid(), userId, creds.UserClaims.SingleOrDefault(x => x.Type == ClaimTypes.Email)?.Value);

                        dbContext.Users.Add(user);

                        try
                        {
                            dbContext.SaveChanges();
                        }
                        catch (Exception)
                        {
                            if (attempt > AttemptsCount)
                            {
                                throw;
                            }

                            dbContext.Users.Remove(user);

                            attempt++;
                        }

                        user = dbContext.Users.SingleOrDefault(x => x.MicrosoftAccountId == userId);
                    }
                }
            }

            Cache.Set(cacheKey, user, DateTimeOffset.Now.AddHours(12));

            return user;
        }