Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/be/815/image annotations #816

Merged
merged 3 commits into from
Dec 24, 2023
Merged
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
18 changes: 17 additions & 1 deletion ludos/annotation/src/controllers/annotation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
Param,
Post,
Delete,
HttpCode
HttpCode,
Query
} from '@nestjs/common';
import {
ApiTags,
Expand Down Expand Up @@ -67,6 +68,21 @@ export class AnnotationController {
public async createAnnotationForPost(@Body() input: CreateAnnotationDto, @Param("postId") postId: string): Promise<AnnotationResponseDto> {
return await this.annotationService.createAnnotationForPost(input, postId);
}
@ApiOkResponse({
description: 'Annotation created successfully',
type: AnnotationResponseDto,
})
@Post('image')
public async createAnnotationForImage(@Body() input: CreateAnnotationDto): Promise<AnnotationResponseDto> {
return await this.annotationService.createAnnotationForImage(input);
}
@ApiOkResponse({
type: [AnnotationResponseDto],
})
@Get('image')
public async getAnnotationsForImage(@Query("imageUrl") imageUrl: string): Promise<AnnotationResponseDto[]> {
return await this.annotationService.getAnnotationsForImage(imageUrl);
}
@ApiOkResponse({
type: [AnnotationResponseDto],
})
Expand Down
25 changes: 22 additions & 3 deletions ludos/annotation/src/dtos/annotation/request/create.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";
import {Expose, Type} from "class-transformer";
import {IsString, ValidateNested, IsNumber} from "class-validator";
import {IsString, ValidateNested, IsNumber, IsOptional} from "class-validator";


class AnnotationTargetSelectorDto {
Expand All @@ -20,11 +20,30 @@ class AnnotationTargetDto {
@IsString()
source: string;

@ApiProperty()
@ApiProperty({required: false})
@Expose()
@Type(() => AnnotationTargetSelectorDto)
@ValidateNested()
selector: AnnotationTargetSelectorDto;
@IsOptional()
selector?: AnnotationTargetSelectorDto;

@ApiProperty({required: false})
@Expose()
@IsString()
@IsOptional()
type?: string;

@ApiProperty({required: false})
@Expose()
@IsString()
@IsOptional()
id?: string;

@ApiProperty({required: false})
@Expose()
@IsString()
@IsOptional()
format?: string;
}
export class CreateAnnotationDto {
@ApiProperty()
Expand Down
7 changes: 5 additions & 2 deletions ludos/annotation/src/entities/annotation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Entity, Column, PrimaryColumn } from 'typeorm';
export interface AnnotationTarget {
source: string;
selector: {
selector?: {
start: number;
end: number;
}
};
type?: string;
id?: string;
format?: string;
}
@Entity("annotations")
export class Annotation {
Expand Down
28 changes: 27 additions & 1 deletion ludos/annotation/src/services/annotation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export class AnnotationService{
...input,
});
}
async createAnnotationForImage(input: CreateAnnotationDto): Promise<AnnotationResponseDto> {
const id = "35.157.67.64:8090/image/" + stringToHash(input.target.source) + "/" + Date.now();
return await this.annotationRepository.createAnnotation({
id,
...input,
});
}
async getAnnotationsForImage(imageUrl: string): Promise<AnnotationResponseDto[]> {
return await this.annotationRepository.getAnnotationsByTypeAndItemId("image", stringToHash(imageUrl).toString());
}
async getAnnotationsForGameBio(gameId: string): Promise<AnnotationResponseDto[]> {
return await this.annotationRepository.getAnnotationsByTypeAndItemId("gamebio", gameId);
}
Expand Down Expand Up @@ -88,4 +98,20 @@ export class AnnotationService{
...input,
});
}
}
}


