Created
May 24, 2021 15:40
-
-
Save Romakita/d3b460d12f2159f6d49f916c59739a85 to your computer and use it in GitHub Desktop.
Prisma UsersController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {BodyParams, Controller, Get, PathParams, Post} from "@tsed/common"; | |
import {Inject} from "@tsed/di"; | |
import {PrismaService} from "../services/PrismaService"; | |
import {Groups, Required, Email, Returns, Summary} from "@tsed/schema"; | |
import {User} from "@prisma/client"; | |
class UserModel implements User { | |
@Required() | |
@Groups("!creation") | |
id: number; | |
@Required() | |
@Email() | |
email: string; | |
@Required() | |
name: string; | |
} | |
@Controller("/users") | |
export class UsersController { | |
@Inject() | |
protected prisma: PrismaService; | |
@Get("/:id") | |
@Summary("Return a use by his id") | |
@Returns(200, UserModel) | |
async getUser(@PathParams("id") id: number) { | |
return this.prisma.user.findUnique({where: {id}}); | |
} | |
@Post("/") | |
@Summary("Create a new user") | |
@Returns(201, UserModel) | |
async signupUser(@BodyParams() @Groups("creation") user: UserModel) { | |
return this.prisma.user.create({data: user}); | |
} | |
@Get("/") | |
@Summary("Return a list of User") | |
@(Returns(200, Array).Of(UserModel)) | |
getUsers() { | |
return this.prisma.user.findMany(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment