Skip to content
Snippets Groups Projects
Commit 8b0ba582 authored by frickerg's avatar frickerg
Browse files

added user service for demo api

parent 649366a0
Branches
No related tags found
No related merge requests found
Pipeline #402225 failed
export type Role = 'ADMIN' | 'SHINOBI' | 'MED'
export type User = {
id: number
firstName: string
lastName: string
email: string
role: Role
}
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get()
findAll() {
return [];
}
// needs to be higher than :id route
@Get('interns')
findAllInterns() {
return [];
}
@Get(':id')
findById(@Param('id') id: string) {
return { id };
}
@Post()
addUser(@Body() user: {}) {
return user;
}
}
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common'
import { UsersService } from './users.service'
import { Role, User } from './types'
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
findAll(@Query('role') role?: Role) {
return this.usersService.findAll(role)
}
// needs to be higher than :id route
@Get('hokage')
findAllHokage() {
return this.usersService.findAll('ADMIN')
}
@Get(':id')
findById(@Param('id') id: string) {
return this.usersService.findById(+id)
}
@Post()
create(@Body() user: Omit<User, 'id'>) {
if (this.findAll().find((exists) => exists.email === user.email)) {
const msg = 'This user already exists'
console.error('ERROR:', msg)
return { error: 'UserExistsError', msg }
}
return this.usersService.create(user)
}
@Patch(':id')
update(@Param('id') id: string, @Body() userUpdate: Omit<User, 'id'>) {
return this.usersService.update(+id, userUpdate)
}
@Delete(':id')
delete(@Param('id') id: string) {
return this.usersService.delete(+id)
}
}
import { Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {}
import { Injectable } from '@nestjs/common'
import { Role, User } from './types'
@Injectable()
export class UsersService {
private users: User[] = [
{
id: 1,
firstName: 'Naruto',
lastName: 'Uzumaki',
email: 'naruto.uzumaki@konoha.com',
role: 'ADMIN', // Hokage
},
{
id: 2,
firstName: 'Sasuke',
lastName: 'Uchiha',
email: 'sasuke.uchiha@konoha.com',
role: 'SHINOBI',
},
{
id: 3,
firstName: 'Hinata',
lastName: 'Hyuga',
email: 'hinata.hyuga@konoha.com',
role: 'SHINOBI',
},
{
id: 4,
firstName: 'Sakura',
lastName: 'Haruno',
email: 'sakura.haruno@konoha.com',
role: 'MED', // Med. Shinobi
},
{
id: 5,
firstName: 'Kakashi',
lastName: 'Hatake',
email: 'kakashi.hatake@konoha.com',
role: 'ADMIN', // Sensei
},
]
findAll(role?: Role): User[] {
if (role) {
return this.users.filter((user) => user.role === role)
}
return this.users
}
findById(id: number): User | undefined {
return this.users.find((user) => user.id === id)
}
create(user: Omit<User, 'id'>): User {
const maxId = this.users.reduce((max, u) => Math.max(max, u.id), 0)
const newUser = { id: maxId + 1, ...user }
this.users.push(newUser)
return newUser
}
update(id: number, updatedUser: Partial<User>) {
this.users = this.users.map((user) => {
if (user.id === id) {
return {
...user,
...updatedUser,
}
}
return user
})
return this.findById(id)
}
delete(id: number) {
const removedUser = this.findById(id)
this.users = this.users.filter((user) => user.id !== id)
return removedUser
}
}
......@@ -2,8 +2,5 @@
"version": "1",
"name": "Agent Framework",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}
\ No newline at end of file
"ignore": ["node_modules", ".git"]
}
meta {
name: createUser
type: http
seq: 4
}
post {
url: http://localhost:3000/users
body: json
auth: inherit
}
headers {
Content-Type: application/json
}
body:json {
{
"firstName": "Tsunade",
"lastName": "Senju",
"email": "tsunade.senju@konoha.com",
"role": "ADMIN"
}
}
meta {
name: deleteUser
type: http
seq: 6
}
delete {
url: http://localhost:3000/users/6
body: json
auth: inherit
}
headers {
Content-Type: application/json
}
meta {
name: findAllHokage (alternative)
type: http
seq: 3
}
get {
url: http://localhost:3000/users/hokage
body: none
auth: inherit
}
meta {
name: findAllShinobi
type: http
seq: 2
}
get {
url: http://localhost:3000/users?role=SHINOBI
body: none
auth: inherit
}
params:query {
role: SHINOBI
}
meta {
name: findAllUsers
type: http
seq: 1
}
get {
url: http://localhost:3000/users
body: none
auth: inherit
}
meta {
name: add_user
name: updateUser
type: http
seq: 2
seq: 5
}
post {
url: http://localhost:3000/users
patch {
url: http://localhost:3000/users/3
body: json
auth: inherit
}
......@@ -16,8 +16,7 @@ headers {
body:json {
{
"id": 1,
"firstName": "Naruto",
"firstName": "Hinata",
"lastName": "Uzumaki"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment