Skip to content

Commit 98bd960

Browse files
committed
Possible implementation
1 parent 250bd5a commit 98bd960

16 files changed

+2490
-75
lines changed

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 120
5+
}

src/api/index.ts

-2
This file was deleted.

src/api/methods.ts

-11
This file was deleted.

src/api/request.ts

-14
This file was deleted.

src/app/app.controller.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Get, Controller, Header, HttpCode } from "@nestjs/common";
1+
import { Get, Controller, Header, HttpCode } from '@nestjs/common';
22

33
@Controller()
44
export class AppController {
55
constructor() {}
66

77
@Get()
88
@HttpCode(301)
9-
@Header("Location", "https://www.webpurple.net/")
9+
@Header('Location', 'https://www.webpurple.net/')
1010
root(): string {
11-
return "Hello world!";
11+
return 'Hello world!';
1212
}
1313
}

src/app/app.module.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Module } from "@nestjs/common";
2-
import { AppController } from "../app/app.controller";
3-
import { AlbumsModule } from "../modules/albums/albums.module";
1+
import { Module } from '@nestjs/common';
2+
import { AppController } from './app.controller';
3+
import { AlbumsModule } from '../modules/albums/albums.module';
44

55
@Module({
66
imports: [AlbumsModule],
77
controllers: [AppController],
8-
providers: []
8+
providers: [],
99
})
1010
export class AppModule {}

src/config.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
type Config = {
2-
port: string | number;
3-
apiVersion: string;
4-
apiUrl: string;
5-
serviceToken: string;
6-
};
7-
8-
export const config: Config = {
1+
export const config = {
92
port: process.env.PORT || 8080,
10-
apiVersion: "5.80",
11-
apiUrl: "https://api.vk.com/method",
12-
serviceToken: process.env.SERVICE_TOKEN
3+
apiVersion: '5.80',
4+
apiUrl: 'https://api.vk.com/method',
5+
serviceToken: process.env.SERVICE_TOKEN,
136
};

src/main.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { NestFactory } from "@nestjs/core";
2-
import { AppModule } from "./app/app.module";
3-
import { config } from "./config";
1+
import { NestFactory } from '@nestjs/core';
2+
import { AppModule } from 'app/app.module';
3+
import { config } from 'config';
44

55
async function bootstrap() {
66
const app = await NestFactory.create(AppModule);
+10-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { Get, Controller, Param, Header } from "@nestjs/common";
2-
import { AlbumsService, GetPhotosType } from "./albums.service";
1+
import { Get, Controller, Param, Header } from '@nestjs/common';
2+
import { IGetPhotosParams } from './interfaces/GetPhoto';
3+
import { GetPhotoDto } from '../common/dto/vk/get-photo.dto';
4+
import { VkService } from '../common/services/vk.service';
35

4-
@Controller("albums")
6+
@Controller('albums')
57
export class AlbumsController {
6-
constructor(private readonly service: AlbumsService) {}
8+
constructor(private readonly service: VkService) {}
79

8-
@Get(":owner_id/:album_id")
9-
@Header("Content-Type", "application/json")
10-
@Header("Access-Control-Allow-Origin", "*")
11-
getPhotos(@Param() params: GetPhotosType): Promise<JSON> {
10+
@Get(':owner_id/:album_id')
11+
@Header('Content-Type', 'application/json')
12+
@Header('Access-Control-Allow-Origin', 'http://webpurple.com')
13+
getPhotos(@Param() params: IGetPhotosParams): Promise<GetPhotoDto> {
1214
return this.service.getPhotos(params);
1315
}
1416
}

src/modules/albums/albums.module.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { Module } from "@nestjs/common";
2-
import { AlbumsController } from "./albums.controller";
3-
import { AlbumsService } from "./albums.service";
1+
import { Module } from '@nestjs/common';
2+
import { AlbumsController } from './albums.controller';
3+
import { CommonModules } from '../common/common.module';
44

55
@Module({
6-
imports: [],
6+
imports: [CommonModules],
77
controllers: [AlbumsController],
8-
providers: [AlbumsService]
98
})
109
export class AlbumsModule {}

src/modules/albums/albums.service.ts

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IGetPhotosParams {
2+
owner_id: string;
3+
album_id: string;
4+
}

src/modules/common/common.module.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Module } from '@nestjs/common';
2+
import { VkService } from './services/vk.service';
3+
4+
@Module({
5+
providers: [VkService],
6+
exports: [VkService],
7+
})
8+
export class CommonModules {}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
interface PhotoSizes {
2+
type: string;
3+
url: string;
4+
width: number;
5+
height: number;
6+
}
7+
8+
export interface IPhoto {
9+
id: number;
10+
sizes: Array<PhotoSizes>;
11+
text: string;
12+
date: number;
13+
}
14+
15+
export class GetPhotoDto {
16+
public count: number;
17+
public items: Array<IPhoto>;
18+
constructor(response) {
19+
this.count = response.count || null;
20+
this.items = response.items || [];
21+
}
22+
23+
static of(response) {
24+
return new GetPhotoDto(response);
25+
}
26+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { config } from '../../../config';
3+
import { GetPhotoDto } from '../dto/vk/get-photo.dto';
4+
import axios from 'axios';
5+
6+
@Injectable()
7+
export class VkService {
8+
private SERVICE_TOKEN = config.serviceToken;
9+
private API_URL = config.apiUrl;
10+
private API_VERSION = config.apiVersion;
11+
12+
public getPhotos(params: { owner_id: string; album_id: string }): Promise<GetPhotoDto> {
13+
return axios
14+
.get(`${this.API_URL}/photos/get`, {
15+
params: {
16+
...params,
17+
access_token: this.SERVICE_TOKEN,
18+
v: this.API_VERSION,
19+
},
20+
})
21+
.then(response => response.data.response)
22+
.catch(e => {
23+
console.error(e);
24+
return null;
25+
})
26+
.then(GetPhotoDto.of);
27+
}
28+
}

0 commit comments

Comments
 (0)