Browse Source

first commit

master
Wanda Gusdya 3 years ago
commit
ff1deff83c
  1. 1
      .env
  2. 25
      .eslintrc.js
  3. 35
      .gitignore
  4. 4
      .prettierrc
  5. 73
      README.md
  6. BIN
      client/images/Breeze.png
  7. BIN
      client/images/Breezy_and_Dry.png
  8. BIN
      client/images/Breezy_and_Mostly_Cloudy.png
  9. BIN
      client/images/Breezy_and_Overcast.png
  10. BIN
      client/images/Breezy_and_Partly_Cloudy.png
  11. BIN
      client/images/Clear.png
  12. BIN
      client/images/Dangerously_Windy_and_Partly_Cloudy.png
  13. BIN
      client/images/Drizzle.png
  14. BIN
      client/images/Dry.png
  15. BIN
      client/images/Dry_and_Mostly_Cloudy.png
  16. BIN
      client/images/Dry_and_Partly_Cloudy.png
  17. BIN
      client/images/Foggy.png
  18. BIN
      client/images/Humid_and_Mostly_Cloudy.png
  19. BIN
      client/images/Humid_and_Partly_Cloudy.png
  20. BIN
      client/images/Light_Rain.png
  21. BIN
      client/images/Mostly_Cloudy.png
  22. BIN
      client/images/Overcast.png
  23. BIN
      client/images/Partly_Cloudy.png
  24. BIN
      client/images/Rain.png
  25. BIN
      client/images/Windy.png
  26. BIN
      client/images/Windy_and_Dry.png
  27. BIN
      client/images/Windy_and_Mostly_Cloudy.png
  28. BIN
      client/images/Windy_and_Overcast.png
  29. BIN
      client/images/Windy_and_Partly_Cloudy.png
  30. 8
      nest-cli.json
  31. 10518
      package-lock.json
  32. 78
      package.json
  33. 22
      src/app.controller.spec.ts
  34. 12
      src/app.controller.ts
  35. 31
      src/app.module.ts
  36. 8
      src/app.service.ts
  37. 16
      src/main.ts
  38. 1
      src/notification/dto/create-notification.dto.ts
  39. 6
      src/notification/dto/update-notification.dto.ts
  40. 1
      src/notification/entities/notification.entity.ts
  41. 19
      src/notification/notification.gateway.spec.ts
  42. 41
      src/notification/notification.gateway.ts
  43. 8
      src/notification/notification.module.ts
  44. 18
      src/notification/notification.service.spec.ts
  45. 26
      src/notification/notification.service.ts
  46. 1
      src/weather/dto/create-weather.dto.ts
  47. 4
      src/weather/dto/update-weather.dto.ts
  48. 1
      src/weather/entities/weather.entity.ts
  49. 48
      src/weather/schemas/weather.schema.ts
  50. 12
      src/weather/weather.collection-properties.ts
  51. 20
      src/weather/weather.controller.spec.ts
  52. 63
      src/weather/weather.controller.ts
  53. 14
      src/weather/weather.module.ts
  54. 18
      src/weather/weather.service.spec.ts
  55. 51
      src/weather/weather.service.ts
  56. 4
      tsconfig.build.json
  57. 21
      tsconfig.json

1
.env

@ -0,0 +1 @@
DATABASE_URL=mongodb://192.168.0.108/weather

25
.eslintrc.js

@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};

35
.gitignore

@ -0,0 +1,35 @@
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

4
.prettierrc

@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}

73
README.md

@ -0,0 +1,73 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
</p>
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
## Description
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
## Installation
```bash
$ npm install
```
## Running the app
```bash
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
```
## Test
```bash
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
```
## Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
## Stay in touch
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
## License
Nest is [MIT licensed](LICENSE).

BIN
client/images/Breeze.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
client/images/Breezy_and_Dry.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
client/images/Breezy_and_Mostly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
client/images/Breezy_and_Overcast.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
client/images/Breezy_and_Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

BIN
client/images/Clear.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
client/images/Dangerously_Windy_and_Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
client/images/Drizzle.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

BIN
client/images/Dry.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
client/images/Dry_and_Mostly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
client/images/Dry_and_Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
client/images/Foggy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

BIN
client/images/Humid_and_Mostly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

BIN
client/images/Humid_and_Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
client/images/Light_Rain.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
client/images/Mostly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

BIN
client/images/Overcast.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
client/images/Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
client/images/Rain.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
client/images/Windy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

BIN
client/images/Windy_and_Dry.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
client/images/Windy_and_Mostly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
client/images/Windy_and_Overcast.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
client/images/Windy_and_Partly_Cloudy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

8
nest-cli.json

@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}

10518
package-lock.json

File diff suppressed because it is too large

78
package.json

