from datetime import datetime
from application.app import db
from models.{{ config['name'] }} import {{ config['name'] | letterUpper }}Model
<<<<<<< HEAD
from webcreator.utils import ResponseCode, response_result
=======
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca

class {{ config['name'] | letterUpper }}Resource(object):
    def __init__(self):
        super().__init__()

    def get(self, uuid, params):
        # handle business
<<<<<<< HEAD
        filters = []
        result = {{ config['name'] | letterUpper }}Model.query.filter(*filters).order_by({{ config['name'] | letterUpper }}Model.areaId).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)
=======
        filters = [{{ config['name'] | letterUpper }}Model.is_delete==False, {{ config['name'] | letterUpper }}Model.uuid==uuid]
        result = {{ config['name'] | letterUpper }}Model.query.filter(*filters).first()
        if result:
            return (True, result)
        return (False, result)
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca

    def getList(self, params):
        # handle business
        logger.warn(params)
        filters = [{{ config['name'] | letterUpper }}Model.is_delete==False]
        result = {{ config['name'] | letterUpper }}Model.query.filter(*filters).order_by({{ config['name'] | letterUpper }}Model.create_at).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)

        return (True, result.items, result.total)

<<<<<<< HEAD
    def post(self, params, jwt=None):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.filter({{ config['name'] | letterUpper }}Model.areaName == params.get('areaName')).first()
        if result and result.is_delete:
            result.is_delete = False
            result.update_by = jwt['id']
            result.update_date = datetime.now()
            db.session.commit()
            return response_result(ResponseCode.OK)
        elif result and result.is_delete == False:
            return response_result(ResponseCode.EXISTS_ERROR)
=======
    def post(self, params, jwt={}):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.filter({{ config['name'] | letterUpper }}Model.{{ config["model"]["fields"][0]["name"] }} == params.get('{{ config["model"]["fields"][0]["name"] }}')).first()
        if result and result.is_delete:
            result.is_delete = False
            result.update_by = jwt.get("id", "")
            result.update_date = datetime.now()
            db.session.commit()
            return (True, None)
        elif result and result.is_delete == False:
            return (False, "record code exists")
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca

        result = {{ config['name'] | letterUpper }}Model(**params)
        db.session.add(result)
        db.session.commit()
<<<<<<< HEAD
        return response_result(ResponseCode.OK)

    def put(self, id, params, jwt=None):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.get(id)
        if not result: return response_result(ResponseCode.NO_DATA)
=======
        return (True, None)

    def put(self, uuid, params, jwt={}):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.filter({{ config['name'] | letterUpper }}Model.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
            result.update_by = jwt.get("id", "")
            result.update_date = datetime.now()
            db.session.commit()
            return (True, None)
        else:
<<<<<<< HEAD
            return response_result(ResponseCode.PARAM_NULL)

    def delete(self, id, jwt=None):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.get(id)
        if not result: return response_result(ResponseCode.NO_DATA_FOUND)
        else:
            result.update_by = jwt['id']
            result.update_date = datetime.now()
            result.is_delete = True
            db.session.delete(result)
            db.session.commit()
=======
            return (False, "params is null")

    def delete(self, uuid, jwt={}):
        # handle business
        result = {{ config['name'] | letterUpper }}Model.query.filter({{ config['name'] | letterUpper }}Model.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

        result.update_by = jwt.get("id", "")
        result.update_date = datetime.now()
        result.is_delete = True
        db.session.delete(result)
        db.session.commit()
        return (True, None)
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca

{{ config["name"] }}Manager = {{ config['name'] | letterUpper }}Resource()