Skip to content

Commit

Permalink
Merge pull request #23 from navikt/innsatsgruppefilter
Browse files Browse the repository at this point in the history
MegaBranch™
  • Loading branch information
taniaholst authored Mar 24, 2022
2 parents 76a8fce + 91e6593 commit 3ac65ac
Show file tree
Hide file tree
Showing 76 changed files with 1,770 additions and 703 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.mulighetsrommet.api.routes

import io.ktor.application.call
import io.ktor.features.BadRequestException
import io.ktor.http.*
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
Expand All @@ -17,14 +18,22 @@ import no.nav.mulighetsrommet.api.services.TiltakstypeService
import org.koin.ktor.ext.inject

// TODO: Må lage noe felles validering her etterhvert
fun Parameters.parseList(parameter: String): List<String> {
return entries().filter { it.key == parameter }.flatMap { it.value }
}

fun Route.tiltakstypeRoutes() {

val tiltakstypeService: TiltakstypeService by inject()
val tiltaksgjennomforingService: TiltaksgjennomforingService by inject()

get("/api/tiltakstyper") {
val tiltakstyper = tiltakstypeService.getTiltakstyper()
call.respond(tiltakstyper)
val search = call.request.queryParameters["search"]

val innsatsgrupper = call.request.queryParameters.parseList("innsatsgrupper").map { Integer.parseInt(it) }

val items = tiltakstypeService.getTiltakstyper(innsatsgrupper, search)
call.respond(items)
}
get("/api/tiltakstyper/{tiltakskode}") {
runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import no.nav.mulighetsrommet.api.database.DatabaseFactory
import no.nav.mulighetsrommet.api.domain.Tiltakskode
import no.nav.mulighetsrommet.api.domain.Tiltakstype
import no.nav.mulighetsrommet.api.domain.TiltakstypeTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.update
import org.jetbrains.exposed.sql.*

class TiltakstypeService(private val db: DatabaseFactory) {

suspend fun getTiltakstyper(): List<Tiltakstype> {
suspend fun getTiltakstyper(innsatsgruppe: List<Int>?, search: String?): List<Tiltakstype> {
val rows = db.dbQuery {
val query = TiltakstypeTable
.selectAll()
.orderBy(TiltakstypeTable.id to SortOrder.ASC)

innsatsgruppe?.let { query.andWhere { TiltakstypeTable.innsatsgruppeId inList it } }
search?.let { query.andWhere { TiltakstypeTable.navn like ("%$it%") } }

query.toList()
}
return rows.map { row ->
Expand Down
29 changes: 28 additions & 1 deletion backend/src/main/resources/web/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ paths:
tags:
- mulighetsrommet
operationId: getTiltakstyper
parameters:
- in: query
name: search
schema:
type: string
description: Search for tiltakstyper
- in: query
name: innsatsgrupper
schema:
type: array
items:
type: number
description: Innsatsgruppefilter
style: form
explode: false
responses:
200:
description: Array of tiltakstyper.
Expand All @@ -61,7 +76,7 @@ paths:
schema:
$ref: "#/components/schemas/Tiltakstype"
404:
description: the specified tiltakstype was not found.
description: The specified tiltakstype was not found.
content:
text/plain:
schema:
Expand Down Expand Up @@ -156,6 +171,18 @@ components:
- beskrivelse
Tiltakstype:
type: object
required:
- id
- innsatsgruppe
- sanityId
- navn
- tiltakskode
- fraDato
- tilDato
- createdBy
- createdAt
- updatedBy
- updatedAt
properties:
id:
type: integer
Expand Down
5 changes: 2 additions & 3 deletions frontend/mulighetsrommet-api/src/core/ApiRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
/* eslint-disable */
export type ApiRequestOptions = {
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
readonly url: string;
readonly path?: Record<string, any>;
readonly path: string;
readonly cookies?: Record<string, any>;
readonly headers?: Record<string, any>;
readonly query?: Record<string, any>;
Expand All @@ -13,4 +12,4 @@ export type ApiRequestOptions = {
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
};
}
2 changes: 1 addition & 1 deletion frontend/mulighetsrommet-api/src/core/ApiResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export type ApiResult = {
readonly status: number;
readonly statusText: string;
readonly body: any;
};
}
44 changes: 15 additions & 29 deletions frontend/mulighetsrommet-api/src/core/CancelablePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* eslint-disable */
export class CancelError extends Error {

constructor(message: string) {
super(message);
constructor(reason: string = 'Promise was canceled') {
super(reason);
this.name = 'CancelError';
}

Expand All @@ -14,8 +14,7 @@ export class CancelError extends Error {
}

export interface OnCancel {
readonly isResolved: boolean;
readonly isRejected: boolean;
readonly isPending: boolean;
readonly isCancelled: boolean;

(cancelHandler: () => void): void;
Expand All @@ -24,8 +23,7 @@ export interface OnCancel {
export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]: string;

#isResolved: boolean;
#isRejected: boolean;
#isPending: boolean;
#isCancelled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
Expand All @@ -39,43 +37,33 @@ export class CancelablePromise<T> implements Promise<T> {
onCancel: OnCancel
) => void
) {
this.#isResolved = false;
this.#isRejected = false;
this.#isPending = true;
this.#isCancelled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;

const onResolve = (value: T | PromiseLike<T>): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
if (!this.#isCancelled) {
this.#isPending = false;
this.#resolve?.(value);
}
this.#isResolved = true;
this.#resolve?.(value);
};

const onReject = (reason?: any): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this.#isRejected = true;
this.#isPending = false;
this.#reject?.(reason);
};

const onCancel = (cancelHandler: () => void): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
if (this.#isPending) {
this.#cancelHandlers.push(cancelHandler);
}
this.#cancelHandlers.push(cancelHandler);
};

Object.defineProperty(onCancel, 'isResolved', {
get: (): boolean => this.#isResolved,
});

Object.defineProperty(onCancel, 'isRejected', {
get: (): boolean => this.#isRejected,
Object.defineProperty(onCancel, 'isPending', {
get: (): boolean => this.#isPending,
});

Object.defineProperty(onCancel, 'isCancelled', {
Expand Down Expand Up @@ -104,7 +92,7 @@ export class CancelablePromise<T> implements Promise<T> {
}

public cancel(): void {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
if (!this.#isPending || this.#isCancelled) {
return;
}
this.#isCancelled = true;
Expand All @@ -114,12 +102,10 @@ export class CancelablePromise<T> implements Promise<T> {
cancelHandler();
}
} catch (error) {
console.warn('Cancellation threw an error', error);
this.#reject?.(error);
return;
}
}
this.#cancelHandlers.length = 0;
this.#reject?.(new CancelError('Request aborted'));
}

public get isCancelled(): boolean {
Expand Down
6 changes: 3 additions & 3 deletions frontend/mulighetsrommet-api/src/core/OpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ApiRequestOptions } from './ApiRequestOptions';
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
type Headers = Record<string, string>;

export type OpenAPIConfig = {
type Config = {
BASE: string;
VERSION: string;
WITH_CREDENTIALS: boolean;
Expand All @@ -16,9 +16,9 @@ export type OpenAPIConfig = {
PASSWORD?: string | Resolver<string>;
HEADERS?: Headers | Resolver<Headers>;
ENCODE_PATH?: (path: string) => string;
};
}

export const OpenAPI: OpenAPIConfig = {
export const OpenAPI: Config = {
BASE: '',
VERSION: '1.0.0',
WITH_CREDENTIALS: false,
Expand Down
Loading

0 comments on commit 3ac65ac

Please sign in to comment.