Skip to content

Commit 3df450e

Browse files
authored
add calendar CRUD (#105)
1 parent e6c0f4c commit 3df450e

6 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
Body,
3+
Controller,
4+
Delete,
5+
Get,
6+
Param,
7+
Post,
8+
Put,
9+
UseGuards,
10+
} from '@nestjs/common';
11+
import { ApiBody, ApiTags } from '@nestjs/swagger';
12+
13+
import { CalendarService } from './calendar.service';
14+
import { CalendarDto } from './calendar.dto';
15+
import { Roles } from 'src/auth/authroization/roles.decorator';
16+
import { RolesGuard } from 'src/auth/authroization/roles.guard';
17+
import { UserType } from 'src/popo/user/user.meta';
18+
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
19+
import * as moment from 'moment';
20+
21+
@ApiTags('Calendar')
22+
@Controller('calendar')
23+
export class CalendarController {
24+
constructor(private readonly calendarService: CalendarService) {}
25+
26+
@Post()
27+
@Roles(UserType.admin, UserType.association)
28+
@UseGuards(JwtAuthGuard, RolesGuard)
29+
@ApiBody({ type: CalendarDto })
30+
createCalendar(@Body() dto: CalendarDto) {
31+
return this.calendarService.save(dto);
32+
}
33+
34+
@Get()
35+
getAllCalendars() {
36+
return this.calendarService.findAll();
37+
}
38+
39+
@Get('get-next-event')
40+
async getNextEvent() {
41+
const today = moment().format('YYYY-MM-DD');
42+
const events = await this.calendarService.findEventAfter(today);
43+
return events[0];
44+
}
45+
46+
@Get(':id')
47+
getCalendarByUuid(@Param('id') id: number) {
48+
return this.calendarService.findById(id);
49+
}
50+
51+
@Put(':id')
52+
@Roles(UserType.admin, UserType.association)
53+
@UseGuards(JwtAuthGuard, RolesGuard)
54+
@ApiBody({ type: CalendarDto })
55+
updateCalendar(@Param('id') id: number, @Body() dto: CalendarDto) {
56+
return this.calendarService.update(id, dto);
57+
}
58+
59+
@Delete(':id')
60+
@Roles(UserType.admin, UserType.association)
61+
@UseGuards(JwtAuthGuard, RolesGuard)
62+
deleteCalendar(@Param('id') id: number) {
63+
return this.calendarService.delete(id);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class CalendarDto {
2+
readonly title: string;
3+
readonly event_date: string;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
BaseEntity,
3+
Column,
4+
CreateDateColumn,
5+
Entity,
6+
PrimaryGeneratedColumn,
7+
UpdateDateColumn,
8+
} from 'typeorm';
9+
10+
@Entity()
11+
export class Calendar extends BaseEntity {
12+
@PrimaryGeneratedColumn('increment')
13+
id: number;
14+
15+
@Column({ nullable: false })
16+
title: string;
17+
18+
@Column({ nullable: false })
19+
event_date: string; // YYYY-MM-DD
20+
21+
@CreateDateColumn()
22+
createdAt: Date;
23+
24+
@UpdateDateColumn()
25+
updateAt: Date;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { Calendar } from './calendar.entity';
4+
import { CalendarService } from './calendar.service';
5+
import { CalendarController } from './calendar.controller';
6+
7+
@Module({
8+
imports: [TypeOrmModule.forFeature([Calendar])],
9+
providers: [CalendarService],
10+
controllers: [CalendarController],
11+
})
12+
export class CalendarModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { InjectRepository } from '@nestjs/typeorm';
3+
import { MoreThanOrEqual, Repository } from 'typeorm';
4+
5+
import { Calendar } from './calendar.entity';
6+
import { CalendarDto } from './calendar.dto';
7+
8+
@Injectable()
9+
export class CalendarService {
10+
constructor(
11+
@InjectRepository(Calendar)
12+
private readonly calendarRepo: Repository<Calendar>,
13+
) {}
14+
15+
save(dto: CalendarDto) {
16+
return this.calendarRepo.save(dto);
17+
}
18+
19+
findAll() {
20+
return this.calendarRepo.find({
21+
order: { event_date: 'DESC' },
22+
});
23+
}
24+
25+
findById(id: number) {
26+
return this.calendarRepo.findOneBy({ id: id });
27+
}
28+
29+
findEventAfter(after_date: string) {
30+
return this.calendarRepo.find({
31+
where: {
32+
event_date: MoreThanOrEqual(after_date),
33+
},
34+
order: {
35+
createdAt: 'ASC',
36+
},
37+
});
38+
}
39+
40+
update(id: number, dto: CalendarDto) {
41+
return this.calendarRepo.update({ id: id }, dto);
42+
}
43+
44+
delete(id: number) {
45+
return this.calendarRepo.delete({ id: id });
46+
}
47+
}

src/popo/popo.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Module } from '@nestjs/common';
22

33
import { BenefitModule } from './benefit/benefit.module';
4+
import { CalendarModule } from './academic-calendar/calendar.module';
45
import { EquipModule } from './equip/equip.module';
56
import { IntroduceModule } from './introduce/introduce.module';
67
import { NoticeModule } from './notice/notice.module';
@@ -13,6 +14,7 @@ import { WhitebookModule } from './whitebook/whitebook.module';
1314
@Module({
1415
imports: [
1516
BenefitModule,
17+
CalendarModule,
1618
NoticeModule,
1719
PlaceModule,
1820
EquipModule,

0 commit comments

Comments
 (0)