@ -0,0 +1,78 @@
{
"name": "weather-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@fastify/static": "^6.6.0",
"@forlagshuset/nestjs-mongoose-paginate": "^1.2.6",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-fastify": "^9.2.1",
"@nestjs/serve-static": "^3.0.0",
"@nestjs/websockets": "^9.2.1",
"mongoose": "^6.8.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.2.4",
"@types/node": "18.11.18",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.3.1",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.1",
"typescript": "^4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

22
src/app.controller.spec.ts

@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

12
src/app.controller.ts

@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

31
src/app.module.ts

@ -0,0 +1,31 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule } from '@nestjs/config';
import { WeatherModule } from './weather/weather.module';
import { RouterModule } from '@nestjs/core';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { NotificationModule } from './notification/notification.module';
@Module({
imports: [
ConfigModule.forRoot(),
MongooseModule.forRoot(process.env.DATABASE_URL),
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'client'),
}),
WeatherModule,
RouterModule.register([
{
path: '/api/v1',
module: WeatherModule,
},
]),
NotificationModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

8
src/app.service.ts

@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

16
src/main.ts

@ -0,0 +1,16 @@
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ logger: true }),
{ cors: true },
);
await app.listen(3001);
}
bootstrap();

1
src/notification/dto/create-notification.dto.ts

@ -0,0 +1 @@
export class CreateNotificationDto {}

6
src/notification/dto/update-notification.dto.ts

@ -0,0 +1,6 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateNotificationDto } from './create-notification.dto';
export class UpdateNotificationDto extends PartialType(CreateNotificationDto) {
id: number;
}

1
src/notification/entities/notification.entity.ts

@ -0,0 +1 @@
export class Notification {}

19
src/notification/notification.gateway.spec.ts

@ -0,0 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing';
import { NotificationGateway } from './notification.gateway';
import { NotificationService } from './notification.service';
describe('NotificationGateway', () => {
let gateway: NotificationGateway;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NotificationGateway, NotificationService],
}).compile();
gateway = module.get<NotificationGateway>(NotificationGateway);
});
it('should be defined', () => {
expect(gateway).toBeDefined();
});
});

41
src/notification/notification.gateway.ts

@ -0,0 +1,41 @@
import {
WebSocketGateway,
SubscribeMessage,
MessageBody,
} from '@nestjs/websockets';
import { NotificationService } from './notification.service';
import { CreateNotificationDto } from './dto/create-notification.dto';
import { UpdateNotificationDto } from './dto/update-notification.dto';
@WebSocketGateway()
export class NotificationGateway {
constructor(private readonly notificationService: NotificationService) {}
@SubscribeMessage('createNotification')
create(@MessageBody() createNotificationDto: CreateNotificationDto) {
return this.notificationService.create(createNotificationDto);
}
@SubscribeMessage('findAllNotification')
findAll() {
return this.notificationService.findAll();
}
@SubscribeMessage('findOneNotification')
findOne(@MessageBody() id: number) {
return this.notificationService.findOne(id);
}
@SubscribeMessage('updateNotification')
update(@MessageBody() updateNotificationDto: UpdateNotificationDto) {
return this.notificationService.update(
updateNotificationDto.id,
updateNotificationDto,
);
}
@SubscribeMessage('removeNotification')
remove(@MessageBody() id: number) {
return this.notificationService.remove(id);
}
}

8
src/notification/notification.module.ts

@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { NotificationService } from './notification.service';
import { NotificationGateway } from './notification.gateway';
@Module({
providers: [NotificationGateway, NotificationService]
})
export class NotificationModule {}

18
src/notification/notification.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { NotificationService } from './notification.service';
describe('NotificationService', () => {
let service: NotificationService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NotificationService],
}).compile();
service = module.get<NotificationService>(NotificationService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

26
src/notification/notification.service.ts

@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreateNotificationDto } from './dto/create-notification.dto';
import { UpdateNotificationDto } from './dto/update-notification.dto';
@Injectable()
export class NotificationService {
create(createNotificationDto: CreateNotificationDto) {
return 'This action adds a new notification';
}
findAll() {
return `This action returns all notification`;
}
findOne(id: number) {
return `This action returns a #${id} notification`;
}
update(id: number, updateNotificationDto: UpdateNotificationDto) {
return `This action updates a #${id} notification`;
}
remove(id: number) {
return `This action removes a #${id} notification`;
}
}

1
src/weather/dto/create-weather.dto.ts

@ -0,0 +1 @@
export class CreateWeatherDto {}

4
src/weather/dto/update-weather.dto.ts

@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateWeatherDto } from './create-weather.dto';
export class UpdateWeatherDto extends PartialType(CreateWeatherDto) {}

1
src/weather/entities/weather.entity.ts

@ -0,0 +1 @@
export class Weather {}

48
src/weather/schemas/weather.schema.ts

