{"size": 2322, "ext": "js", "max_stars_repo_path": "server/controllers/user.js", "max_stars_repo_name": "alincode/koa2-starter-kit", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/user.js", "max_issues_repo_name": "alincode/koa2-starter-kit", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/controllers/user.js", "max_forks_repo_name": "alincode/koa2-starter-kit", "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": 24.9677419355, "max_line_length": 58, "alphanum_fraction": 0.4379844961}
import _ from 'lodash';
const { validateFields } = require('../utils/fields');
const jwt = require('jsonwebtoken');
var debug = require('debug')('bc:controllers:user');

export default class User {

    async index(ctx) {
        // let req = ctx.request;
        // let limit = req.query.limit;
        try {
            let users = await userService.findAll();
            ctx.body = { users };
        } catch (error) {
            ctx.body = error;
            ctx.status = error.status;
        }
    }

    async show(ctx) {
        try {
            let userId = ctx.state.user.sid;
            let user = await models.User.findById(userId);
            user = user.dataValues;
            user = _.omit(user, ['password']);
            ctx.body = user;
        } catch (error) {
            ctx.body = error;
            ctx.status = error.status;
        }
    }

    async store(ctx) {
        try {
            let req = ctx.request;
            let values = req.body;

            values = _.pick(values, [
                'username',
                'email',
                'password'
            ]);

            validateFields(values, [
                'username',
                'email',
                'password'
            ]);

            let user = await models.User.create(values);
            debug('create success');
            ctx.body = user;
        } catch (error) {
            ctx.body = error;
            ctx.status = error.status;
        }
    }

    async update(ctx) {
        try {
            debug('update success');
            let req = ctx.request;
            let userId = ctx.params.userId;
            let values = req.body;

            values = _.pick(values, [
                'username',
                'email',
                'password'
            ]);

            let options = {
                where : {
                    id : userId
                }
            };
            let user = await models.User.findOne(options);
            user.username = values.username;
            user.email = values.email;
            user.password = values.password;
            await user.save();
            ctx.body = user;
        } catch (error) {
            ctx.body = error;
            ctx.status = error.status;
        }
    }

    async logout(ctx) {

    }
}
