57 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { SubjectService } from '#services/subject_service'
 | |
| import { updateUserValidator } from '#validators/user'
 | |
| import { inject } from '@adonisjs/core'
 | |
| import type { HttpContext } from '@adonisjs/core/http'
 | |
| 
 | |
| @inject()
 | |
| export default class UserController {
 | |
|   constructor(private subjectService: SubjectService) {}
 | |
| 
 | |
|   // GET /users/@me
 | |
|   async me({ auth }: HttpContext) {
 | |
|     return {
 | |
|       success: true,
 | |
|       data: {
 | |
|         ...auth.user?.serialize(),
 | |
|         email: auth.user!.email || '',
 | |
|       },
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   // POST /users/@me
 | |
|   // Update user preferences (for subjects)
 | |
|   async update({ request, response, auth }: HttpContext) {
 | |
|     const user = auth.user!
 | |
|     const { preferences: data } = await request.validateUsing(updateUserValidator)
 | |
|     const preferences = user.extras?.preferences || []
 | |
| 
 | |
|     // Validate subject names
 | |
|     const validSubjects = await this.subjectService.getAll(user.className)
 | |
|     for (const { name, emoji, color } of data) {
 | |
|       if (!validSubjects.includes(name)) {
 | |
|         return response.badRequest({
 | |
|           message: `Invalid subject name: ${name}`,
 | |
|         })
 | |
|       }
 | |
|       const existing = preferences.find((p: any) => p.name === name)
 | |
|       if (existing) {
 | |
|         // Update
 | |
|         existing.emoji = emoji
 | |
|         existing.color = color
 | |
|       } else {
 | |
|         // Create new preference
 | |
|         preferences.push({ name, emoji, color })
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     user.extras = {
 | |
|       ...user.extras,
 | |
|       preferences,
 | |
|     }
 | |
|     await user.save()
 | |
|     return {
 | |
|       success: true,
 | |
|       data: user,
 | |
|     }
 | |
|   }
 | |
| }
 | 