@ -0,0 +1,48 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
export type WeatherDocument = HydratedDocument<Weather>;
@Schema()
export class Weather {
@Prop()
deviceId: string;
@Prop()
date: Date;
@Prop()
summary: string;
@Prop()
precipType: string;
@Prop()
temperature: number;
@Prop()
apparentTemperature: number;
@Prop()
humidity: number;
@Prop()
windSpeed: number;
@Prop()
windBearing: number;
@Prop()
visibility: number;
@Prop()
loudCover: number;
@Prop()
pressure: number;
@Prop()
dailySummary: string;
}
export const WeatherSchema = SchemaFactory.createForClass(Weather);

12
src/weather/weather.collection-properties.ts

@ -0,0 +1,12 @@
import {
CollectionProperties,
Expose,
} from '@forlagshuset/nestjs-mongoose-paginate';
export class WeatherCollectionProperties extends CollectionProperties {
@Expose({ name: 'date', sortable: true })
readonly date: 'asc' | 'desc';
@Expose({ name: 'deviceId', sortable: true })
readonly deviceId: 'asc' | 'desc';
}

20
src/weather/weather.controller.spec.ts

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { WeatherController } from './weather.controller';
import { WeatherService } from './weather.service';
describe('WeatherController', () => {
let controller: WeatherController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [WeatherController],
providers: [WeatherService],
}).compile();
controller = module.get<WeatherController>(WeatherController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

63
src/weather/weather.controller.ts

@ -0,0 +1,63 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
} from '@nestjs/common';
import { WeatherService } from './weather.service';
import { CreateWeatherDto } from './dto/create-weather.dto';
import { UpdateWeatherDto } from './dto/update-weather.dto';
import { WeatherDocument } from './schemas/weather.schema';
import {
CollectionDto,
CollectionResponse,
ValidationPipe,
} from '@forlagshuset/nestjs-mongoose-paginate';
import { WeatherCollectionProperties } from './weather.collection-properties';
@Controller('weather')
export class WeatherController {
constructor(private readonly weatherService: WeatherService) {}
@Post()
create(@Body() createWeatherDto: CreateWeatherDto) {
return this.weatherService.create(createWeatherDto);
}
@Get()
findAll() {
return this.weatherService.findAll();
}
@Get('list')
async filter(
@Query(new ValidationPipe(WeatherCollectionProperties))
collectionDto: CollectionDto,
): Promise<CollectionResponse<WeatherDocument>> {
return await this.weatherService.list(collectionDto);
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.weatherService.findOne(id);
}
@Get('latest/:deviceId')
latest(@Param('deviceId') deviceId: string) {
return this.weatherService.latest(deviceId);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateWeatherDto: UpdateWeatherDto) {
return this.weatherService.update(id, updateWeatherDto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.weatherService.remove(id);
}
}

14
src/weather/weather.module.ts

@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { WeatherService } from './weather.service';
import { WeatherController } from './weather.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Weather, WeatherSchema } from './schemas/weather.schema';
@Module({
imports: [
MongooseModule.forFeature([{ name: Weather.name, schema: WeatherSchema }]),
],
controllers: [WeatherController],
providers: [WeatherService],
})
export class WeatherModule {}

18
src/weather/weather.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { WeatherService } from './weather.service';
describe('WeatherService', () => {
let service: WeatherService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [WeatherService],
}).compile();
service = module.get<WeatherService>(WeatherService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

51
src/weather/weather.service.ts

@ -0,0 +1,51 @@
import { Injectable } from '@nestjs/common';
import { CreateWeatherDto } from './dto/create-weather.dto';
import { UpdateWeatherDto } from './dto/update-weather.dto';
import { Weather, WeatherDocument } from './schemas/weather.schema';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import {
CollectionDto,
CollectionResponse,
DocumentCollector,
} from '@forlagshuset/nestjs-mongoose-paginate';
@Injectable()
export class WeatherService {
constructor(
@InjectModel(Weather.name) private weatherModel: Model<WeatherDocument>,
) {}
create(createWeatherDto: CreateWeatherDto) {
const weatherModel = new this.weatherModel(createWeatherDto);
return weatherModel.save();
}
findAll() {
return this.weatherModel.find().exec();
}
async list(
collectionDto: CollectionDto,
): Promise<CollectionResponse<WeatherDocument>> {
const collector = new DocumentCollector<WeatherDocument>(this.weatherModel);
return collector.find(collectionDto);
}
findOne(id: string) {
return this.weatherModel.findById(id).exec();
}
latest(deviceId: string) {
return this.weatherModel.findOne({ deviceId: deviceId }).exec();
}
async update(id: string, updateWeatherDto: UpdateWeatherDto) {
const weatherModel = await this.weatherModel.findById(id);
weatherModel.overwrite(updateWeatherDto);
return weatherModel.save();
}
remove(id: string) {
return this.weatherModel.findByIdAndRemove(id).exec();
}
}

4
tsconfig.build.json

@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

21
tsconfig.json

@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
Loading…
Cancel
Save