{"size": 1284, "ext": "js", "max_stars_repo_path": "packages/admin/app/Controllers/ApplicationController.src.js", "max_stars_repo_name": "batosai/Tetra", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/admin/app/Controllers/ApplicationController.src.js", "max_issues_repo_name": "batosai/Tetra", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/admin/app/Controllers/ApplicationController.src.js", "max_forks_repo_name": "batosai/Tetra", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5263157895, "max_line_length": 144, "alphanum_fraction": 0.5856697819}
const { Controller } = require('@tetrajs/router').decorators
const { security, middleware } = require('@tetrajs/core')
const { SitesService } = require('../Services')

// Todo ne pas faire de .src garder en build mais lors de l'ecriture du .build search and replace require *Controller.js by *Controller.js.build

@Controller()
class ApplicationController {
  constructor() {
    this.middlewares = [
      {
        actions: [
          middleware('require-authentication'),
          security.csrf.protection,
          security.csrf.token,
          this.setSite,
          this.setLocale,
          middleware('assets')
        ],
      },
    ]
  }

  // @Action()
  // @Action({ type: 'json' })
  // @Request()
  // @Response()
  // @Next()
  // @Json()
  // @Xml()
  // @Stream()
  // @Html()
  // async create(req, res, next) {

  // }

  async setSite(req, res, next) {
    const site = await SitesService.findOne()
    res.locals.site = site
    req.site = site
    await next()
  }

  async setLocale(req, res, next) {
    if (
      req.query['lang'] === undefined &&
      req.cookies['language'] === undefined
    ) {
      const site = await SitesService.findOne()
      res.setLocale(site.language)
    }
    await next()
  }
}

module.exports = ApplicationController
