Skip to content

Commit

Permalink
Merge pull request #786 from bounswe/feature/BE/783/delete-annotation…
Browse files Browse the repository at this point in the history
…-by-id

Delete annotation by id
  • Loading branch information
omersafakbebek authored Dec 23, 2023
2 parents 5827dad + e8aa56c commit 331b177
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
1 change: 1 addition & 0 deletions bounswe2023group7
Submodule bounswe2023group7 added at 32b0b4
21 changes: 20 additions & 1 deletion ludos/annotation/src/controllers/annotation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
Controller,
Get,
Param,
Post
Post,
Delete,
HttpCode
} from '@nestjs/common';
import {
ApiTags,
Expand Down Expand Up @@ -114,4 +116,21 @@ export class AnnotationController {
public async getAnnotationByDate(@Param("type") type: string, @Param("itemId") itemId: string, @Param("date") date: number): Promise<AnnotationResponseDto> {
return await this.annotationService.getAnnotationByTypeAndItemIdAndDate(type, itemId, date);
}
// Delete a global resource
@ApiOkResponse({
description: 'Annotation deleted successfully',
status: 204,
})
@Delete(':source/:type/:itemId/:date')
@HttpCode(204)
async deleteAnnotationById(
@Param('source') source: string,
@Param('type') type: string,
@Param('itemId') itemId: string,
@Param('date') date: number,
): Promise<void> {
// Generate global resource id
const annotationId = `${source}/${type}/${itemId}/${date}`;
return this.annotationService.deleteAnnotationById(annotationId);
}
}
23 changes: 20 additions & 3 deletions ludos/annotation/src/repositories/annotation.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,36 @@ export class AnnotationRepository extends Repository<Annotation> {
constructor(dataSource: DataSource) {
super(Annotation, dataSource.createEntityManager());
}
public async createAnnotation(input: Partial<Annotation>): Promise<Annotation> {
public async createAnnotation(
input: Partial<Annotation>,
): Promise<Annotation> {
const annotation = this.create(input);
await this.insert(annotation);
return annotation;
}
public getAnnotationsByTypeAndItemId(type: string, itemId: string): Promise<Annotation[]> {
async deleteAnnotation(annotation: Annotation): Promise<void> {
this.delete(annotation);
}
async getAnnotationByAnnotationId(annotationId): Promise<Annotation> {
return this.findOneBy({
'id': annotationId
})
}
public getAnnotationsByTypeAndItemId(
type: string,
itemId: string,
): Promise<Annotation[]> {
return this.find({
where: {
id: Like(`%/${type}/${itemId}/%`),
},
});
}
public getAnnotationByTypeAndItemIdAndDate(type: string, itemId: string, date: number): Promise<Annotation> {
public getAnnotationByTypeAndItemIdAndDate(
type: string,
itemId: string,
date: number,
): Promise<Annotation> {
return this.findOne({
where: {
id: Like(`%/${type}/${itemId}/${date}`),
Expand Down
17 changes: 12 additions & 5 deletions ludos/annotation/src/services/annotation.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from "@nestjs/common";
import { CreateAnnotationDto } from "../dtos/annotation/request/create.dto";
import { AnnotationResponseDto } from "../dtos/annotation/response/response.dto";
import { Annotation } from "../entities/annotation.entity";
import { AnnotationRepository } from "../repositories/annotation.repository";
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateAnnotationDto } from '../dtos/annotation/request/create.dto';
import { AnnotationResponseDto } from '../dtos/annotation/response/response.dto';
import { Annotation } from '../entities/annotation.entity';
import { AnnotationRepository } from '../repositories/annotation.repository';

@Injectable()
export class AnnotationService{
Expand Down Expand Up @@ -70,4 +70,11 @@ export class AnnotationService{
async getAnnotationByTypeAndItemIdAndDate(type: string, itemId: string, date: number): Promise<Annotation> {
return await this.annotationRepository.getAnnotationByTypeAndItemIdAndDate(type, itemId, date);
}
async deleteAnnotationById(annotationId: string): Promise<void> {
const annotation = await this.annotationRepository.getAnnotationByAnnotationId(annotationId);
if (!annotation){
throw new NotFoundException('Annotation cannot be found.')
}
return this.annotationRepository.deleteAnnotation(annotation);
}
}
22 changes: 21 additions & 1 deletion ludos/backend/src/controllers/annotation.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { Body, Controller, Delete, Get, HttpCode, Param, Post } from '@nestjs/common';
import {
ApiNotFoundResponse,
ApiOkResponse,
Expand Down Expand Up @@ -204,4 +204,24 @@ export class AnnotationController {
): Promise<AnnotationResponseDto[]> {
return await this.annotationService.getAnnotationsForPost(postId);
}

@ApiOperation({ summary: 'Delete annotation with id' })
@ApiOkResponse({
description: 'Annotation is deleted successfully.',
status: 204,
})
@ApiNotFoundResponse({
description: 'Annotation with id not found',
})
@HttpCode(204)
@Delete(':source/:type/:itemId/:date')
public async deleteAnnotationById(
@Param('source') source: string,
@Param('type') type: string,
@Param('itemId') itemId: string,
@Param('date') date: number,
) {
const annotationId = `${source}/${type}/${itemId}/${date}`;
return this.annotationService.deleteAnnotationById(annotationId);
}
}
16 changes: 16 additions & 0 deletions ludos/backend/src/services/annotation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,20 @@ export class AnnotationService {
const response = await axios.get(`http://35.157.67.64:8090/post/${postId}`);
return response.data;
}

// Delete global annotation
async deleteAnnotationById(annotationId: string): Promise<void> {
try {
const response = await axios.delete(
`http://35.157.67.64:8090/${annotationId}`,
);
return response.data;
} catch (e) {
if (e?.response?.status === 404) {
throw new NotFoundException(
`Annotation with id ${annotationId} is not found.`,
);
}
}
}
}

0 comments on commit 331b177

Please sign in to comment.