from app import app, db, csrf
from app.services import webhook
from app.models.issue import Issue
from app.helpers.application_helper import flash
from app.helpers.topics_helper import TopicsHelper
from app.helpers.users_helper import UsersHelper
from app.helpers.repositories_helper import RepositoriesHelper
from lib.utils import datetime_from_utc
from flask import request, redirect, abort, jsonify
from flask import Blueprint, url_for, render_template
from flask_login import login_required, current_user

views = Blueprint('issues', __name__)
repos_helper = RepositoriesHelper(app)
users_helper = UsersHelper(app)
topics_helper = TopicsHelper()

def create(data):
    issue = data['issue']
    user = users_helper.get_user(issue['user']['id'])
    if user is None:
        user = users_helper.create_user(issue['user'])
        try:
            db.session.add(user)
            db.session.commit()
        except:
            db.session.rollback()
            return None
    repo = repos_helper.get_repo_by_id(data['repository']['id'])
    if repo is None:
        return None
    issue = Issue(user, repo, issue)
    try:
        db.session.add(issue)
        db.session.commit()
    except:
        db.session.rollback()
        return None
    for topic in issue.topics:
        topics_helper.update_issues_count(topic)
    db.session.commit()
    return issue

def update(data):
    action = data['action']
    issue = Issue.query.filter_by(origin_id=data['issue']['id']).first()
    if issue is None:
        issue = create(data)
        if issue is None:
            return False
    try:
        db.session.add(issue)
        db.session.commit()
        if action == 'opened':
            return True
    except:
        db.session.rollback()
        return False
    data = data['issue']
    issue.state = data['state']
    issue.title = data['title']
    issue.body = data['body']
    issue.comments_count = data['comments']
    issue.updated_at = datetime_from_utc(data['updated_at'])
    try:
        db.session.commit()
    except:
        db.session.rollback()
        return False
    return True

@webhook.hook()
def issues(data):
    if update(data):
        return 'ok'
    return 'faild', 500

# only for test add issue
@csrf.exempt
@app.route('/api/issues', methods=['POST'])
def new():
    data = request.get_json()
    return jsonify({ 'success': update(data) })

app.register_blueprint(views)

@@ -17,12 +17,8 @@
 app,
- db,
  csr
@@ -65,227 +65,64 @@
 app.
-models.issue import Issue%0Afrom app.helpers.application_helper import flash%0Afrom app.helpers.topics_helper import TopicsHelper%0Afrom app.helpers.users_helper import UsersHelper%0Afrom app.helpers.repositories_helper
+services import issues_service%0Afrom app.services
  import 
 Repo
@@ -117,17 +117,17 @@
  import 
-R
+r
 epositor
@@ -133,444 +133,163 @@
 ries
-Helper%0Afrom lib.utils import datetime_from_utc%0Afrom flask import request, redirect, abort, jsonify%0Afrom flask import Blueprint, url_for, render_template%0Afrom flask_login import login_required, current_user%0A%0Aviews = Blueprint('issues', __name__)%0Arepos_helper = RepositoriesHelper(app)%0Ausers_helper = UsersHelper(app)%0Atopics_helper = TopicsHelper()%0A%0Adef create(data):%0A    issue = data%5B'issue'%5D%0A    user = users_helper.get_user(issue%5B'user
+_service%0Afrom flask import jsonify, Blueprint, request%0A%0Aviews = Blueprint('issues', __name__)%0A%0Adef update(data):%0A    issue = issues_service.get(data%5B'issue
 '%5D%5B'
@@ -305,15 +305,20 @@
  if 
-user is
+issue is not
  Non
@@ -332,775 +332,237 @@
     
-user = users_helper.create_user(issue%5B'user'%5D)%0A        try:%0A            db.session.add(user)%0A            db.session.commit()%0A        except:%0A            db.session.rollback()%0A            return None%0A    repo = repos_helper.get_repo_by_id(data%5B'repository'%5D%5B'id'%5D)%0A    if repo is None:%0A        return None%0A    issue = Issue(user, repo, issue)%0A    try:%0A        db.session.add(issue)%0A        db.session.commit()%0A    except:%0A        db.session.rollback()%0A        return None%0A    for topic in issue.topics:%0A        topics_helper.update_issues_count(topic)%0A    db.session.commit()%0A    return issue%0A%0Adef update(data):%0A    action = data%5B'action'%5D%0A    issue = Issue.query.filter_by(origin_id=data%5B'issue'%5D%5B'id'%5D).first()%0A    if issue is None:%0A        issue = create(data)%0A    
+issues_service.update(issue, data%5B'issue'%5D)%0A        return True%0A    repo = repositories_service.get_by_id(data%5B'repository'%5D%5B'id'%5D)%0A    if repo is None:%0A        return False%0A    issue = issues_service.create(data%5B'issue'%5D, repo)%0A
     
@@ -575,28 +575,24 @@
 ue is None:%0A
-    
         retu
@@ -671,63 +671,8 @@
 t()%0A
-        if action == 'opened':%0A            return True%0A
     
@@ -738,420 +738,175 @@
     
-data = data%5B'issue'%5D%0A    issue.state = data%5B'state'%5D%0A    issue.title = data%5B'title'%5D%0A    issue.body = data%5B'body'%5D%0A    issue.comments_count = data%5B'comments'%5D%0A    issue.updated_at = datetime_from_utc(data%5B'updated_at'%5D)%0A    try:%0A        db.session.commit()%0A    except:%0A        db.session.rollback()%0A        return False%0A    return True%0A%0A@webhook.hook()%0Adef issues(data):%0A    if update(data):%0A        return 'ok'%0A
+return True%0A%0A@webhook.hook()%0Adef issues(data):%0A    try:%0A        if update(data):%0A            return 'ok'%0A        else:%0A            return 'faild', 500%0A    except:%0A    
     

Update issues webhook