import {normalize} from './params'

export function list({Model, req, res}) {
  const query = normalize({req})
  const include = query.include.map(include => {
    return Model.modelManager.getModel(include)
  })
  return Model
    .findAll({
      order: query.order,
      limit: query.limit,
      offset: query.offset,
      include
    })
    .then(documents => {
      res
        .status(200)
        .body = documents.map(document => document.get({plain: true}))
    })
}

export function create({Model, req, res}) {
  const query = normalize({req})
}

export function read({Model, idParam, idField, req, res}) {
  const query = normalize({req})
  const include = query.include.map(include => {
    return Model.modelManager.getModel(include)
  })
  return Model
    .find({
      where: {
        [idField]: req.params[idParam]
      },
      order: query.order,
      limit: query.limit,
      offset: query.offset,
      include
    })
    .then(document => {
      res
        .status(200)
        .body = document.get({plain: true})
    })
}

export function update({Model, req, res}) {
  const query = normalize({req})
}

export function remove({Model, req, res}) {
  const query = normalize({req})
}

@@ -1,16 +1,19 @@
 import %7B
+%0A  
 normaliz
@@ -13,16 +13,28 @@
 ormalize
+,%0A  include%0A
 %7D from '
@@ -123,110 +123,8 @@
 q%7D)%0A
-  const include = query.include.map(include =%3E %7B%0A    return Model.modelManager.getModel(include)%0A  %7D)%0A
   re
@@ -234,32 +234,96 @@
 t,%0A      include
+: include(%7B%0A        Model,%0A        param: query.include%0A      %7D)
 %0A    %7D)%0A    .the
@@ -311,32 +311,37 @@
  %7D)%0A    %7D)%0A    .
+%0A    
 then(documents =
@@ -633,110 +633,8 @@
 q%7D)%0A
-  const include = query.include.map(include =%3E %7B%0A    return Model.modelManager.getModel(include)%0A  %7D)%0A
   re
@@ -808,24 +808,88 @@
      include
+: include(%7B%0A        Model,%0A        param: query.include%0A      %7D)
 %0A    %7D)%0A    

use include method