diff --git a/.gitignore b/.gitignore index 4e9b445..3af9d10 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ yarn-error.log # Platform specific .DS_Store + +# Storage +storage/ diff --git a/app/controllers/user_controller.ts b/app/controllers/user_controller.ts new file mode 100644 index 0000000..934b2ed --- /dev/null +++ b/app/controllers/user_controller.ts @@ -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, + }) + } +} diff --git a/app/models/user.ts b/app/models/user.ts index 1e8deef..d2283d3 100644 --- a/app/models/user.ts +++ b/app/models/user.ts @@ -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 -} diff --git a/app/validators/user.ts b/app/validators/user.ts new file mode 100644 index 0000000..9504d9a --- /dev/null +++ b/app/validators/user.ts @@ -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(), + }) +) diff --git a/database/migrations/1747564644339_create_users_table.ts b/database/migrations/1747564644339_create_users_table.ts index a25df05..7d3453a 100644 --- a/database/migrations/1747564644339_create_users_table.ts +++ b/database/migrations/1747564644339_create_users_table.ts @@ -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() }) diff --git a/docker-compose.yml b/docker-compose.yml index e0e1611..fc08f2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" diff --git a/start/routes.ts b/start/routes.ts index 935ca66..3f983fc 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -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())