Skip to content

Commit

Permalink
Feat/owner id filter for resources (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Herrmann authored Nov 20, 2023
2 parents 7cde688 + 4bc522d commit 1632085
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 54 deletions.
5 changes: 3 additions & 2 deletions src/resources/attractions/AttractionsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export class AttractionsRoutes {
.get(AttractionsRoutes.basePath + "/", (req: express.Request, res: express.Response) => {
const asReference = req.query.asReference;
const pagination: Pagination = getPagination(req);
const curatedBy = req.query.curatedBy as string;

if (asReference) {
this.attractionsController.listAttractionsAsReference(res, pagination);
this.attractionsController.listAttractionsAsReference(res, pagination, curatedBy);
} else {
this.attractionsController.listAttractions(res, pagination);
this.attractionsController.listAttractions(res, pagination, curatedBy);
}
})
.post(
Expand Down
19 changes: 13 additions & 6 deletions src/resources/attractions/controllers/AttractionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import { AuthUser } from "../../../generated/models/AuthUser.generated";
export class AttractionsController implements ResourcePermissionController {
constructor(public attractionsService: AttractionsService) {}

async listAttractions(res: Response, pagination: Pagination) {
const attractions = await this.attractionsService.list(pagination);
const totalCount = await this.attractionsService.countAttractions();
getCuratedByFilter(curatedBy?: string) {
return curatedBy ? { "curator.referenceId": curatedBy } : undefined;
}

async listAttractions(res: Response, pagination: Pagination, curatedBy?: string) {
const attractions = await this.attractionsService.list(pagination, this.getCuratedByFilter(curatedBy));
const totalCount = await this.attractionsService.countAttractions(this.getCuratedByFilter(curatedBy));
res.status(200).send(
new SuccessResponseBuilder<GetAttractionsResponse>()
.okResponse({
Expand All @@ -38,9 +42,12 @@ export class AttractionsController implements ResourcePermissionController {
);
}

async listAttractionsAsReference(res: Response, pagination: Pagination) {
const attractionsReferences = await this.attractionsService.listAsReferences(pagination);
const totalCount = await this.attractionsService.countAttractions();
async listAttractionsAsReference(res: Response, pagination: Pagination, curatedBy?: string) {
const attractionsReferences = await this.attractionsService.listAsReferences(
pagination,
this.getCuratedByFilter(curatedBy),
);
const totalCount = await this.attractionsService.countAttractions(this.getCuratedByFilter(curatedBy));

res.status(200).send(
new SuccessResponseBuilder<GetAttractionsResponse>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { UpdateAttractionRequest } from "../../../generated/models/UpdateAttract
const log: debug.IDebugger = debug("app:attractions-repository");

export interface AttractionsRepository {
getAttractions(pagination?: Pagination): Promise<Attraction[]>;
getAttractions(pagination?: Pagination, filter?: Filter): Promise<Attraction[]>;

getAttractionsAsReferences(pagination?: Pagination): Promise<Reference[]>;
getAttractionsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]>;

searchAttractions(filter?: Filter, pagination?: Pagination): Promise<Attraction[]>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export class MongoDBAttractionsRepository implements AttractionsRepository {
return this.get(filter, projection, undefined);
}

async getAttractions(pagination?: Pagination): Promise<Attraction[]> {
return this.get(undefined, undefined, pagination);
async getAttractions(pagination?: Pagination, filter?: Filter): Promise<Attraction[]> {
return this.get(filter, undefined, pagination);
}

async getAttractionsAsReferences(pagination?: Pagination): Promise<Reference[]> {
return this.get(undefined, getAttractionReferenceProjection(), pagination);
async getAttractionsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]> {
return this.get(filter, getAttractionReferenceProjection(), pagination);
}

async addAttraction(createAttraction: CreateAttractionRequest): Promise<Reference | null> {
Expand Down
8 changes: 4 additions & 4 deletions src/resources/attractions/services/AttractionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class AttractionsService {
@Inject("EventsRepository") public eventsRepository: EventsRepository,
) {}

async list(pagination?: Pagination): Promise<Attraction[]> {
return this.attractionsRepository.getAttractions(pagination);
async list(pagination?: Pagination, searchFilter?: Filter): Promise<Attraction[]> {
return this.attractionsRepository.getAttractions(pagination, searchFilter);
}

async listAsReferences(pagination?: Pagination): Promise<Reference[]> {
return this.attractionsRepository.getAttractionsAsReferences(pagination);
async listAsReferences(pagination?: Pagination, searchFilter?: Filter): Promise<Reference[]> {
return this.attractionsRepository.getAttractionsAsReferences(pagination, searchFilter);
}

async listForAdmins(pagination?: Pagination): Promise<AdminAttraction[]> {
Expand Down
5 changes: 3 additions & 2 deletions src/resources/events/EventsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ export class EventsRoutes {
.get(EventsRoutes.basePath + "/", (req: express.Request, res: express.Response) => {
const asReference = req.query.asReference;
const pagination: Pagination = getPagination(req);
const organizedBy = req.query.organizedBy as string;

if (asReference) {
this.eventsController.listEventsAsReference(res, pagination);
this.eventsController.listEventsAsReference(res, pagination, organizedBy);
} else {
this.eventsController.listEvents(res, pagination);
this.eventsController.listEvents(res, pagination, organizedBy);
}
})
.post(
Expand Down
19 changes: 13 additions & 6 deletions src/resources/events/controllers/EventsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ const log: debug.IDebugger = debug("app:events-controller");
export class EventsController implements ResourcePermissionController {
constructor(public eventsService: EventsService) {}

async listEvents(res: express.Response, pagination: Pagination) {
const events = await this.eventsService.list(pagination);
const totalCount = await this.eventsService.countEvents();
getOrganizedByFilter(organizedBy?: string) {
return organizedBy ? { "organizer.referenceId": organizedBy } : undefined;
}

async listEvents(res: express.Response, pagination: Pagination, organizedBy?: string) {
const events = await this.eventsService.list(pagination, this.getOrganizedByFilter(organizedBy));
const totalCount = await this.eventsService.countEvents(this.getOrganizedByFilter(organizedBy));
res.status(200).send(
new SuccessResponseBuilder<GetEventsResponse>()
.okResponse({
Expand All @@ -43,9 +47,12 @@ export class EventsController implements ResourcePermissionController {
);
}

async listEventsAsReference(res: express.Response, pagination: Pagination) {
const eventsReferences = await this.eventsService.listAsReferences(pagination);
const totalCount = await this.eventsService.countEvents();
async listEventsAsReference(res: express.Response, pagination: Pagination, organizedBy?: string) {
const eventsReferences = await this.eventsService.listAsReferences(
pagination,
this.getOrganizedByFilter(organizedBy),
);
const totalCount = await this.eventsService.countEvents(this.getOrganizedByFilter(organizedBy));
res.status(200).send(
new SuccessResponseBuilder<GetEventsResponse>()
.okResponse({
Expand Down
4 changes: 2 additions & 2 deletions src/resources/events/repositories/EventsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { UpdateEventRequest } from "../../../generated/models/UpdateEventRequest
const log: debug.IDebugger = debug("app:events-repository");

export interface EventsRepository {
getEvents(pagination?: Pagination): Promise<Event[]>;
getEvents(pagination?: Pagination, filter?: Filter): Promise<Event[]>;

getEventsAsReferences(pagination?: Pagination): Promise<Reference[]>;
getEventsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]>;

searchEvents(filter?: Filter, pagination?: Pagination): Promise<Event[]>;

Expand Down
8 changes: 4 additions & 4 deletions src/resources/events/repositories/MongoDBEventsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export class MongoDBEventsRepository implements EventsRepository {
return events.countDocuments(filter);
}

async getEvents(pagination?: Pagination): Promise<Event[]> {
return this.get(undefined, undefined, pagination);
async getEvents(pagination?: Pagination, filter?: Filter): Promise<Event[]> {
return this.get(filter, undefined, pagination);
}

async getEventsAsReferences(pagination?: Pagination): Promise<Reference[]> {
return this.get(undefined, getEventReferenceProjection(), pagination) as unknown as Promise<Reference[]>;
async getEventsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]> {
return this.get(filter, getEventReferenceProjection(), pagination) as unknown as Promise<Reference[]>;
}

async addEvent(createEvent: CreateEventRequest): Promise<Reference | null> {
Expand Down
8 changes: 4 additions & 4 deletions src/resources/events/services/EventsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class EventsService {

constructor(@Inject("EventsRepository") public eventsRepository: EventsRepository) {}

async list(pagination?: Pagination) {
return this.eventsRepository.getEvents(pagination);
async list(pagination?: Pagination, searchFilter?: Filter) {
return this.eventsRepository.getEvents(pagination, searchFilter);
}

async listAsReferences(pagination?: Pagination) {
return this.eventsRepository.getEventsAsReferences(pagination);
async listAsReferences(pagination?: Pagination, searchFilter?: Filter) {
return this.eventsRepository.getEventsAsReferences(pagination, searchFilter);
}

async search(
Expand Down
5 changes: 3 additions & 2 deletions src/resources/locations/LocationsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export class LocationsRoutes {
.get(LocationsRoutes.basePath + "/", (req: express.Request, res: express.Response) => {
const asReference = req.query.asReference;
const pagination: Pagination = getPagination(req);
const managedBy = req.query.managedBy as string;

if (asReference) {
this.locationsController.listLocationsAsReference(res, pagination);
this.locationsController.listLocationsAsReference(res, pagination, managedBy);
} else {
this.locationsController.listLocations(res, pagination);
this.locationsController.listLocations(res, pagination, managedBy);
}
})
.post(
Expand Down
20 changes: 14 additions & 6 deletions src/resources/locations/controllers/LocationsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ const log: debug.IDebugger = debug("app:locations-controller");
@Service()
export class LocationsController implements ResourcePermissionController {
constructor(public locationsService: LocationsService) {}
async listLocations(res: express.Response, pagination: Pagination) {
const locations = await this.locationsService.list(pagination);
const totalCount = await this.locationsService.countLocations();

getManagedByFilter(managedBy?: string) {
return managedBy ? { "manager.referenceId": managedBy } : undefined;
}

async listLocations(res: express.Response, pagination: Pagination, managedBy?: string) {
const locations = await this.locationsService.list(pagination, this.getManagedByFilter(managedBy));
const totalCount = await this.locationsService.countLocations(this.getManagedByFilter(managedBy));

if (locations) {
res.status(200).send(
Expand All @@ -43,9 +48,12 @@ export class LocationsController implements ResourcePermissionController {
}
}

async listLocationsAsReference(res: express.Response, pagination: Pagination) {
const locationsReferences = await this.locationsService.listAsReferences(pagination);
const totalCount = await this.locationsService.countLocations();
async listLocationsAsReference(res: express.Response, pagination: Pagination, managedBy?: string) {
const locationsReferences = await this.locationsService.listAsReferences(
pagination,
this.getManagedByFilter(managedBy),
);
const totalCount = await this.locationsService.countLocations(this.getManagedByFilter(managedBy));

if (locationsReferences) {
res.status(200).send(
Expand Down
4 changes: 2 additions & 2 deletions src/resources/locations/repositories/LocationsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { UpdateLocationRequest } from "../../../generated/models/UpdateLocationR
const log: debug.IDebugger = debug("app:locations-repository");

export interface LocationsRepository {
getLocations(pagination?: Pagination): Promise<Location[]>;
getLocations(pagination?: Pagination, filter?: Filter): Promise<Location[]>;

getLocationsAsReferences(pagination?: Pagination): Promise<Reference[]>;
getLocationsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]>;

searchLocations(filter?: Filter, pagination?: Pagination): Promise<Location[]>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export class MongoDBLocationsRepository implements LocationsRepository {
return this.get(filter, undefined, pagination);
}

async getLocations(pagination?: Pagination): Promise<Location[]> {
return this.get(undefined, undefined, pagination);
async getLocations(pagination?: Pagination, filter?: Filter): Promise<Location[]> {
return this.get(filter, undefined, pagination);
}

async getLocationsAsReferences(pagination?: Pagination): Promise<Reference[]> {
return this.get(undefined, getLocationReferenceProjection(), pagination);
async getLocationsAsReferences(pagination?: Pagination, filter?: Filter): Promise<Reference[]> {
return this.get(filter, getLocationReferenceProjection(), pagination);
}

async addLocation(createLocation: CreateLocationRequest): Promise<Reference | null> {
Expand Down
8 changes: 4 additions & 4 deletions src/resources/locations/services/LocationsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { CreateLocationRequest } from "../../../generated/models/CreateLocationR
export class LocationsService {
constructor(@Inject("LocationsRepository") public locationsRepository: LocationsRepository) {}

async list(pagination?: Pagination) {
return this.locationsRepository.getLocations(pagination);
async list(pagination?: Pagination, searchFilter?: Filter) {
return this.locationsRepository.getLocations(pagination, searchFilter);
}

async listAsReferences(pagination?: Pagination) {
return this.locationsRepository.getLocationsAsReferences(pagination);
async listAsReferences(pagination?: Pagination, searchFilter?: Filter) {
return this.locationsRepository.getLocationsAsReferences(pagination, searchFilter);
}

async create(resource: CreateLocationRequest, authUser?: AuthUser): Promise<Reference | null> {
Expand Down
18 changes: 18 additions & 0 deletions src/schemas/kulturdaten.berlin.openapi.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: curatedBy
in: query
required: false
description: Returns only the Attractions curated by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down Expand Up @@ -339,6 +345,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: organizedBy
in: query
required: false
description: Returns only the events organized by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down Expand Up @@ -711,6 +723,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: managedBy
in: query
required: false
description: Returns only the locations managed by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down
18 changes: 18 additions & 0 deletions src/schemas/kulturdaten.berlin.openapi.generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: curatedBy
in: query
required: false
description: Returns only the Attractions curated by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down Expand Up @@ -378,6 +384,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: organizedBy
in: query
required: false
description: Returns only the events organized by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down Expand Up @@ -732,6 +744,12 @@ paths:
- $ref: "#/components/parameters/page"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/asReference"
- name: managedBy
in: query
required: false
description: Returns only the locations managed by the entered Organization-ID
schema:
type: string
responses:
"200":
description: OK
Expand Down

0 comments on commit 1632085

Please sign in to comment.