Skip to content

Commit

Permalink
add: problem service
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicaker committed Jul 25, 2018
1 parent 65f4b90 commit ac1eb6b
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 16 deletions.
5 changes: 5 additions & 0 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Config(object):
LOG_FORMAT = '%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s'
LOG_DATE_FORMAT = "%a %d %b %Y %H:%M:%S"

# 分页设置
PAGE_SMALL = 10
PAGE_MEDIUM = 25
PAGE_LARGE = 50


class TestConfig(Config):
"""
Expand Down
Binary file modified config/config.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion controller/leetcode_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__author__ = 'Jiateng Liang'
from common.exception import api
from flask import Blueprint, jsonify
from service.leetcode_service import LeetCodeService
from service.problem_service import LeetCodeService
from bootstrap_init import app

leetcode_bp = Blueprint('leetcode_bp', __name__)
Expand Down
5 changes: 5 additions & 0 deletions model/leetcode_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Difficulty(Enum):
HARD: '困难',
}

ALL = [EASY, MEDIUM, HARD]

@labels
class IsLocked(Enum):
"""
Expand All @@ -54,6 +56,7 @@ class IsLocked(Enum):
UNLOCKED: '解锁',
LOCKED: '上锁',
}
ALL = [UNLOCKED, LOCKED]

@labels
class Type(Enum):
Expand All @@ -67,3 +70,5 @@ class Type(Enum):
ALGO: '算法',
DB: '数据库',
}

ALL = [ALGO, DB]
Binary file modified model/leetcode_problems.pyc
Binary file not shown.
15 changes: 0 additions & 15 deletions service/leetcode_service.py

This file was deleted.

Binary file removed service/leetcode_service.pyc
Binary file not shown.
148 changes: 148 additions & 0 deletions service/problem_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# coding=utf-8
'题目服务'
from common.exception import ServiceException, ErrorCode
from model.leetcode_problems import LeetcodeProblem

__author__ = 'Jiateng Liang'


class ProblemService(object):

@staticmethod
def get_problem_by_lid(lid):
"""
获取题目信息
:param lid: 题号
:return:
"""

problem = LeetcodeProblem.query.filter(LeetcodeProblem.lid == lid).first()
if not problem:
raise ServiceException(ErrorCode.NOT_FOUND, u'题号=%s不存在' % lid)
return problem

@staticmethod
def get_problem_by_qid(qid):
"""
获取题目信息
:param qid: leetcode题目id
:return:
"""
problem = LeetcodeProblem.query.filter(LeetcodeProblem.qid == qid).first()
if not problem:
raise ServiceException(ErrorCode.NOT_FOUND, u'题目qid=%s不存在' % qid)
return problem

@staticmethod
def list_problems_order_by_lid_asc(page, page_size, difficulty=100, is_locked=100, type=100):
"""
根据题号升序
:param difficulty:
:param is_locked:
:param type:
:return:
"""
if page <= 0 or page_size <= 0:
raise ServiceException(ErrorCode.PARAM_ERROR, u'page或者page_size参数错误')
# 过滤
filter_list = []
if difficulty in LeetcodeProblem.Difficulty.ALL.value:
filter_list.append(LeetcodeProblem.difficulty == difficulty)
if is_locked in LeetcodeProblem.IsLocked.ALL.value:
filter_list.append(LeetcodeProblem.IsLocked == is_locked)
if type in LeetcodeProblem.Type.ALL.value:
filter_list.append(LeetcodeProblem.Type == type)

res = LeetcodeProblem.query.filter(*filter_list).order_by(LeetcodeProblem.lid.asc()).slice(
(page - 1) * page_size,
page * page_size).all()

return res

@staticmethod
def list_problems_order_by_frequency_asc(page, page_size, difficulty=100, is_locked=100, type=100):
"""
根据频率升序
:param difficulty:
:param is_locked:
:param type:
:return:
"""
if page <= 0 or page_size <= 0:
raise ServiceException(ErrorCode.PARAM_ERROR, u'page或者page_size参数错误')
# 过滤
filter_list = []
if difficulty in LeetcodeProblem.Difficulty.ALL.value:
filter_list.append(LeetcodeProblem.difficulty == difficulty)
if is_locked in LeetcodeProblem.IsLocked.ALL.value:
filter_list.append(LeetcodeProblem.IsLocked == is_locked)
if type in LeetcodeProblem.Type.ALL.value:
filter_list.append(LeetcodeProblem.Type == type)

res = LeetcodeProblem.query.filter(*filter_list).order_by(LeetcodeProblem.frequency.asc()).slice(
(page - 1) * page_size,
page * page_size).all()

return res

@staticmethod
def list_problems_order_by_frequency_desc(page, page_size, difficulty=100, is_locked=100, type=100):
"""
根据频率降序
:param difficulty:
:param is_locked:
:param type:
:return:
"""
if page <= 0 or page_size <= 0:
raise ServiceException(ErrorCode.PARAM_ERROR, u'page或者page_size参数错误')
# 过滤
filter_list = []
if difficulty in LeetcodeProblem.Difficulty.ALL.value:
filter_list.append(LeetcodeProblem.difficulty == difficulty)
if is_locked in LeetcodeProblem.IsLocked.ALL.value:
filter_list.append(LeetcodeProblem.IsLocked == is_locked)
if type in LeetcodeProblem.Type.ALL.value:
filter_list.append(LeetcodeProblem.Type == type)

res = LeetcodeProblem.query.filter(*filter_list).order_by(LeetcodeProblem.frequency.desc()).slice(
(page - 1) * page_size,
page * page_size).all()

return res

@staticmethod
def search_problems_by_title(title, page, page_size):
"""
根据名称模糊匹配
:param title:
:param page:
:param page_size:
:return:
"""
if page <= 0 or page_size <= 0:
raise ServiceException(ErrorCode.PARAM_ERROR, u'page或者page_size参数错误')
res = LeetcodeProblem.query.filter(LeetcodeProblem.title.like('%' + title + '%')).order_by(
LeetcodeProblem.lid.asc()).slice(
(page - 1) * page_size,
page * page_size).all()
return res

@staticmethod
def search_problems_by_content(content, page, page_size):
"""
根据内容模糊匹配
:param title:
:param page:
:param page_size:
:return:
"""
if page <= 0 or page_size <= 0:
raise ServiceException(ErrorCode.PARAM_ERROR, u'page或者page_size参数错误')
res = LeetcodeProblem.query.filter(LeetcodeProblem.desc.like('%' + content + '%')).order_by(
LeetcodeProblem.lid.asc()).slice(
(page - 1) * page_size,
page * page_size).all()
return res


0 comments on commit ac1eb6b

Please sign in to comment.