refactor: controllers and models to adapt them to the previous version of my-links

This commit is contained in:
Sonny
2024-05-24 23:54:43 +02:00
committed by Sonny
parent 31b4f22772
commit b28499a69a
19 changed files with 113 additions and 85 deletions

View File

@@ -120,6 +120,7 @@ export default class CollectionsController {
async getCollectionsByAuthorId(authorId: User['id']) {
return await Collection.query()
.where('author_id', authorId)
.orderBy('created_at')
.preload('links');
}

View File

@@ -49,6 +49,7 @@ export default class UsersController {
nickName,
avatarUrl,
token,
providerType: 'google',
}
);

View File

@@ -10,7 +10,7 @@ export default class LogRequest {
!request.url().startsWith('/@react-refresh') &&
!request.url().includes('.ts')
) {
logger.info(`-> ${request.method()}: ${request.url()}`);
logger.info(`[${request.method()}]: ${request.url()}`);
}
await next();
}

View File

@@ -1,29 +1,27 @@
import { BaseModel, CamelCaseNamingStrategy, beforeCreate, column } from '@adonisjs/lucid/orm';
import {
BaseModel,
CamelCaseNamingStrategy,
column,
} from '@adonisjs/lucid/orm';
import { DateTime } from 'luxon';
import { v4 as uuidv4 } from 'uuid';
export default class AppBaseModel extends BaseModel {
static namingStrategy = new CamelCaseNamingStrategy();
static selfAssignPrimaryKey = true;
@column({ isPrimary: true })
declare id: string; // UUID
declare id: number;
@column.dateTime({
autoCreate: true,
serializeAs: 'createdAt',
serializeAs: 'created_at',
})
declare createdAt: DateTime;
declare created_at: DateTime;
@column.dateTime({
autoCreate: true,
autoUpdate: true,
serializeAs: 'updatedAt',
serializeAs: 'updated_at',
})
declare updatedAt: DateTime;
@beforeCreate()
static assignUuid(item: any) {
item.id = uuidv4();
}
declare updated_at: DateTime;
}

View File

@@ -16,10 +16,10 @@ export default class Collection extends AppBaseModel {
declare visibility: Visibility;
@column()
declare nextId: string;
declare next_id: number;
@column()
declare authorId: string;
declare authorId: number;
@belongsTo(() => User, { foreignKey: 'authorId' })
declare author: BelongsTo<typeof User>;

View File

@@ -9,7 +9,7 @@ export default class Link extends AppBaseModel {
declare name: string;
@column()
declare description: string;
declare description: string | null;
@column()
declare url: string;
@@ -18,13 +18,13 @@ export default class Link extends AppBaseModel {
declare favorite: boolean;
@column()
declare collectionId: string;
declare collectionId: number;
@belongsTo(() => Collection, { foreignKey: 'collectionId' })
declare collection: BelongsTo<typeof Collection>;
@column()
declare authorId: string;
declare authorId: number;
@belongsTo(() => User, { foreignKey: 'authorId' })
declare author: BelongsTo<typeof User>;

View File

@@ -1,7 +1,7 @@
import Collection from '#models/collection';
import Link from '#models/link';
import type { GoogleToken } from '@adonisjs/ally/types';
import { column, manyToMany } from '@adonisjs/lucid/orm';
import { column, computed, manyToMany } from '@adonisjs/lucid/orm';
import type { ManyToMany } from '@adonisjs/lucid/types/relations';
import AppBaseModel from './app_base_model.js';
@@ -12,20 +12,23 @@ export default class User extends AppBaseModel {
@column()
declare name: string;
@column({ serializeAs: 'nickName' })
@column()
declare nickName: string; // public username
@column({ serializeAs: 'avatarUrl' })
@column()
declare avatarUrl: string;
@column()
declare isAdmin: boolean;
@column({ serializeAs: null })
declare token: GoogleToken;
declare token?: GoogleToken;
@column({ serializeAs: null })
declare providerId: string;
declare providerId: number;
@column({ serializeAs: null })
declare providerType: 'google';
@manyToMany(() => Collection, {
relatedKey: 'authorId',
@@ -36,4 +39,9 @@ export default class User extends AppBaseModel {
relatedKey: 'authorId',
})
declare links: ManyToMany<typeof Link>;
@computed()
get fullname() {
return this.nickName || this.name;
}
}

View File

@@ -2,7 +2,7 @@ import vine, { SimpleMessagesProvider } from '@vinejs/vine';
import { Visibility } from '../enums/visibility.js';
const params = vine.object({
id: vine.string().trim(),
id: vine.number(),
});
export const createCollectionValidator = vine.compile(
@@ -10,7 +10,7 @@ export const createCollectionValidator = vine.compile(
name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(254).nullable(),
visibility: vine.enum(Visibility),
nextId: vine.string().optional(),
nextId: vine.number(),
})
);
@@ -19,7 +19,7 @@ export const updateCollectionValidator = vine.compile(
name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(254).nullable(),
visibility: vine.enum(Visibility),
nextId: vine.string().optional(),
nextId: vine.number(),
params,
})

View File

@@ -1,7 +1,7 @@
import vine from '@vinejs/vine';
const params = vine.object({
id: vine.string().trim(),
id: vine.number(),
});
export const createLinkValidator = vine.compile(
@@ -10,7 +10,7 @@ export const createLinkValidator = vine.compile(
description: vine.string().trim().maxLength(300).optional(),
url: vine.string().trim(),
favorite: vine.boolean(),
collectionId: vine.string().trim(),
collectionId: vine.number(),
})
);
@@ -20,7 +20,7 @@ export const updateLinkValidator = vine.compile(
description: vine.string().trim().maxLength(300).optional(),
url: vine.string().trim(),
favorite: vine.boolean(),
collectionId: vine.string().trim(),
collectionId: vine.number(),
params,
})
@@ -37,7 +37,7 @@ export const updateLinkFavoriteStatusValidator = vine.compile(
favorite: vine.boolean(),
params: vine.object({
id: vine.string().trim(),
id: vine.number(),
}),
})
);

9
app/validators/search.ts Normal file
View File

@@ -0,0 +1,9 @@
import vine from '@vinejs/vine';
export const searchValidator = vine.compile(
vine.object({
searchTerm: vine.string().trim().minLength(1).maxLength(254),
links: vine.boolean(),
collections: vine.boolean(),
})
);