Cloud Defense Logo

Products

Solutions

Company

Book A Live Demo

Top 10 Examples of "koa-swagger-decorator in functional component" in JavaScript

Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'koa-swagger-decorator' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.

// load user by id
        const user: User = await userRepository.findOne(+ctx.params.id || 0);

        if (user) {
            // return OK status code and loaded user object
            ctx.status = 200;
            ctx.body = user;
        } else {
            // return a BAD REQUEST status code and error message
            ctx.status = 400;
            ctx.body = 'The user you are trying to retrieve doesn\'t exist in the db';
        }

    }

    @request('post', '/users')
    @summary('Create a user')
    @body(userSchema)
    public static async createUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // build up entity user to be saved
        const userToBeSaved: User = new User();
        userToBeSaved.name = ctx.request.body.name;
        userToBeSaved.email = ctx.request.body.email;

        // validate user entity
        const errors: ValidationError[] = await validate(userToBeSaved); // errors is an array of validation errors

        if (errors.length > 0) {
ctx.status = 400;
            ctx.body = errors;
        } else if (await userRepository.findOne({ email: userToBeSaved.email })) {
            // return BAD REQUEST status code and email already exists error
            ctx.status = 400;
            ctx.body = 'The specified e-mail address already exists';
        } else {
            // save the user contained in the POST body
            const user = await userRepository.save(userToBeSaved);
            // return CREATED status code and updated user
            ctx.status = 201;
            ctx.body = user;
        }
    }

    @request('put', '/users/{id}')
    @summary('Update a user')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    @body(userSchema)
    public static async updateUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // update the user by specified id
        // build up entity user to be updated
        const userToBeUpdated: User = new User();
        userToBeUpdated.id = +ctx.params.id || 0; // will always have a number, this will avoid errors
        userToBeUpdated.name = ctx.request.body.name;
        userToBeUpdated.email = ctx.request.body.email;
import { BaseContext } from 'koa';
import { getManager, Repository, Not, Equal } from 'typeorm';
import { validate, ValidationError } from 'class-validator';
import { request, summary, path, body, responsesAll, tagsAll } from 'koa-swagger-decorator';
import { User, userSchema } from '../entity/user';

@responsesAll({ 200: { description: 'success'}, 400: { description: 'bad request'}, 401: { description: 'unauthorized, missing/wrong jwt token'}})
@tagsAll(['User'])
export default class UserController {

    @request('get', '/users')
    @summary('Find all users')
    public static async getUsers(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }

    @request('get', '/users/{id}')
    @summary('Find user by id')
ctx.body = 'The user you are trying to update doesn\'t exist in the db';
        } else if (await userRepository.findOne({ id: Not(Equal(userToBeUpdated.id)), email: userToBeUpdated.email })) {
            // return BAD REQUEST status code and email already exists error
            ctx.status = 400;
            ctx.body = 'The specified e-mail address already exists';
        } else {
            // save the user contained in the PUT body
            const user = await userRepository.save(userToBeUpdated);
            // return CREATED status code and updated user
            ctx.status = 201;
            ctx.body = user;
        }

    }

    @request('delete', '/users/{id}')
    @summary('Delete user by id')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    public static async deleteUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository = getManager().getRepository(User);

        // find the user by specified id
        const userToRemove: User = await userRepository.findOne(+ctx.params.id || 0);
        if (!userToRemove) {
            // return a BAD REQUEST status code and error message
            ctx.status = 400;
            ctx.body = 'The user you are trying to delete doesn\'t exist in the db';
        } else if (+ctx.state.user.id !== userToRemove.id) {
@request('get', '/users')
    @summary('Find all users')
    public static async getUsers(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }

    @request('get', '/users/{id}')
    @summary('Find user by id')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    public static async getUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load user by id
        const user: User = await userRepository.findOne(+ctx.params.id || 0);

        if (user) {
            // return OK status code and loaded user object
            ctx.status = 200;
            ctx.body = user;
const user: User = await userRepository.findOne(+ctx.params.id || 0);

        if (user) {
            // return OK status code and loaded user object
            ctx.status = 200;
            ctx.body = user;
        } else {
            // return a BAD REQUEST status code and error message
            ctx.status = 400;
            ctx.body = 'The user you are trying to retrieve doesn\'t exist in the db';
        }

    }

    @request('post', '/users')
    @summary('Create a user')
    @body(userSchema)
    public static async createUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // build up entity user to be saved
        const userToBeSaved: User = new User();
        userToBeSaved.name = ctx.request.body.name;
        userToBeSaved.email = ctx.request.body.email;

        // validate user entity
        const errors: ValidationError[] = await validate(userToBeSaved); // errors is an array of validation errors

        if (errors.length > 0) {
            // return BAD REQUEST status code and errors array
@summary('Find all users')
    public static async getUsers(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }

    @request('get', '/users/{id}')
    @summary('Find user by id')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    public static async getUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load user by id
        const user: User = await userRepository.findOne(+ctx.params.id || 0);

        if (user) {
            // return OK status code and loaded user object
            ctx.status = 200;
            ctx.body = user;
        } else {
} else if (await userRepository.findOne({ id: Not(Equal(userToBeUpdated.id)), email: userToBeUpdated.email })) {
            // return BAD REQUEST status code and email already exists error
            ctx.status = 400;
            ctx.body = 'The specified e-mail address already exists';
        } else {
            // save the user contained in the PUT body
            const user = await userRepository.save(userToBeUpdated);
            // return CREATED status code and updated user
            ctx.status = 201;
            ctx.body = user;
        }

    }

    @request('delete', '/users/{id}')
    @summary('Delete user by id')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    public static async deleteUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository = getManager().getRepository(User);

        // find the user by specified id
        const userToRemove: User = await userRepository.findOne(+ctx.params.id || 0);
        if (!userToRemove) {
            // return a BAD REQUEST status code and error message
            ctx.status = 400;
            ctx.body = 'The user you are trying to delete doesn\'t exist in the db';
        } else if (+ctx.state.user.id !== userToRemove.id) {
            // check user's token id and user id are the same
ctx.body = errors;
        } else if (await userRepository.findOne({ email: userToBeSaved.email })) {
            // return BAD REQUEST status code and email already exists error
            ctx.status = 400;
            ctx.body = 'The specified e-mail address already exists';
        } else {
            // save the user contained in the POST body
            const user = await userRepository.save(userToBeSaved);
            // return CREATED status code and updated user
            ctx.status = 201;
            ctx.body = user;
        }
    }

    @request('put', '/users/{id}')
    @summary('Update a user')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    @body(userSchema)
    public static async updateUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // update the user by specified id
        // build up entity user to be updated
        const userToBeUpdated: User = new User();
        userToBeUpdated.id = +ctx.params.id || 0; // will always have a number, this will avoid errors
        userToBeUpdated.name = ctx.request.body.name;
        userToBeUpdated.email = ctx.request.body.email;
import { BaseContext } from 'koa';
import { getManager, Repository, Not, Equal } from 'typeorm';
import { validate, ValidationError } from 'class-validator';
import { request, summary, path, body, responsesAll, tagsAll } from 'koa-swagger-decorator';
import { User, userSchema } from '../entity/user';

@responsesAll({ 200: { description: 'success'}, 400: { description: 'bad request'}, 401: { description: 'unauthorized, missing/wrong jwt token'}})
@tagsAll(['User'])
export default class UserController {

    @request('get', '/users')
    @summary('Find all users')
    public static async getUsers(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }

    @request('get', '/users/{id}')
    @summary('Find user by id')
    @path({

Is your System Free of Underlying Vulnerabilities?
Find Out Now