Beispiel #1
0
        public TopicDo ApiGetTopic(int id, string token)
        {
            ApiTokenVerifier tokenVerifier = new ApiTokenVerifier(token);
            bool hasValidToken = tokenVerifier.VerifyToken();

            if (hasValidToken)
            {
                MiscDataContext db = new MiscDataContext(DbConnectionString.Value);

                FansiteApiToken fsToken = (from t in db.FansiteApiTokens
                                           where t.TokenId == tokenVerifier.TokenId
                                           && !t.Disabled
                                           select t).Single();

                if (fsToken.LastResetDate != null)
                {
                    TimeSpan diff = DateTime.UtcNow - fsToken.LastResetDate.Value;
                    if (diff.TotalHours >= 24)
                    {
                        fsToken.RequestCount = 0;
                        fsToken.LastResetDate = DateTime.UtcNow;
                    }
                }
                else
                {
                    fsToken.RequestCount = 0;
                    fsToken.LastResetDate = DateTime.UtcNow;
                }
                if (fsToken.RequestCount < fsToken.RequestLimit)
                {
                    fsToken.RequestCount++;
                    db.SubmitChanges();

                    TopicDo topic = GetTopic(id);
                    return topic;
                }
                else
                {
                    throw new BpApiException(ApiStatusCode.Forbidden);
                }

            }
            else
            {
                throw new BpApiException(ApiStatusCode.Forbidden);
            }
        }
Beispiel #2
0
        public List<ApiTopicDo> ApiGetTopics(string game, string region, string lang, string token)
        {
            foreach (var ch in game)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in region)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in lang)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }

            game = game.ToLower().Trim();
            region = region.ToLower().Trim();
            lang = lang.ToLower().Trim();

            ApiTokenVerifier tokenVerifier = new ApiTokenVerifier(token);
            bool hasValidToken = tokenVerifier.VerifyToken();

            if (hasValidToken)
            {
                using (MiscDataContext db = new MiscDataContext(DbConnectionString.Value))
                {
                    FansiteApiToken fsToken = (from t in db.FansiteApiTokens
                                               where t.TokenId == tokenVerifier.TokenId
                                               && !t.Disabled
                                               select t).Single();

                    if (fsToken.LastResetDate != null)
                    {
                        TimeSpan diff = DateTime.UtcNow - fsToken.LastResetDate.Value;
                        if (diff.TotalHours >= 24)
                        {
                            fsToken.RequestCount = 0;
                            fsToken.LastResetDate = DateTime.UtcNow;
                        }
                    }
                    else
                    {
                        fsToken.RequestCount = 0;
                        fsToken.LastResetDate = DateTime.UtcNow;
                    }
                    if (fsToken.RequestCount < fsToken.RequestLimit)
                    {
                        List<ApiTopicDo> topics = new List<ApiTopicDo>();

                        IQueryable<ApiGetForumTopic> queriableTopics = null;

                        if (!String.IsNullOrEmpty(region) && !String.IsNullOrWhiteSpace(region) && region != "any")
                        {
                            queriableTopics = from t in db.ApiGetForumTopics
                                              where t.RegionAbbreviation == region.ToUpper()
                                              && t.LanguageAbbreviation == lang.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }
                        else
                        {
                            queriableTopics = from t in db.ApiGetForumTopics
                                              where t.LanguageAbbreviation == lang.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }

                        if (!String.IsNullOrEmpty(game) && !String.IsNullOrWhiteSpace(game) && game != "any")
                        {
                            queriableTopics = from t in queriableTopics
                                              where t.GameAbbreviation == game.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }

                        queriableTopics = queriableTopics.Take(10);

                        List<ApiGetForumTopic> results = queriableTopics.ToList();

                        foreach (var result in results)
                        {
                            ApiTopicDo topic = new ApiTopicDo
                            {
                                TopicId = result.Id,
                                SubForumId = result.ForumBoardId,
                                SubForumBlizzardId = result.ForumBoardNo,
                                Game = result.GameAbbreviation.ToLower(),
                                Region = result.RegionAbbreviation.ToLower(),
                                Language = result.LanguageAbbreviation.ToLower(),
                                SubForumName = result.BoardName,
                                BlizzardThreadNumber = result.ThreadNumber,
                                TopicTitle = result.ThreadTitle,
                                AuthorOfThread = result.AuthorOfThread,
                                FullDateTimeUtc = result.LastPostDate,
                                UpdateStamp = result.LastModifiedStamp
                            };

                            topics.Add(topic);
                        }

                        return topics;

                    }
                    else
                    {
                        throw new BpApiException(ApiStatusCode.Forbidden);
                    }
                }
            }
            else
            {
                throw new BpApiException(ApiStatusCode.Forbidden);
            }
        }