-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Hapi from '@hapi/hapi'; | ||
import { prisma } from '#common/db'; | ||
import Joi from 'joi'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
export const createUserionRoute: Hapi.ServerRoute = { | ||
method: 'POST', | ||
path: '/users', | ||
options: { | ||
description: 'Create a new user', | ||
notes: 'Create user and its attributes', | ||
tags: ['api', 'user', 'create'], | ||
plugins: { 'hapi-swagger': {} }, | ||
validate: { | ||
payload: Joi.object({ | ||
name: Joi.string().required().example('User name'), | ||
bio: Joi.string().required().example('Bio'), | ||
email: Joi.string().required().email().example('[email protected]'), | ||
wallet_address: Joi.string().required().example('0x1234567890'), | ||
avatar_img: Joi.string() | ||
.required() | ||
.example('https://loremflickr.com/640/480?lock=1572275828555776'), | ||
banner_img: Joi.string() | ||
.required() | ||
.example('https://loremflickr.com/640/480?lock=1572275828555776'), | ||
attributes: Joi.array() | ||
.items( | ||
Joi.object({ | ||
name: Joi.string().required().example('attribute name'), | ||
value: Joi.string().required().example('attribute value'), | ||
}), | ||
) | ||
.default([]), | ||
additional_info: Joi.object({ | ||
headline: Joi.string().optional().example('headline'), | ||
location: Joi.string().optional().example('location'), | ||
socials: Joi.array() | ||
.items( | ||
Joi.object({ | ||
name: Joi.string().required().example('twitter'), | ||
url: Joi.string().required().example('https://twitter.com'), | ||
}), | ||
) | ||
.default([]), | ||
}) | ||
.optional() | ||
.default({}), | ||
}), | ||
}, | ||
// TODO validate response: { schema: Joi.object({}) } | ||
}, | ||
handler: async (request: Hapi.Request) => { | ||
const payload = request.payload as any; | ||
|
||
// create new user | ||
const userData = { | ||
name: payload.name, | ||
bio: payload.bio, | ||
email: payload.email, | ||
wallet_address: payload.wallet_address, | ||
avatar_img: payload.avatar_img, | ||
banner_img: payload.banner_img, | ||
additional_info: payload.additional_info, | ||
attributes: { create: payload.attributes }, | ||
} as Prisma.UserCreateInput; | ||
const user = await prisma.user.create({ data: userData }); | ||
|
||
return { data: { id: user.id } }; | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Hapi from '@hapi/hapi'; | ||
import { prisma } from '#common/db'; | ||
import { Prisma } from '@prisma/client'; | ||
import Joi from 'joi'; | ||
|
||
export const updateUserRoute: Hapi.ServerRoute = { | ||
method: 'PUT', | ||
path: '/users/{id}', | ||
options: { | ||
description: 'Update user by its ID', | ||
notes: 'Update user, its attributes and collections', | ||
tags: ['api', 'user', 'update'], | ||
plugins: { 'hapi-swagger': {} }, | ||
validate: { | ||
params: Joi.object({ | ||
id: Joi.number().required().example(1), | ||
}), | ||
payload: Joi.object({ | ||
name: Joi.string().optional().example('User name'), | ||
bio: Joi.string().optional().example('Bio'), | ||
email: Joi.string().optional().email().example('[email protected]'), | ||
wallet_address: Joi.string().optional().example('0x1234567890'), | ||
avatar_img: Joi.string() | ||
.optional() | ||
.example('https://loremflickr.com/640/480?lock=1572275828555776'), | ||
banner_img: Joi.string() | ||
.optional() | ||
.example('https://loremflickr.com/640/480?lock=1572275828555776'), | ||
attributes: Joi.array() | ||
.items( | ||
Joi.object({ | ||
name: Joi.string().required().example('attribute name'), | ||
value: Joi.string().required().example('attribute value'), | ||
}), | ||
) | ||
.default([]), | ||
additional_info: Joi.object({ | ||
headline: Joi.string().optional().example('headline'), | ||
location: Joi.string().optional().example('location'), | ||
socials: Joi.array() | ||
.optional() | ||
.items( | ||
Joi.object({ | ||
name: Joi.string().required().example('twitter'), | ||
url: Joi.string().required().example('https://twitter.com'), | ||
}), | ||
) | ||
.default([]) | ||
.optional(), | ||
}).optional(), | ||
}), | ||
}, | ||
// response: { schema: Joi.object({}) } | ||
}, | ||
handler: async (request: Hapi.Request) => { | ||
const id = Number(request.params.id); | ||
const payload = request.payload as any; | ||
|
||
// create new user | ||
const user = { | ||
name: payload.name, | ||
bio: payload.bio, | ||
avatar_img: payload.avatar_img, | ||
banner_img: payload.banner_img, | ||
additional_info: payload.additional_info, | ||
} as Prisma.UserCreateInput; | ||
|
||
await Promise.all([ | ||
prisma.user.update({ | ||
where: { id: Number(request.params.id) }, | ||
data: user, | ||
}), | ||
prisma.$transaction([ | ||
prisma.userAttribute.deleteMany({ where: { user_id: id } }), | ||
prisma.userAttribute.createMany({ | ||
data: payload.attributes.map((att: any) => ({ | ||
...att, | ||
user_id: id, | ||
})) as Prisma.UserAttributeCreateManyInput[], | ||
}), | ||
]), | ||
]); | ||
|
||
return { data: { id } }; | ||
}, | ||
}; |