Add svelte as a view engine to swaf #33

Merged
ashpie merged 97 commits from svelte into develop 2021-11-09 19:31:22 +01:00
5 changed files with 34 additions and 11 deletions
Showing only changes of commit d925237233 - Show all commits

View File

@ -1,6 +1,9 @@
import {WrappingError} from "./Utils.js"; export class Pagination {
public static deserialize(str: string): Pagination {
const data = JSON.parse(str);
return new Pagination(data.page, data.perPage, data.totalCount);
}
export default class Pagination {
public readonly page: number; public readonly page: number;
public readonly perPage: number; public readonly perPage: number;
public readonly totalCount: number; public readonly totalCount: number;
@ -80,9 +83,13 @@ export default class Pagination {
return pages; return pages;
} }
public serialize(): string {
return JSON.stringify(this);
}
} }
export class PageNotFoundError extends WrappingError { export class PageNotFoundError extends Error {
public constructor( public constructor(
public readonly page: number, public readonly page: number,
) { ) {

View File

@ -1,7 +1,7 @@
import {Request} from "express"; import {Request} from "express";
import {PageNotFoundError} from "../common/Pagination.js";
import {NotFoundHttpError} from "../HttpError.js"; import {NotFoundHttpError} from "../HttpError.js";
import {PageNotFoundError} from "../Pagination.js";
import Model, {ModelType} from "./Model.js"; import Model, {ModelType} from "./Model.js";
import ModelComponent from "./ModelComponent.js"; import ModelComponent from "./ModelComponent.js";
import ModelQuery, {ModelQueryResult, QueryFields} from "./ModelQuery.js"; import ModelQuery, {ModelQueryResult, QueryFields} from "./ModelQuery.js";

View File

@ -1,6 +1,6 @@
import {Connection} from "mysql"; import {Connection} from "mysql";
import Pagination from "../Pagination.js"; import {Pagination} from "../common/Pagination.js";
import Model from "./Model.js"; import Model from "./Model.js";
import ModelFactory from "./ModelFactory.js"; import ModelFactory from "./ModelFactory.js";
import ModelRelation, {RelationDatabaseProperties} from "./ModelRelation.js"; import ModelRelation, {RelationDatabaseProperties} from "./ModelRelation.js";

View File

@ -6,6 +6,7 @@ import UserApprovedComponent from "../auth/models/UserApprovedComponent.js";
import UserEmail from "../auth/models/UserEmail.js"; import UserEmail from "../auth/models/UserEmail.js";
import UserNameComponent from "../auth/models/UserNameComponent.js"; import UserNameComponent from "../auth/models/UserNameComponent.js";
import {route} from "../common/Routing.js"; import {route} from "../common/Routing.js";
import {Time} from "../common/Time.js";
import MailComponent from "../components/MailComponent.js"; import MailComponent from "../components/MailComponent.js";
import Controller from "../Controller.js"; import Controller from "../Controller.js";
import ModelFactory from "../db/ModelFactory.js"; import ModelFactory from "../db/ModelFactory.js";
@ -45,7 +46,7 @@ export default class BackendController extends Controller {
this.get('/', this.getIndex, 'backend'); this.get('/', this.getIndex, 'backend');
if (User.isApprovalMode()) { if (User.isApprovalMode()) {
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval'); this.get('/accounts-approval/:page?', this.getAccountApproval, 'accounts-approval');
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account'); this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account');
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account'); this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account');
} }
@ -62,12 +63,16 @@ export default class BackendController extends Controller {
} }
protected async getAccountApproval(req: Request, res: Response): Promise<void> { protected async getAccountApproval(req: Request, res: Response): Promise<void> {
const accounts = await User.select() const accounts = await User.paginate(req, 1, User.select()
.where('approved', 0) .where('approved', 0)
.with('mainEmail') .with('mainEmail'));
.get();
res.render('backend/accounts_approval', { res.render('backend/accounts_approval', {
accounts: accounts, accounts: accounts.map(account => Object.assign({
mainEmailStr: account.mainEmail.getOrFail()?.email,
created_at_iso: account.created_at?.toISOString(),
created_at_human: account.created_at ? Time.humanizeTimeSince(account.created_at) : '',
}, account)),
pagination: accounts.pagination?.serialize(),
has_user_name_component: ModelFactory.get(User).hasComponent(UserNameComponent), has_user_name_component: ModelFactory.get(User).hasComponent(UserNameComponent),
}); });
} }

View File

@ -1,4 +1,4 @@
import Pagination from "../src/Pagination.js"; import Pagination from "../../src/common/Pagination.js";
describe('Pagination', () => { describe('Pagination', () => {
const pagination = new Pagination(3, 5, 31); const pagination = new Pagination(3, 5, 31);
@ -134,4 +134,15 @@ describe('Pagination', () => {
expect(pagination.nextPages(2)).toStrictEqual([91, 92, -1, 99, 100]); expect(pagination.nextPages(2)).toStrictEqual([91, 92, -1, 99, 100]);
expect(pagination.nextPages(3)).toStrictEqual([91, 92, 93, -1, 98, 99, 100]); expect(pagination.nextPages(3)).toStrictEqual([91, 92, 93, -1, 98, 99, 100]);
}); });
test('Should serialize properly', () => {
expect(new Pagination(5, 6, 1000).serialize()).toStrictEqual('{"page":5,"perPage":6,"totalCount":1000}');
});
test('Should deserialize properly', () => {
const pagination = Pagination.deserialize('{"page":5,"perPage":6,"totalCount":1000}');
expect(pagination.page).toStrictEqual(5);
expect(pagination.perPage).toStrictEqual(6);
expect(pagination.totalCount).toStrictEqual(1000);
});
}); });