✨ Add user creation
This commit is contained in:
parent
709a7bb6f7
commit
211e6e26f5
7 changed files with 72 additions and 12 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -23,3 +23,6 @@ yarn-error.log
|
|||
|
||||
# Platform specific
|
||||
.DS_Store
|
||||
|
||||
# Storage
|
||||
storage/
|
||||
|
|
|
|||
29
app/controllers/user_controller.ts
Normal file
29
app/controllers/user_controller.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import User from '#models/user'
|
||||
import { createUserValidator } from '#validators/user'
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import { cuid } from '@adonisjs/core/helpers'
|
||||
import drive from '@adonisjs/drive/services/main'
|
||||
|
||||
export default class UserController {
|
||||
// GET /user/@me
|
||||
async me({ auth }: HttpContext) {
|
||||
return {
|
||||
success: true,
|
||||
data: auth.user,
|
||||
}
|
||||
}
|
||||
|
||||
// POST /user
|
||||
async create({ request }: HttpContext) {
|
||||
const payload = await request.validateUsing(createUserValidator)
|
||||
// Save avatar
|
||||
const avatar = `avatars/${cuid()}.${payload.avatar.extname}`
|
||||
await payload.avatar.moveToDisk(avatar)
|
||||
// const avatar = await drive.use().getSignedUrl(key)
|
||||
console.log(avatar)
|
||||
return User.create({
|
||||
...payload,
|
||||
avatar,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -8,8 +8,8 @@ export default class User extends BaseModel {
|
|||
@column({ isPrimary: true })
|
||||
declare id: number
|
||||
|
||||
@column({ serializeAs: 'class', serialize: serializeClass })
|
||||
declare classId: number
|
||||
@column()
|
||||
declare className: string
|
||||
|
||||
@column()
|
||||
declare firstName: string
|
||||
|
|
@ -29,8 +29,3 @@ export default class User extends BaseModel {
|
|||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
declare updatedAt: DateTime | null
|
||||
}
|
||||
|
||||
// TODO
|
||||
function serializeClass(classId: number): string {
|
||||
return 'class_' + classId
|
||||
}
|
||||
|
|
|
|||
10
app/validators/user.ts
Normal file
10
app/validators/user.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import vine from '@vinejs/vine'
|
||||
|
||||
export const createUserValidator = vine.compile(
|
||||
vine.object({
|
||||
firstName: vine.string().minLength(2).maxLength(50),
|
||||
lastName: vine.string().minLength(2).maxLength(50),
|
||||
className: vine.string().minLength(2).maxLength(10),
|
||||
avatar: vine.file(),
|
||||
})
|
||||
)
|
||||
|
|
@ -6,10 +6,11 @@ export default class extends BaseSchema {
|
|||
async up() {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id').notNullable()
|
||||
table.integer('class_id').notNullable()
|
||||
table.string('first_name', 80).notNullable()
|
||||
table.string('last_name', 80).notNullable()
|
||||
table.string('email', 254).notNullable().unique()
|
||||
table.string('class_name', 10).notNullable()
|
||||
table.string('first_name', 50).notNullable()
|
||||
table.string('last_name', 50).notNullable()
|
||||
table.string('email', 254).unique().nullable()
|
||||
table.string('avatar', 254).notNullable()
|
||||
table.timestamp('created_at').notNullable()
|
||||
table.timestamp('updated_at').nullable()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,4 +4,17 @@ services:
|
|||
container_name: khollise-redis
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "6379:6379"
|
||||
- "127.0.0.1:6379:6379"
|
||||
|
||||
postgres:
|
||||
image: postgres:15
|
||||
container_name: khollise-postgres
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: khollise
|
||||
POSTGRES_USER: khollise
|
||||
POSTGRES_PASSWORD: securepass
|
||||
volumes:
|
||||
- ./data:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "127.0.0.1:5432:5432"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import router from '@adonisjs/core/services/router'
|
|||
import transmit from '@adonisjs/transmit/services/main'
|
||||
import { authThrottle } from './limiters.js'
|
||||
import { throttle } from './limiter.js'
|
||||
import app from '@adonisjs/core/services/app'
|
||||
import { middleware } from './kernel.js'
|
||||
|
||||
transmit.registerRoutes()
|
||||
|
||||
|
|
@ -22,3 +24,10 @@ router.group(() => {
|
|||
// router.get('/auth/magic-link', 'AuthController.magicLink').use(throttle)
|
||||
// router.get('/auth/listen', 'AuthController.listen')
|
||||
})
|
||||
|
||||
const UserController = () => import('#controllers/user_controller')
|
||||
|
||||
if (app.inDev) {
|
||||
router.post('user', [UserController, 'create'])
|
||||
}
|
||||
router.get('/user/@me', [UserController, 'me']).use(middleware.auth())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue