Skip to content
Draft
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
2 changes: 2 additions & 0 deletions apps/entity-service/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DisturbancesController } from "./entities/disturbances.controller";
import { DisturbanceService } from "./entities/disturbance.service";
import { OptionLabelsController } from "./forms/option-labels.controller";
import { LinkedFieldsController } from "./forms/linked-fields.controller";
import { MediasController } from "./file/medias.controller";

@Module({
imports: [SentryModule.forRoot(), CommonModule, HealthModule],
Expand All @@ -31,6 +32,7 @@ import { LinkedFieldsController } from "./forms/linked-fields.controller";
controllers: [
ProjectPitchesController,
ImpactStoriesController,
MediasController,
TasksController,
FileUploadController,
TreesController,
Expand Down
8 changes: 7 additions & 1 deletion apps/entity-service/src/entities/dto/media-query.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsOptional } from "class-validator";
import { IsOptional, IsUUID } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { TransformBooleanString } from "@terramatch-microservices/common/decorators/transform-boolean-string.decorator";
import { EntityQueryDto } from "./entity-query.dto";
Expand Down Expand Up @@ -31,3 +31,9 @@ export class MediaQueryDto extends EntityQueryDto {
@TransformBooleanString()
isCover?: boolean;
}

export class SingleMediaDto {
@IsUUID()
@ApiProperty({ description: "Media UUID for media to retrieve" })
uuid: string;
}
51 changes: 51 additions & 0 deletions apps/entity-service/src/file/medias.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
BadRequestException,
Body,
Controller,
Get,
NotFoundException,
Param,
Patch,
UnauthorizedException
} from "@nestjs/common";
import { ExceptionResponse, JsonApiResponse } from "@terramatch-microservices/common/decorators";
import { MediaDto } from "../entities/dto/media.dto";
import { ApiOperation } from "@nestjs/swagger";
import { SingleMediaDto } from "../entities/dto/media-query.dto";
import { MediaService } from "@terramatch-microservices/common/media/media.service";
import { PolicyService } from "@terramatch-microservices/common/policies/policy.service";
import { MediaUpdateBody } from "@terramatch-microservices/common/dto/media-update.dto";

@Controller("entities/v3/medias")
export class MediasController {
constructor(private readonly mediaService: MediaService, private readonly policyService: PolicyService) {}

@Get(":uuid")
@ApiOperation({
operationId: "mediaGet",
summary: "Get a media by uuid"
})
@JsonApiResponse({ data: MediaDto })
@ExceptionResponse(UnauthorizedException, { description: "Authentication failed." })
@ExceptionResponse(NotFoundException, { description: "Resource not found." })
async mediaGet(@Param() { uuid }: SingleMediaDto) {
return this.mediaService.getMedia(uuid);
}

@Patch(":uuid")
@ApiOperation({
operationId: "mediaUpdate",
summary: "Update a media by uuid"
})
@JsonApiResponse({ data: MediaDto })
@ExceptionResponse(UnauthorizedException, { description: "Authentication failed." })
@ExceptionResponse(NotFoundException, { description: "Resource not found." })
@ExceptionResponse(BadRequestException, { description: "Invalid request." })
async mediaUpdate(@Param() { uuid }: SingleMediaDto, @Body() updatePayload: MediaUpdateBody) {
const media = await this.mediaService.getMedia(uuid);

// await this.policyService.authorize("update", media);

return this.mediaService.updateMedia(media, updatePayload);
}
}
34 changes: 34 additions & 0 deletions libs/common/src/lib/dto/media-update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ApiProperty } from "@nestjs/swagger";
import { CreateDataDto, JsonApiBodyDto } from "../util/json-api-update-dto";
import { IsBoolean, IsOptional, IsString } from "class-validator";

export class MediaUpdateAttributes {
@IsOptional()
@IsString()
@ApiProperty({ description: "The name of the media" })
name?: string;

@IsOptional()
@IsString()
@ApiProperty({ description: "The title of the media" })
title?: string;

@IsOptional()
@IsString()
@ApiProperty({ description: "The photographer of the media" })
photographer?: string;

@IsOptional()
@IsBoolean()
@ApiProperty({ description: "The public status of the media" })
isPublic?: boolean;

@IsOptional()
@IsBoolean()
@ApiProperty({ description: "The cover of the project" })
isCover?: boolean;
}

export class MediaUpdateBody extends JsonApiBodyDto(
class MediaData extends CreateDataDto("medias", MediaUpdateAttributes) {}
) {}
17 changes: 16 additions & 1 deletion libs/common/src/lib/media/media.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Injectable } from "@nestjs/common";
import { Injectable, NotFoundException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Media } from "@terramatch-microservices/database/entities";

import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { TMLogger } from "../util/tm-logger";
import { MediaUpdateBody } from "../dto/media-update.dto";
import "multer";

@Injectable()
Expand All @@ -21,6 +23,19 @@ export class MediaService {
});
}

async getMedia(uuid: string) {
const media = await Media.findOne({
where: { uuid }
});
if (media == null) throw new NotFoundException();

return media;
}

async updateMedia(media: Media, updatePayload: MediaUpdateBody) {
await media.update(updatePayload.data.attributes);
}

async uploadFile(file: Express.Multer.File, bucket: string = this.configService.get<string>("AWS_BUCKET") ?? "") {
const { buffer, originalname, mimetype } = file;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"entity": "nx serve entity-service --no-cloud",
"research": "nx serve research-service --no-cloud",
"unified-database": "nx serve unified-database-service --no-cloud",
"fe-services": "nx run-many -t serve --parallel=10 --no-cloud --projects user-service job-service entity-service research-service dashboard-service",
Expand Down