<commit_before>import * as Router from 'koa-router';
import * as Koa from 'koa';

import {IController} from '../libs/controller.interface';
import {RenderCtx} from '../libs/render.class';

import {IUserModel, User} from '../models/user.model';

class UserController implements IController {

  public user: IUserModel;
  private Router = new Router({
    prefix: '/users',
  });
  private renderCtx = new RenderCtx();

  constructor() {}

  // We need use bind for transmission context
  public router(): Router {
    return this.Router
      .param('userName', this.findUser.bind(this))
      .post('/', this.create.bind(this))
      .get('/', this.index.bind(this))
      .get('/:userName', this.show.bind(this))
      .put('/:userName', this.update.bind(this));
  }

  private async create(ctx: Koa.Context) {
    const user: IUserModel = new User({
      name: ctx.request.body.name,
      username: ctx.request.body.username,
      email: ctx.request.body.email,
      password: <PASSWORD>,
    });
    await user.save();
    this.renderCtx
      .renderSuccess(
        ctx,
        201,
        'users',
        user.toPlainObject());
  }

  private async index(ctx: Koa.Context) {
    const users: Array<IUserModel> = await User.find({});
    const usersData = users.map((user: IUserModel) => user.toPlainObject());
    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        usersData);
  }

  private async show(ctx: Koa.Context) {
    const user: IUserModel = this.user;
    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        user.toPlainObject());
  }

  private async update(ctx: Koa.Context) {
    const user: IUserModel = this.user;
    await user.update({name: ctx.request.body.name});

    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        user.toPlainObject());
  }

  private async findUser(username, ctx: Koa.Context, next) {
    this.user = await User.findOne({username: username});

    if (!this.user) {
      return ctx.throw(404);
    }
    return next();
  }
}

export default new UserController().router();<commit_msg>add Destroy method for UserController<commit_after>import * as Router from 'koa-router';
import * as Koa from 'koa';

import {IController} from '../libs/controller.interface';
import {RenderCtx} from '../libs/render.class';

import {IUserModel, User} from '../models/user.model';

class UserController implements IController {

  public user: IUserModel;
  private Router = new Router({
    prefix: '/users',
  });
  private renderCtx = new RenderCtx();

  constructor() {}

  // We need use bind for transmission context
  public router(): Router {
    return this.Router
      .param('userName', this.findUser.bind(this))
      .post('/', this.create.bind(this))
      .get('/', this.index.bind(this))
      .get('/:userName', this.show.bind(this))
      .put('/:userName', this.update.bind(this))
      .delete('/:userName', this.destroy.bind(this));
  }

  private async create(ctx: Koa.Context) {
    const user: IUserModel = new User({
      name: ctx.request.body.name,
      username: ctx.request.body.username,
      email: ctx.request.body.email,
      password: ctx.<PASSWORD>.body.password,
    });
    await user.save();
    this.renderCtx
      .renderSuccess(
        ctx,
        201,
        'users',
        user.toPlainObject());
  }

  private async index(ctx: Koa.Context) {
    const users: Array<IUserModel> = await User.find({});
    const usersData = users.map((user: IUserModel) => user.toPlainObject());
    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        usersData);
  }

  private async show(ctx: Koa.Context) {
    const user: IUserModel = this.user;
    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        user.toPlainObject());
  }

  private async update(ctx: Koa.Context) {
    let user: IUserModel = this.user;
    await user.update({
      name: ctx.request.body.name,
      email: ctx.request.body.email,
    });
    user = await User.findOne({username: user.username});

    this.renderCtx
      .renderSuccess(
        ctx,
        200,
        'users',
        user.toPlainObject());
  }

  private async destroy(ctx: Koa.Context) {
    const user: IUserModel = this.user;
    await user.remove();

    this.renderCtx
      .renderSuccess(
        ctx,
        202,
      'user',
      user.toPlainObject());
  }

  private async findUser(username, ctx: Koa.Context, next) {
    this.user = await User.findOne({username: username});

    if (!this.user) {
      return ctx.throw(404);
    }
    return next();
  }
}

export default new UserController().router();
{'fraction_non_alphanumeric': 0.14355179704016913, 'fraction_numerical': 0.0069767441860465115, 'mean_word_length': 2.5438202247191013, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7300360', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1484, 'n_words': 346}