diff --git a/src/lib/dataaccess.ts b/src/lib/dataaccess.ts index ec753e4..1fbf1f0 100644 --- a/src/lib/dataaccess.ts +++ b/src/lib/dataaccess.ts @@ -40,6 +40,8 @@ namespace dataaccess { models.Group, models.GroupAdmin, models.GroupMember, + models.EventParticipant, + models.Event, ]); } catch (err) { globals.logger.error(err.message); diff --git a/src/lib/models/Event.ts b/src/lib/models/Event.ts new file mode 100644 index 0000000..49aede3 --- /dev/null +++ b/src/lib/models/Event.ts @@ -0,0 +1,34 @@ +import {BelongsTo, BelongsToMany, Column, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; +import {EventParticipant} from "./EventParticipant"; +import {Group} from "./Group"; +import {User} from "./User"; + +@Table({underscored: true}) +export class Event extends Model { + @NotNull + @Column({allowNull: false}) + public name: string; + + @NotNull + @Column({allowNull: false}) + public dueDate: Date; + + @NotNull + @ForeignKey(() => Group) + @Column({allowNull: false}) + public groupId: number; + + @BelongsTo(() => Group, "groupId") + public rGroup: Group; + + @BelongsToMany(() => User, () => EventParticipant) + public rParticipants: User[]; + + public async group(): Promise { + return await this.$get("rGroup") as Group; + } + + public async participants({first, offset}: {first: number, offset: number}): Promise { + return await this.$get("rParticipants") as User[]; + } +} diff --git a/src/lib/models/EventParticipant.ts b/src/lib/models/EventParticipant.ts new file mode 100644 index 0000000..c7c7199 --- /dev/null +++ b/src/lib/models/EventParticipant.ts @@ -0,0 +1,16 @@ +import {BelongsTo, BelongsToMany, Column, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; +import {Event} from "./Event"; +import {User} from "./User"; + +@Table({underscored: true}) +export class EventParticipant extends Model { + @NotNull + @ForeignKey(() => User) + @Column({allowNull: false}) + public userId: number; + + @NotNull + @ForeignKey(() => Event) + @Column({allowNull: false}) + public eventId: number; +} diff --git a/src/lib/models/Group.ts b/src/lib/models/Group.ts index 004ee03..c07544f 100644 --- a/src/lib/models/Group.ts +++ b/src/lib/models/Group.ts @@ -1,5 +1,6 @@ -import {BelongsTo, BelongsToMany, Column, ForeignKey, Model, NotNull, Table,} from "sequelize-typescript"; +import {BelongsTo, BelongsToMany, Column, ForeignKey, HasMany, Model, NotNull, Table} from "sequelize-typescript"; import {ChatRoom} from "./ChatRoom"; +import {Event} from "./Event"; import {GroupAdmin} from "./GroupAdmin"; import {GroupMember} from "./GroupMember"; import {User} from "./User"; @@ -32,6 +33,9 @@ export class Group extends Model { @BelongsTo(() => ChatRoom) public rChat: ChatRoom; + @HasMany(() => Event, "groupId") + public rEvents: Event[]; + public async creator(): Promise { return await this.$get("rCreator") as User; } @@ -49,4 +53,8 @@ export class Group extends Model { public async chat(): Promise { return await this.$get("rChat") as ChatRoom; } + + public async events(): Promise { + return await this.$get("rEvents") as Event[]; + } } diff --git a/src/lib/models/User.ts b/src/lib/models/User.ts index 278bf76..8b5538e 100644 --- a/src/lib/models/User.ts +++ b/src/lib/models/User.ts @@ -14,6 +14,8 @@ import {RequestNotFoundError} from "../errors/RequestNotFoundError"; import {ChatMember} from "./ChatMember"; import {ChatMessage} from "./ChatMessage"; import {ChatRoom} from "./ChatRoom"; +import {Event} from "./Event"; +import {EventParticipant} from "./EventParticipant"; import {Friendship} from "./Friendship"; import {Group} from "./Group"; import {GroupAdmin} from "./GroupAdmin"; @@ -58,6 +60,9 @@ export class User extends Model { @BelongsToMany(() => Group, () => GroupAdmin) public rAdministratedGroups: Group[]; + @BelongsToMany(() => Event, () => EventParticipant) + public rEvents: Event[]; + @BelongsToMany(() => Group, () => GroupMember) public rGroups: Group[]; @@ -134,6 +139,10 @@ export class User extends Model { return await this.$get("rGroups") as Group[]; } + public async events(): Promise { + return await this.$get("rEvents") as Event[]; + } + public async denyRequest(sender: number, type: RequestType) { const request = await this.$get("rReceivedRequests", {where: {senderId: sender, requestType: type}}) as Request[]; diff --git a/src/lib/models/index.ts b/src/lib/models/index.ts index 0388fbc..bc2a53e 100644 --- a/src/lib/models/index.ts +++ b/src/lib/models/index.ts @@ -9,3 +9,5 @@ export {User} from "./User"; export {Group} from "./Group"; export {GroupAdmin} from "./GroupAdmin"; export {GroupMember} from "./GroupMember"; +export {Event} from "./Event"; +export {EventParticipant} from "./EventParticipant";