Skip to content

Commit

Permalink
api version refactor: documenten
Browse files Browse the repository at this point in the history
  • Loading branch information
nijmra committed Nov 19, 2024
1 parent 7949f14 commit 5e5c22a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace ODPC.Features.Documenten.DocumentBijwerken
[ApiController]
public class DocumentBijwerkenController(IOdrcClientFactory clientFactory) : ControllerBase
{
[HttpPut("api/v1/documenten/{uuid:guid}")]
public async Task<IActionResult> Put(Guid uuid, PublicatieDocument document, CancellationToken token)
[HttpPut("api/{apiVersion}/documenten/{uuid:guid}")]
public async Task<IActionResult> Put(string apiVersion, Guid uuid, PublicatieDocument document, CancellationToken token)
{
using var client = clientFactory.Create("Document bijwerken");
var response = await client.PutAsJsonAsync("/api/v1/documenten/" + uuid, document, token);
var url = "/api/" + apiVersion + "/documenten/" + uuid;

var response = await client.PutAsJsonAsync(url, document, token);

response.EnsureSuccessStatusCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace ODPC.Features.Documenten.DocumentDownload
{
public class DocumentDownloadController(IOdrcClientFactory clientFactory) : ControllerBase
{
[HttpGet("api/v1/documenten/{uuid:guid}/download")]
public async Task<IActionResult> Get(Guid uuid, CancellationToken token)
[HttpGet("api/{apiVersion}/documenten/{uuid:guid}/download")]
public async Task<IActionResult> Get(string apiVersion, Guid uuid, CancellationToken token)
{
using var client = clientFactory.Create("Document downloaden");
var response = await client.GetAsync("/api/v1/documenten/" + uuid + "/download", token);
var url = "/api/" + apiVersion + "/documenten/" + uuid + "/download";

var response = await client.GetAsync(url, token);

response.EnsureSuccessStatusCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace ODPC.Features.Documenten.DocumentenOverzicht
[ApiController]
public class DocumentenOverzichtController(IOdrcClientFactory clientFactory) : ControllerBase
{
[HttpGet("api/v1/documenten")]
public async Task<IActionResult> Get([FromQuery] string publicatie, string? page, CancellationToken token)
[HttpGet("api/{apiVersion}/documenten")]
public async Task<IActionResult> Get(string apiVersion, [FromQuery] string publicatie, string? page, CancellationToken token)
{
// documenten ophalen uit het ODRC
using var client = clientFactory.Create("Documenten ophalen");
var json = await client.GetFromJsonAsync<PagedResponseModel<JsonNode>>("/api/v1/documenten?publicatie=" + publicatie + "&page=" + page, token);
var url = "/api/" + apiVersion + "/documenten?publicatie=" + publicatie + "&page=" + page;

var json = await client.GetFromJsonAsync<PagedResponseModel<JsonNode>>(url, token);

if (json != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ODPC.Apis.Odrc;
using ODPC.Features.Publicaties;

namespace ODPC.Features.Documenten.InitialiseerDocument
{
[ApiController]
public class InitialiseerDocumentController(IOdrcClientFactory clientFactory) : ControllerBase
{
[HttpPost("api/v1/documenten")]
public async Task<IActionResult> Post(PublicatieDocument document, CancellationToken token)
[HttpPost("api/{apiVersion}/documenten")]
public async Task<IActionResult> Post(string apiVersion, PublicatieDocument document, CancellationToken token)
{
using var client = clientFactory.Create("Initialiseer document");
var response = await client.PostAsJsonAsync("/api/v1/documenten", document, token);
var url = "/api/" + apiVersion + "/documenten";

var response = await client.PostAsJsonAsync(url, document, token);

response.EnsureSuccessStatusCode();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ODPC.Apis.Odrc;
using ODPC.Features.Publicaties;

namespace ODPC.Features.Documenten.UploadBestandsdeel
{
[ApiController]
[DisableRequestSizeLimit]
public class UploadBestandsdeelController(IOdrcClientFactory clientFactory) : ControllerBase
{
[HttpPut("api/v1/documenten/bestandsdelen")]
public async Task<IActionResult> Put(CancellationToken token)
[HttpPut("api/{apiVersion}/documenten/{docUuid:guid}/bestandsdelen/{partUuid:guid}")]
public async Task<IActionResult> Put(string apiVersion, Guid docUuid, Guid partUuid, CancellationToken token)
{
var form = await Request.ReadFormAsync(token);

Expand All @@ -17,25 +19,16 @@ public async Task<IActionResult> Put(CancellationToken token)
return BadRequest("No file uploaded");
}

var endpoint = form["url"].ToString();

if (string.IsNullOrEmpty(endpoint))
{
return BadRequest("No upload url");
}
else
{
endpoint = UrlHelper.GetPathAndQuery(endpoint);
}

var file = form.Files[0];
var content = new MultipartFormDataContent();
var fileContent = new StreamContent(file.OpenReadStream());

content.Add(fileContent, "inhoud", file.FileName);

using var client = clientFactory.Create("Upload bestandsdeel");
var response = await client.PutAsync(endpoint, content, token);
var url = "/api/" + apiVersion + "/documenten/" + docUuid + "/bestandsdelen/" + partUuid;

var response = await client.PutAsync(url, content, token);

response.EnsureSuccessStatusCode();

Expand Down
12 changes: 10 additions & 2 deletions odpc.client/src/composables/use-all-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ const fetchPage = <T>(url: string, signal?: AbortSignal | undefined) =>
.then((r) => (r.ok ? r : Promise.reject(r)))
.then((r) => r.json() as Promise<PagedResult<T>>);

const fetchAllPages = async <T>(url: string, signal?: AbortSignal | undefined): Promise<T[]> => {
export const fetchAllPages = async <T>(
url: string,
signal?: AbortSignal | undefined
): Promise<T[]> => {
const { results, next } = await fetchPage<T>(url, signal);
if (next) return [...results, ...(await fetchAllPages<T>(next, signal))];
if (next) {
const { pathname, search } = new URL(next);

return [...results, ...(await fetchAllPages<T>(pathname + search, signal))];
}

return results;
};

Expand Down
23 changes: 9 additions & 14 deletions odpc.client/src/features/publicatie/composables/use-documenten.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ref, onMounted, type ComputedRef, watchEffect } from "vue";
import { ref, onMounted, type ComputedRef } from "vue";
import { useFetchApi } from "@/api/use-fetch-api";
import { useAllPages } from "@/composables/use-all-pages";
import { fetchAllPages } from "@/composables/use-all-pages";
import toast from "@/stores/toast";
import { uploadFile } from "../service";
import type { PublicatieDocument } from "../types";

const API_URL = `/api/v1`;

export const useDocumenten = (pubUUID: ComputedRef<string | undefined>) => {
// Documenten
const files = ref<File[]>([]);
Expand All @@ -16,15 +14,12 @@ export const useDocumenten = (pubUUID: ComputedRef<string | undefined>) => {
const documentenError = ref(false);

const getDocumenten = () => {
const { data, loading, error } = useAllPages<PublicatieDocument>(
`${API_URL}/documenten/?publicatie=${pubUUID.value}`
);

watchEffect(() => {
documenten.value = data.value || documenten.value;
loadingDocumenten.value = loading.value;
documentenError.value = error.value;
});
loadingDocumenten.value = true;

fetchAllPages<PublicatieDocument>(`/api/v1/documenten/?publicatie=${pubUUID.value}`)
.then((results) => (documenten.value = results))
.catch(() => (documentenError.value = true))
.finally(() => (loadingDocumenten.value = false));
};

const submitDocumenten = async () => {
Expand Down Expand Up @@ -68,7 +63,7 @@ export const useDocumenten = (pubUUID: ComputedRef<string | undefined>) => {
data: documentData,
isFetching: loadingDocument,
error: documentError
} = useFetchApi(() => `${API_URL}/documenten${docUUID.value ? "/" + docUUID.value : ""}`, {
} = useFetchApi(() => `/api/v1/documenten${docUUID.value ? "/" + docUUID.value : ""}`, {
immediate: false
}).json<PublicatieDocument>();

Expand Down
11 changes: 5 additions & 6 deletions odpc.client/src/features/publicatie/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { ref } from "vue";
import { useFetchApi } from "@/api/use-fetch-api";
import type { Bestandsdeel, MimeType } from "./types";

const API_URL = `/api/v1`;

const mimeTypesMap = ref<Map<string, MimeType> | null>(null);

(async () => {
const { data } = await useFetchApi(() => `${API_URL}/formats`).json<MimeType[]>();
const { data } = await useFetchApi(() => "/api/v1/formats").json<MimeType[]>();

mimeTypesMap.value = new Map(data.value?.map((type) => [type.mimeType, type]));
})();
Expand All @@ -19,13 +17,14 @@ const uploadFile = async (file: File, bestandsdelen: Bestandsdeel[]) => {

try {
for (const { url, omvang } of bestandsdelen) {
const { pathname, search } = new URL(url);

const body = new FormData();
const blob = file.slice(blobStart, blobStart + omvang);

body.append("url", url);

body.append("inhoud", blob);

const { ok } = await fetch(`${API_URL}/documenten/bestandsdelen`, {
const { ok } = await fetch(pathname + search, {
method: "PUT",
body
});
Expand Down

0 comments on commit 5e5c22a

Please sign in to comment.