function stringToHash(string: string) {

let hash = 0;

if (string.length == 0) return hash;

for (let i = 0; i < string.length; i++) {
const char = string.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}

return hash;
}
38 changes: 35 additions & 3 deletions ludos/backend/src/controllers/annotation.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Body, Controller, Delete, Get, HttpCode, Param, Post } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
HttpCode,
Param,
Post,
Query,
} from '@nestjs/common';
import {
ApiNotFoundResponse,
ApiOkResponse,
Expand Down Expand Up @@ -126,6 +135,27 @@ export class AnnotationController {
): Promise<AnnotationResponseDto> {
return await this.annotationService.createAnnotationForPost(input, postId);
}
@ApiOperation({ summary: 'Create Annotation For Image Endpoint' })
@ApiOkResponse({
description: 'Annotation created successfully',
type: AnnotationResponseDto,
})
@Post('image')
public async createAnnotationForImage(
@Body() input: CreateAnnotationDto,
): Promise<AnnotationResponseDto> {
return await this.annotationService.createAnnotationForImage(input);
}
@ApiOperation({ summary: 'Get Annotations For Image Endpoint' })
@ApiOkResponse({
type: [AnnotationResponseDto],
})
@Get('image')
public async getAnnotationsForImage(
@Query('imageUrl') imageUrl: string,
): Promise<AnnotationResponseDto[]> {
return await this.annotationService.getAnnotationsForImage(imageUrl);
}
@ApiOperation({ summary: 'Get Annotations For Game Bio of Game Endpoint' })
@ApiOkResponse({
type: [AnnotationResponseDto],
Expand Down Expand Up @@ -225,7 +255,6 @@ export class AnnotationController {
return this.annotationService.deleteAnnotationById(annotationId);
}


@ApiOperation({ summary: 'Create Annotation For Comment Endpoint' })
@ApiOkResponse({
description: 'Annotation created successfully',
Expand All @@ -239,7 +268,10 @@ export class AnnotationController {
@Body() input: CreateAnnotationDto,
@Param('commentId') commentId: string,
): Promise<AnnotationResponseDto> {
return await this.annotationService.createAnnotationForComment(input, commentId);
return await this.annotationService.createAnnotationForComment(
input,
commentId,
);
}

@ApiOperation({ summary: 'Get Annotations For Comment Endpoint' })
Expand Down
5 changes: 4 additions & 1 deletion ludos/backend/src/controllers/comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export class CommentController {
@Req() req: AuthorizedRequest,
@Param('parentId') parentId: string,
) {
return await this.commentService.getCommentsByParent(req.user?.id, parentId);
return await this.commentService.getCommentsByParent(
req.user?.id,
parentId,
);
}

@ApiOperation({ summary: 'Comment on a post/comment/review' })
Expand Down
1 change: 0 additions & 1 deletion ludos/backend/src/controllers/review.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,4 @@ export class ReviewController {
);
return reviews;
}

}
1 change: 0 additions & 1 deletion ludos/backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,4 @@ export class UserController {
public async getUserIdByUsername(@Param('userId') username: string) {
return await this.userService.getUserIdByUsername(username);
}

}
30 changes: 27 additions & 3 deletions ludos/backend/src/dtos/annotation/request/create.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose, Type } from 'class-transformer';
import { IsString, ValidateNested, IsNumber } from 'class-validator';
import {
IsString,
ValidateNested,
IsNumber,
IsOptional,
} from 'class-validator';

class AnnotationTargetSelectorDto {
@ApiProperty()
Expand All @@ -19,11 +24,30 @@ class AnnotationTargetDto {
@IsString()
source: string;

@ApiProperty()
@ApiProperty({ required: false })
@Expose()
@Type(() => AnnotationTargetSelectorDto)
@ValidateNested()
selector: AnnotationTargetSelectorDto;
@IsOptional()
selector?: AnnotationTargetSelectorDto;

@ApiProperty({ required: false })
@Expose()
@IsString()
@IsOptional()
type?: string;

@ApiProperty({ required: false })
@Expose()
@IsString()
@IsOptional()
id?: string;

@ApiProperty({ required: false })
@Expose()
@IsString()
@IsOptional()
format?: string;
}
export class CreateAnnotationDto {
@ApiProperty()
Expand Down
1 change: 0 additions & 1 deletion ludos/backend/src/dtos/post/request/create.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export class PostCreateDto {
@IsOptional()
tags: string[];


@ApiProperty({
description: 'Upcoming Title',
})
Expand Down
2 changes: 1 addition & 1 deletion ludos/backend/src/dtos/post/upcomingTitle.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export class UpcomingTitleDto {

@IsString()
demoLink: string;
}
}
10 changes: 6 additions & 4 deletions ludos/backend/src/repositories/post.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ export class PostRepository extends Repository<Post> {
queryBuilder.andWhere('posts.userId = :ownerUserId', { ownerUserId });
}
if (isUpcomingTitle) {
console.log("seeeennnn");
queryBuilder.andWhere('posts."upcomingTitle"->>\'isUpcomingTitle\' = :isUpcomingTitle', {
isUpcomingTitle: isUpcomingTitle.toString(),
});
queryBuilder.andWhere(
'posts."upcomingTitle"->>\'isUpcomingTitle\' = :isUpcomingTitle',
{
isUpcomingTitle: isUpcomingTitle.toString(),
},
);
}
if (userId && isLiked) {
const subQuery = this.createQueryBuilder()
Expand Down
18 changes: 17 additions & 1 deletion ludos/backend/src/services/annotation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ export class AnnotationService {
);
return response.data;
}
async createAnnotationForImage(
input: CreateAnnotationDto,
): Promise<AnnotationResponseDto> {
const response = await axios.post(`http://35.157.67.64:8090/image`, input);
return response.data;
}
async getAnnotationsForImage(
imageUrl: string,
): Promise<AnnotationResponseDto[]> {
const response = await axios.get(
`http://35.157.67.64:8090/image?imageUrl=${imageUrl}`,
);
return response.data;
}
async getAnnotationsForGameBio(
gameId: string,
): Promise<AnnotationResponseDto[]> {
Expand Down Expand Up @@ -209,7 +223,9 @@ export class AnnotationService {
throw new NotFoundException(`comment with id ${commentId} not found`);
}

const response = await axios.get(`http://35.157.67.64:8090/comment/${commentId}`);
const response = await axios.get(
`http://35.157.67.64:8090/comment/${commentId}`,
);
return response.data;
}
}
2 changes: 0 additions & 2 deletions ludos/backend/src/services/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class CommentService {
userId: string | null,
commentId: string,
): Promise<GetCommentResponseDto> {

if (!userId) {
const user = await this.userRepository.findUserById(userId);

Expand Down Expand Up @@ -75,7 +74,6 @@ export class CommentService {
userId: string | null,
parentId: string,
): Promise<GetCommentResponseDto[]> {

if (!userId) {
const user = await this.userRepository.findUserById(userId);

Expand Down
13 changes: 8 additions & 5 deletions ludos/backend/src/services/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ export class ReviewService {
likedUserCount: likedUserCount,
dislikedUserCount: dislikedUserCount,
isBelongToUser: userId && review.user.id == userId,
isLikedByUser: userId && review.likedUsers.some((user) => user.id === userId),
isDislikedByUser: userId && review.dislikedUsers.some((user) => user.id === userId),
isLikedByUser:
userId && review.likedUsers.some((user) => user.id === userId),
isDislikedByUser:
userId && review.dislikedUsers.some((user) => user.id === userId),
};
}

Expand All @@ -259,7 +261,6 @@ export class ReviewService {
throw new NotFoundException('Game Not Found!');
}


const reviews = await this.reviewRepository.findReviewsByGame(game);

const mappedReviews: ReviewGetInfoResponseDto[] = reviews.map((review) => ({
Expand All @@ -273,8 +274,10 @@ export class ReviewService {
likedUserCount: review.likedUsers.length,
dislikedUserCount: review.dislikedUsers.length,
isBelongToUser: userId && review.user.id == userId,
isLikedByUser: userId && review.likedUsers.some((user) => user.id === userId),
isDislikedByUser: userId && review.dislikedUsers.some((user) => user.id === userId),
isLikedByUser:
userId && review.likedUsers.some((user) => user.id === userId),
isDislikedByUser:
userId && review.dislikedUsers.some((user) => user.id === userId),
}));
return mappedReviews;
}
Expand Down
2 changes: 1 addition & 1 deletion ludos/backend/src/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SearchService {
undefined,
undefined,
userId,
)
);
return {
users: users.items,
games: games.items,
Expand Down
Loading
Loading