-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
287 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UntisModule } from './untis/untis.module'; | ||
import { ClassesModule } from './classes/classes.module'; | ||
import { CacheModule } from '@nestjs/cache-manager'; | ||
|
||
@Module({ | ||
imports: [], | ||
imports: [ | ||
CacheModule.register({ isGlobal: true }), | ||
UntisModule, | ||
ClassesModule, | ||
], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ClassesController } from './classes.controller'; | ||
|
||
describe('ClassesController', () => { | ||
let controller: ClassesController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ClassesController], | ||
}).compile(); | ||
|
||
controller = module.get<ClassesController>(ClassesController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CacheInterceptor } from '@nestjs/cache-manager'; | ||
import { | ||
CacheTTL, | ||
Controller, | ||
Get, | ||
HttpException, | ||
HttpStatus, | ||
Param, | ||
ParseIntPipe, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { ApiNotFoundResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import { UntisService } from 'src/untis/untis.service'; | ||
import { GetClassDto } from './dto/get-class.dto'; | ||
|
||
@ApiTags('classes') | ||
@UseInterceptors(CacheInterceptor) | ||
@CacheTTL(30) | ||
@Controller('classes') | ||
export class ClassesController { | ||
constructor(private readonly untisService: UntisService) {} | ||
|
||
@ApiOkResponse({ | ||
description: | ||
'The classes of the (currently latest) schoolyear are returned.', | ||
type: [GetClassDto], | ||
}) | ||
@Get() | ||
async getClasses() { | ||
return await this.untisService.fetchClasses(); | ||
} | ||
|
||
@ApiOkResponse({ | ||
description: 'The requested class is returned.', | ||
type: GetClassDto, | ||
}) | ||
@ApiNotFoundResponse({ description: 'The requested class was not found.' }) | ||
@Get(':id') | ||
async getClass(@Param('id', ParseIntPipe) id: number) { | ||
const targetClass = (await this.untisService.fetchClasses()).find( | ||
(c) => c.id === id, | ||
); | ||
|
||
if (!targetClass) | ||
throw new HttpException('Class not found.', HttpStatus.NOT_FOUND); | ||
|
||
return targetClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UntisModule } from 'src/untis/untis.module'; | ||
import { ClassesController } from './classes.controller'; | ||
|
||
@Module({ | ||
controllers: [ClassesController], | ||
imports: [UntisModule], | ||
}) | ||
export class ClassesModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; | ||
|
||
export class GetClassDto { | ||
@ApiProperty() | ||
id: number; | ||
|
||
@ApiProperty() | ||
name: string; | ||
|
||
@ApiProperty() | ||
longName: string; | ||
|
||
@ApiProperty() | ||
did: number; | ||
|
||
@ApiPropertyOptional() | ||
foreColor: string; | ||
|
||
@ApiPropertyOptional() | ||
backColor: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UntisService } from './untis.service'; | ||
|
||
@Module({ | ||
providers: [UntisService], | ||
exports: [UntisService], | ||
}) | ||
export class UntisModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UntisService } from './untis.service'; | ||
|
||
describe('UntisService', () => { | ||
let service: UntisService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UntisService], | ||
}).compile(); | ||
|
||
service = module.get<UntisService>(UntisService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { WebUntis } from 'webuntis'; | ||
|
||
@Injectable() | ||
export class UntisService { | ||
private readonly logger = new Logger(UntisService.name); | ||
|
||
client: WebUntis; | ||
|
||
constructor() { | ||
this.client = new WebUntis(); | ||
// TODO: Environment loading | ||
} | ||
|
||
async validateSession() { | ||
if (!(await this.client.validateSession())) { | ||
this.logger.debug('Session is invalid. Renewing now...'); | ||
await this.client.login(); | ||
} | ||
} | ||
|
||
async fetchLatestSchoolyear() { | ||
await this.validateSession(); | ||
|
||
return this.client.getLatestSchoolyear(); | ||
} | ||
|
||
async fetchClasses(schoolyearId?: number) { | ||
await this.validateSession(); | ||
|
||
if (!schoolyearId) schoolyearId = (await this.fetchLatestSchoolyear()).id; | ||
this.logger.log(`Fetching classes for schoolyear ${schoolyearId}...`); | ||
return this.client.getClasses(false, schoolyearId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.