Skip to content
Snippets Groups Projects
Commit 7473c5be authored by nar20's avatar nar20
Browse files

Issue #1:ADD update user route

parent 21d4745e
No related branches found
No related tags found
1 merge request!2Issue/1 update user route
...@@ -4,6 +4,7 @@ import userTable, { ...@@ -4,6 +4,7 @@ import userTable, {
userSchemas, userSchemas,
type UserInput, type UserInput,
type UserModel, type UserModel,
type UserUpdate,
} from '../schema/user.model' } from '../schema/user.model'
import { z } from 'zod' import { z } from 'zod'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
...@@ -57,4 +58,15 @@ export class UserRepo { ...@@ -57,4 +58,15 @@ export class UserRepo {
.parse(await tx.insert(userTable).values(users).returning()) .parse(await tx.insert(userTable).values(users).returning())
}) })
} }
async update(user: UserUpdate): Promise<UserModel>{
const [ret] = await db
.update(userTable)
.set(user)
.where(eq(userTable.id, user.id))
.returning()
return userSchemas.userModelSchema.parse(ret)
}
} }
...@@ -14,13 +14,18 @@ const userModelSchema = createSelectSchema(userTable) ...@@ -14,13 +14,18 @@ const userModelSchema = createSelectSchema(userTable)
const userDtoSchema = createSelectSchema(userTable).omit({ const userDtoSchema = createSelectSchema(userTable).omit({
password: true, password: true,
}) })
const userUpdateSchema = createSelectSchema(userTable)
.partial()
.required({ id: true })
export type UserInput = z.infer<typeof userInputSchema> export type UserInput = z.infer<typeof userInputSchema>
export type UserUpdate = z.infer<typeof userUpdateSchema>
export type UserModel = z.infer<typeof userModelSchema> export type UserModel = z.infer<typeof userModelSchema>
export type UserDto = z.infer<typeof userDtoSchema> export type UserDto = z.infer<typeof userDtoSchema>
export const userSchemas = { export const userSchemas = {
userInputSchema, userInputSchema,
userUpdateSchema,
userModelSchema, userModelSchema,
userDtoSchema, userDtoSchema,
} }
......
import { Hono } from 'hono' import { Hono } from 'hono'
import { logger } from 'hono/logger' import { logger } from 'hono/logger'
import { userRouter } from './routes/user.routes' import { signUpRouter, userRouter } from './routes/user.routes'
import { authRouter } from './routes/auth.route' import { authRouter } from './routes/auth.route'
import { jwt } from 'hono/jwt' import { jwt } from 'hono/jwt'
import env from './env' import env from './env'
...@@ -46,7 +46,8 @@ app.use( ...@@ -46,7 +46,8 @@ app.use(
app.get('/', (c) => c.json({ message: 'sv running on /api' })) app.get('/', (c) => c.json({ message: 'sv running on /api' }))
app.route('auth', authRouter) app.route('/auth', authRouter)
app.route('/signup', signUpRouter)
app app
.basePath('/api') .basePath('/api')
......
...@@ -4,21 +4,29 @@ import { Container } from 'typedi' ...@@ -4,21 +4,29 @@ import { Container } from 'typedi'
import { z } from 'zod' import { z } from 'zod'
import { zValidator } from '@hono/zod-validator' import { zValidator } from '@hono/zod-validator'
import { honoWithJwt } from '..' import { honoWithJwt } from '..'
import { Hono } from 'hono'
import { createApexError, HttpStatus } from '@/services/error.service'
const service = Container.get(UserService) const service = Container.get(UserService)
export const userRouter = honoWithJwt() export const signUpRouter = new Hono()
.get('/:username', async (c) => { .post('/',
zValidator('json', userSchemas.userInputSchema),
async (c) => {
try { try {
const username = c.req.param('username') const input = await c.req.valid('json')
const user = await service.findByUsername(username)
const ret = userSchemas.userDtoSchema.parse(user)
return c.json({ user: ret }) const user = userSchemas.userDtoSchema.parse(
} catch { await service.create(input)
return c.notFound() )
return c.json({ user })
} catch (e) {
return c.text('could not create')
} }
}) })
export const userRouter = honoWithJwt()
.get('/users', async (c) => { .get('/users', async (c) => {
const p = c.get('jwtPayload') const p = c.get('jwtPayload')
...@@ -30,20 +38,40 @@ export const userRouter = honoWithJwt() ...@@ -30,20 +38,40 @@ export const userRouter = honoWithJwt()
return c.json({ users: ret }) return c.json({ users: ret })
}) })
.post( .get('/:username', async (c) => {
'/create', try {
zValidator('json', userSchemas.userInputSchema), const username = c.req.param('username')
const user = await service.findByUsername(username)
const ret = userSchemas.userDtoSchema.parse(user)
return c.json({ user: ret })
} catch {
return c.notFound()
}
})
.post('/update',
zValidator('json', userSchemas.userUpdateSchema),
async (c) => { async (c) => {
try { try {
const input = await c.req.valid('json') const input = await c.req.valid('json')
const user = userSchemas.userDtoSchema.parse( const user = userSchemas.userDtoSchema.parse(
await service.create(input) await service.update(input)
) )
return c.json({user}) return c.json({user})
} catch (e) { } catch (e) {
return c.text('could not create')
} return c.json(
} createApexError({
status: 'error',
message: 'could not update user',
code: HttpStatus.BAD_REQUEST,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.BAD_REQUEST
) )
}
})
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
userSchemas, userSchemas,
type UserInput, type UserInput,
type UserModel, type UserModel,
type UserUpdate,
} from '@/db/schema/user.model' } from '@/db/schema/user.model'
import { Inject, Service } from 'typedi' import { Inject, Service } from 'typedi'
import { z } from 'zod' import { z } from 'zod'
...@@ -51,4 +52,10 @@ export class UserService { ...@@ -51,4 +52,10 @@ export class UserService {
return this.repo.uploadUsers(users) return this.repo.uploadUsers(users)
} }
async update(user: UserUpdate): Promise<UserModel> {
if(user.password)
user.password = hashPassword(user.password)
return this.repo.update(user)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment