feat: add merge user
This commit is contained in:
parent
1682132e1c
commit
ede2eba681
5 changed files with 67 additions and 1 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { SubjectService } from '#services/subject_service'
|
||||
import { updateUserValidator } from '#validators/user'
|
||||
import { updateUserValidator, matchStudentValidator, mergeStudentValidator } from '#validators/user'
|
||||
import { inject } from '@adonisjs/core'
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import User from '#models/user'
|
||||
|
||||
@inject()
|
||||
export default class UserController {
|
||||
|
|
@ -54,4 +55,32 @@ export default class UserController {
|
|||
data: user,
|
||||
}
|
||||
}
|
||||
|
||||
// GET /match-student
|
||||
async matchStudent({ request, response }: HttpContext) {
|
||||
const { firstName, lastName, classId } = await request.validateUsing(matchStudentValidator)
|
||||
|
||||
const user = await User.query()
|
||||
.where('firstName', firstName)
|
||||
.andWhere('lastName', lastName)
|
||||
.andWhere('className', classId)
|
||||
.first()
|
||||
|
||||
if (!user) {
|
||||
return response.notFound({ message: 'User not found' })
|
||||
}
|
||||
|
||||
return { success: true, data: user }
|
||||
}
|
||||
|
||||
// POST /merge-student
|
||||
async mergeStudent({ request, auth }: HttpContext) {
|
||||
const user = auth.user!
|
||||
const { pastStudentId } = await request.validateUsing(mergeStudentValidator)
|
||||
|
||||
user.pastStudentId = pastStudentId
|
||||
await user.save()
|
||||
|
||||
return { success: true, data: user }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ export default class User extends BaseModel {
|
|||
@column({ isPrimary: true })
|
||||
declare id: number
|
||||
|
||||
@column()
|
||||
declare pastStudentId: number | null
|
||||
|
||||
@column()
|
||||
declare className: string
|
||||
|
||||
|
|
|
|||
|
|
@ -11,3 +11,17 @@ export const updateUserValidator = vine.compile(
|
|||
),
|
||||
})
|
||||
)
|
||||
|
||||
export const matchStudentValidator = vine.compile(
|
||||
vine.object({
|
||||
firstName: vine.string(),
|
||||
lastName: vine.string(),
|
||||
classId: vine.string(),
|
||||
})
|
||||
)
|
||||
|
||||
export const mergeStudentValidator = vine.compile(
|
||||
vine.object({
|
||||
pastStudentId: vine.number(),
|
||||
})
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
import { BaseSchema } from '@adonisjs/lucid/schema'
|
||||
|
||||
export default class extends BaseSchema {
|
||||
protected tableName = 'add_past_student_id_to_users'
|
||||
|
||||
async up() {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id')
|
||||
|
||||
table.timestamp('created_at')
|
||||
table.timestamp('updated_at')
|
||||
})
|
||||
}
|
||||
|
||||
async down() {
|
||||
this.schema.dropTable(this.tableName)
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,8 @@ router
|
|||
const UserController = () => import('#controllers/user_controller')
|
||||
router.get('/users/@me', [UserController, 'me']).use(middleware.auth())
|
||||
router.post('/users/@me', [UserController, 'update']).use(middleware.auth())
|
||||
router.get('/match-student', [UserController, 'matchStudent']).use(middleware.auth())
|
||||
router.post('/merge-student', [UserController, 'mergeStudent']).use(middleware.auth())
|
||||
|
||||
const SubjectsController = () => import('#controllers/subjects_controller')
|
||||
router.get('/subjects', [SubjectsController, 'index']).use(middleware.auth())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue