from flask import request, Response
from db.repository import Repository
from db.entities import Layer

repo = Repository()

#region layers

def get():
    return [l.to_serializable_dict() for l in repo.get_layers()]

def get_by_name(name):
    res = repo.get_layer(name)
    if res is not None:
        return res.to_serializable_dict() 
    else:
        return Response(status=404)

def get_by_use_case_and_table(use_case: str, table_name: str):
    return [layer.to_serializable_dict() for layer in repo.get_layers_use_case_and_table(use_case, table_name)]

def get_by_name_and_use_case_and_table(name: str, use_case: str, table_name: str):
    res = repo.get_layers_by_name_and_use_case_and_table(name, use_case, table_name)
    if res is not None:
        return res.to_serializable_dict() 
    else:
        return Response(status=404)

def get_by_use_case(use_case: str):
    res = [layer.to_serializable_dict() for layer in repo.get_layers_by_use_case(use_case)]
    if res is not None and len(res) > 0:
        return res
    else:
        return Response(status=404)

#endregion
