-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
218 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { Body, Controller, Delete, Param, Patch, Post } from '@nestjs/common'; | ||
import { AvailabilityService } from './availability.service'; | ||
import { | ||
Action, | ||
insertAvailabilitySchema, | ||
updateAvailabilitySchema, | ||
} from '@hkrecruitment/shared'; | ||
import { JoiValidate } from '../joi-validation/joi-validate.decorator'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiBearerAuth, | ||
ApiForbiddenResponse, | ||
ApiNotFoundResponse, | ||
ApiCreatedResponse, | ||
ApiOkResponse, | ||
ApiTags, | ||
ApiConflictResponse, | ||
ApiNoContentResponse, | ||
ApiBadGatewayResponse, | ||
} from '@nestjs/swagger'; | ||
import { CheckPolicies } from 'src/authorization/check-policies.decorator'; | ||
import { Availability } from './availability.entity'; | ||
import Joi from 'joi'; | ||
|
||
@ApiBearerAuth() | ||
@ApiTags('timeslots') | ||
@Controller('timeslots') | ||
export class AvailabilityController { | ||
constructor(private readonly availabilityService: AvailabilityService) {} | ||
|
||
@ApiBadRequestResponse() | ||
@ApiForbiddenResponse() | ||
@ApiNotFoundResponse() | ||
@ApiOkResponse() | ||
@ApiNoContentResponse() | ||
@ApiBadGatewayResponse() | ||
async listAvailabilities(): Promise<Availability[]> { | ||
const matches = await this.availabilityService.listAvailabilities(); | ||
return matches; | ||
} | ||
|
||
@ApiBadRequestResponse() | ||
@ApiForbiddenResponse() | ||
@ApiNotFoundResponse() | ||
@ApiOkResponse() | ||
@ApiNoContentResponse() | ||
@ApiBadGatewayResponse() | ||
@JoiValidate({ | ||
param: Joi.number().positive().integer().required(), | ||
}) | ||
async findAvailabilityById(id: number): Promise<Availability> { | ||
const matches = await this.availabilityService.findAvailabilityById(id); | ||
return matches; | ||
} | ||
|
||
@ApiBadRequestResponse() | ||
@ApiForbiddenResponse() | ||
@ApiNotFoundResponse() | ||
@ApiOkResponse() | ||
@ApiNoContentResponse() | ||
@ApiBadGatewayResponse() | ||
@ApiConflictResponse() | ||
@ApiCreatedResponse() | ||
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability')) | ||
@Post() | ||
@JoiValidate({ | ||
body: insertAvailabilitySchema, | ||
}) | ||
async createAvailability(@Body() body: Availability): Promise<boolean> { | ||
const res = await this.availabilityService.createAvailability(body); | ||
if (res.identifiers.length > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@ApiBadRequestResponse() | ||
@ApiForbiddenResponse() | ||
@ApiNotFoundResponse() | ||
@ApiOkResponse() | ||
@ApiNoContentResponse() | ||
@ApiBadGatewayResponse() | ||
@ApiConflictResponse() | ||
@ApiCreatedResponse() | ||
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability')) | ||
@Patch() | ||
@JoiValidate({ | ||
body: updateAvailabilitySchema, | ||
}) | ||
async updateAvailability(@Body() body: Availability): Promise<boolean> { | ||
const res = await this.availabilityService.updateAvailability(body); | ||
if (res.affected != undefined && res.affected != null && res.affected > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@ApiBadRequestResponse() | ||
@ApiForbiddenResponse() | ||
@ApiNotFoundResponse() | ||
@ApiOkResponse() | ||
@ApiNoContentResponse() | ||
@ApiBadGatewayResponse() | ||
@ApiConflictResponse() | ||
@ApiCreatedResponse() | ||
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability')) | ||
@Delete() | ||
@JoiValidate({ | ||
param: Joi.number().positive().integer().required(), | ||
}) | ||
async deleteAvailability(@Param() id: number): Promise<boolean> { | ||
const res = await this.availabilityService.deleteAvailability(id); | ||
if (res.affected != undefined && res.affected != null && res.affected > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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,28 @@ | ||
import { | ||
Column, | ||
Entity, | ||
JoinColumn, | ||
OneToOne, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { | ||
Availability as AvailabilityInterface, | ||
AvailabilityState, | ||
} from '@hkrecruitment/shared/availability'; | ||
import { User } from 'src/users/user.entity'; | ||
import { TimeSlot } from 'src/timeslots/timeslot.entity'; | ||
|
||
@Entity() | ||
export class Availability implements AvailabilityInterface { | ||
@PrimaryGeneratedColumn('increment') | ||
id: number; | ||
@Column() | ||
state: AvailabilityState; | ||
@Column() | ||
lastModified: Date; | ||
@OneToOne(() => TimeSlot) | ||
timeSlot: TimeSlot; | ||
@OneToOne(() => User) | ||
@JoinColumn() | ||
user: User; | ||
} |
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,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AvailabilityService } from './availability.service'; | ||
import { AvailabilityController } from './availability.controller'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Availability } from './availability.entity'; | ||
import { UsersModule } from 'src/users/users.module'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Availability]), UsersModule], | ||
providers: [AvailabilityService], | ||
controllers: [AvailabilityController], | ||
exports: [AvailabilityService], | ||
}) | ||
export class AvailabilityModule {} |
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,38 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm'; | ||
import { Availability } from './availability.entity'; | ||
|
||
@Injectable() | ||
export class AvailabilityService { | ||
constructor( | ||
@InjectRepository(Availability) | ||
private readonly availabilityRepository: Repository<Availability>, | ||
) {} | ||
|
||
async listAvailabilities(): Promise<Availability[]> { | ||
return await this.availabilityRepository.find(); | ||
} | ||
|
||
async findAvailabilityById(id: number): Promise<Availability> { | ||
const matches = await this.availabilityRepository.findBy({ | ||
id: id, | ||
}); | ||
return matches.length > 0 ? matches[0] : null; | ||
} | ||
|
||
async createAvailability(availability: Availability): Promise<InsertResult> { | ||
return await this.availabilityRepository.insert(availability); | ||
} | ||
|
||
async updateAvailability(availability: Availability): Promise<UpdateResult> { | ||
return await this.availabilityRepository.update( | ||
availability.id, | ||
availability, | ||
); | ||
} | ||
|
||
async deleteAvailability(id: number): Promise<DeleteResult> { | ||
return await this.availabilityRepository.delete(id); | ||
} | ||
} |
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