Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/procedures service #602

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"[ignore]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
"conventionalCommits.scopes": ["push-send-queued"]
"conventionalCommits.scopes": ["push-send-queued"],
"CodeGPT.apiKey": "OpenAI"
}
2 changes: 1 addition & 1 deletion bundestag.io/admin/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kind: Deploy
name: admin
type: kubernetes
description: Deploy the Admin
dependencies: [build.admin, deploy.bundestag-io-api, deploy.votes-ai, deploy.non-named-votes-ai]
dependencies: [build.admin, deploy.bundestag-io-api, deploy.votes-ai, deploy.non-named-votes-ai, deploy.procedures]

variables:
BIO_EDIT_TOKEN: ${actions.deploy.bundestag-io-api.var.BIO_EDIT_TOKEN}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from 'next/link';

export const PaginationNavigation = ({ currentPage, totalPages }) => (
<div>
<Link href={`?page=${currentPage - 1}`} passHref>
<button disabled={currentPage === 1}>Previous</button>
</Link>
<span>{`Page ${currentPage} of ${totalPages}`}</span>
<Link href={`?page=${currentPage + 1}`} passHref>
<button disabled={currentPage === totalPages}>Next</button>
</Link>
</div>
);
49 changes: 31 additions & 18 deletions bundestag.io/admin/src/app/list/past/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import Entry from '../_components/entry';
import { IProcedure } from '@democracy-deutschland/bundestagio-common';
import { unstable_noStore as noStore } from 'next/cache';
import Link from 'next/link';
import { PaginationNavigation } from '../_components/pagination-navigation';

const ITEMS_PER_PAGE = 10;

export const dynamic = 'force-dynamic';

export default async function Page() {
async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_PAGE;

const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past?limit=${limit}&page=${page}`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}

export default async function Page({ searchParams }: { searchParams: { page?: string } }) {
noStore();
const data = await getData();
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; // Standardwert ist Seite 1
const { procedures, count } = await getData(currentPage);
const totalPages = Math.ceil(count / ITEMS_PER_PAGE); // Berechne die Gesamtseitenzahl basierend auf der Anzahl der Elemente

return (
<>
<h1>Past procedures</h1>
{data.map((procedure) => (
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
Expand All @@ -21,21 +47,8 @@ export default async function Page() {
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
);
}

async function getData(): Promise<IProcedure[]> {
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}
50 changes: 32 additions & 18 deletions bundestag.io/admin/src/app/list/upcoming/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import Entry from '../_components/entry';
import { IProcedure } from '@democracy-deutschland/bundestagio-common';
import { unstable_noStore as noStore } from 'next/cache';
import Link from 'next/link';
import { PaginationNavigation } from '../_components/pagination-navigation';

const ITEMS_PER_PAGE = 10;

export const dynamic = 'force-dynamic';

export default async function Page() {
async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_PAGE;

const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming?limit=${limit}&page=${page}`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}

export default async function Page({ searchParams }: { searchParams: { page?: string } }) {
noStore();
const data = await getData();
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; // Standardwert ist Seite 1
const { procedures, count } = await getData(currentPage);
const totalPages = Math.ceil(count / ITEMS_PER_PAGE); // Berechne die Gesamtseitenzahl basierend auf der Anzahl der Elemente

return (
<>
<h1>Upcoming procedures</h1>
{data.map((procedure) => (

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
Expand All @@ -21,21 +48,8 @@ export default async function Page() {
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
);
}

async function getData(): Promise<IProcedure[]> {
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}
2 changes: 1 addition & 1 deletion services/procedures/bruno/environments/localhost.bru
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vars {
url: http://localhost:3000
url: http://localhost:3006
}
2 changes: 1 addition & 1 deletion services/procedures/bruno/findAll.bru
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ get {
}

assert {
res.body.procedureId: isString
res.body.procedures[0].procedureId: isString
res.status: eq 200
}
8 changes: 6 additions & 2 deletions services/procedures/bruno/pastProcedures.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ meta {
}

get {
url: {{url}}/procedures/list/past
url: {{url}}/procedures/list/past?limit=2
body: none
auth: none
}

params:query {
limit: 2
}

assert {
res.body[0].procedureId: isString
res.body.count: gt 0
res.status: eq 200
}
9 changes: 7 additions & 2 deletions services/procedures/bruno/upcomingProcedures.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ meta {
}

get {
url: {{url}}/procedures/list/upcoming
url: {{url}}/procedures/list/upcoming?limit=1&page=2
body: none
auth: none
}

params:query {
limit: 1
page: 2
}

assert {
res.body[0].procedureId: isString
res.body.procedures[0].procedureId: isString
res.status: eq 200
}
2 changes: 1 addition & 1 deletion services/procedures/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
sourcePath: src
mode: one-way
overrides:
- command: [pnpm, --filter, procedures, run, garden:dev]
- command: [pnpm, --filter, procedures, run, dev]
patchResources:
- name: procedures
kind: Deployment
Expand Down
14 changes: 14 additions & 0 deletions services/procedures/src/decorators/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const Pagination = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const { page = 1, limit = 10 } = request.query;

// Ensure valid pagination values
const validPage = Math.max(1, parseInt(page));
const validLimit = Math.min(Math.max(1, parseInt(limit)), 100); // Max limit of 100

return { page: validPage, limit: validLimit };
},
);
Loading