This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
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
1 parent
6a311a8
commit 2fd6acd
Showing
23 changed files
with
497 additions
and
6 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
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { Follow } from '../follow.entity'; | ||
|
||
export class FollowCommand implements ICommand { | ||
constructor(public readonly follow: Follow) {} | ||
} |
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,27 @@ | ||
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { FollowCommand } from '../follow.command'; | ||
import { FollowRepository } from '../../follow.repository'; | ||
|
||
@CommandHandler(FollowCommand) | ||
export class FollowCommandHandler implements ICommandHandler<FollowCommand> { | ||
constructor( | ||
@InjectRepository(FollowRepository) | ||
private readonly repository: FollowRepository, | ||
private readonly publisher: EventPublisher, | ||
) { | ||
} | ||
|
||
async execute(command: FollowCommand, resolve: (value?) => void) { | ||
const { follow } = command; | ||
const entity = this.publisher.mergeObjectContext( | ||
await this.repository.save(follow), | ||
); | ||
|
||
// Sends actual event | ||
await entity.follow(); | ||
|
||
entity.commit(); | ||
resolve(entity); | ||
} | ||
} |
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,7 @@ | ||
import { FollowCommandHandler } from "./follow.command"; | ||
import { UnFollowCommandHandler } from "./unfollow.command"; | ||
|
||
export const commandHandlers = [ | ||
FollowCommandHandler, | ||
UnFollowCommandHandler, | ||
]; |
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,27 @@ | ||
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { FollowRepository } from '../../follow.repository'; | ||
import { UnFollowCommand } from '../unfollow.command'; | ||
|
||
@CommandHandler(UnFollowCommand) | ||
export class UnFollowCommandHandler implements ICommandHandler<UnFollowCommand> { | ||
constructor( | ||
@InjectRepository(FollowRepository) | ||
private readonly repository: FollowRepository, | ||
private readonly publisher: EventPublisher, | ||
) { | ||
} | ||
|
||
async execute(command: UnFollowCommand, resolve: (value?) => void) { | ||
const { follow } = command; | ||
|
||
// Sends actual event | ||
await follow.unFollow(); | ||
|
||
follow.commit(); | ||
|
||
await this.repository.delete(follow.id); | ||
|
||
resolve(follow); | ||
} | ||
} |
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,6 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { Follow } from '../follow.entity'; | ||
|
||
export class UnFollowCommand implements ICommand { | ||
constructor(public readonly follow: Follow) {} | ||
} |
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,16 @@ | ||
import { IEvent } from '@nestjs/cqrs'; | ||
import { Follow } from '../follow.entity'; | ||
|
||
export class FollowEvent implements IEvent { | ||
constructor(public readonly follow: Follow) { | ||
} | ||
} | ||
|
||
export class FollowCharacterEvent extends FollowEvent { | ||
} | ||
|
||
export class FollowCorporationEvent extends FollowEvent { | ||
} | ||
|
||
export class FollowAllianceEvent extends FollowEvent { | ||
} |
27 changes: 27 additions & 0 deletions
27
src/modules/follow/events/handlers/follow.character.handler.ts
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,27 @@ | ||
import { EventsHandler, IEventHandler, CommandBus } from '@nestjs/cqrs'; | ||
import { FollowEvent, FollowCharacterEvent } from '../follow.event'; | ||
import { CreateNotificationCommand } from '../../../notification/commands/create.command'; | ||
import { Notification } from '../../../notification/notification.entity'; | ||
import { NOTIFICATION_TYPE } from '../../../notification/notification.constants'; | ||
import * as uuidv4 from 'uuid/v4'; | ||
|
||
@EventsHandler(FollowCharacterEvent) | ||
export class FollowCharacterEventHandler implements IEventHandler<FollowCharacterEvent> { | ||
|
||
constructor( | ||
private commandBus: CommandBus, | ||
) { | ||
} | ||
|
||
async handle(event: FollowEvent) { | ||
const eventUuid = uuidv4(); | ||
|
||
const notification = new Notification(); | ||
notification.type = NOTIFICATION_TYPE.NEW_FOLLOWER; | ||
notification.recipient = event.follow.followingCharacter; | ||
notification.senderCharacter = event.follow.follower; | ||
notification.eventUuid = eventUuid; | ||
|
||
this.commandBus.execute(new CreateNotificationCommand(notification)) | ||
} | ||
} |
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,5 @@ | ||
import { FollowCharacterEventHandler } from "./follow.character.handler"; | ||
|
||
export const eventHandlers = [ | ||
FollowCharacterEventHandler, | ||
]; |
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,7 @@ | ||
import { IEvent } from '@nestjs/cqrs'; | ||
import { Follow } from '../follow.entity'; | ||
|
||
export class UnFollowEvent implements IEvent { | ||
constructor(public readonly follow: Follow) { | ||
} | ||
} |
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,5 @@ | ||
|
||
export enum FOLLOW_TYPE { | ||
FOLLOW = 'FOLLOW', | ||
UN_FOLLOW = 'UN_FOLLOW', | ||
} |
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,97 @@ | ||
import { ApiUseTags, ApiResponse, ApiBearerAuth } from "@nestjs/swagger"; | ||
import { Controller, HttpStatus, Param, UseGuards, Post } from "@nestjs/common"; | ||
import { Character } from "../character/character.entity"; | ||
import { AuthenticatedCharacter } from "../authentication/authentication.decorators"; | ||
import { AuthenticationGuard } from "../authentication/authentication.guard"; | ||
import { DFollow } from "./follow.dto"; | ||
import { FollowService } from "./follow.service"; | ||
import { CharacterService } from "../character/character.service"; | ||
import { CorporationService } from "../corporation/corporation.service"; | ||
import { AllianceService } from "../alliance/alliance.service"; | ||
import { FOLLOW_TYPE } from "./follow.constants"; | ||
|
||
@ApiUseTags('follow') | ||
@Controller('follow') | ||
export class FollowController { | ||
|
||
constructor( | ||
private followService: FollowService, | ||
private characterService: CharacterService, | ||
private corporationService: CorporationService, | ||
private allianceService: AllianceService, | ||
){ | ||
} | ||
|
||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
type: DFollow, | ||
description: 'Follow a character', | ||
}) | ||
@ApiBearerAuth() | ||
@UseGuards(AuthenticationGuard) | ||
@Post('character/:characterId') | ||
public async followCharacter( | ||
@Param('characterId') characterId: number, | ||
@AuthenticatedCharacter() follower: Character, | ||
): Promise<DFollow> { | ||
const character = await this.characterService.get(characterId); | ||
const follow = await this.followService.checkIfFolowingCharacter(follower, character); | ||
|
||
if (follow) { | ||
await this.followService.unfollow(follow); | ||
return new DFollow(FOLLOW_TYPE.UN_FOLLOW); | ||
} | ||
|
||
const newFollow = await this.followService.followCharacter(follower, character); | ||
return new DFollow(FOLLOW_TYPE.FOLLOW); | ||
} | ||
|
||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
type: DFollow, | ||
description: 'Follow a corporation', | ||
}) | ||
@ApiBearerAuth() | ||
@UseGuards(AuthenticationGuard) | ||
@Post('corporation/:corporationId') | ||
public async followCorporation( | ||
@Param('corporationId') corporationId: number, | ||
@AuthenticatedCharacter() follower: Character, | ||
): Promise<DFollow> { | ||
const corporation = await this.corporationService.get(corporationId); | ||
const follow = await this.followService.checkIfFolowingCorporation(follower, corporation); | ||
|
||
if (follow) { | ||
await this.followService.unfollow(follow); | ||
return new DFollow(FOLLOW_TYPE.UN_FOLLOW); | ||
} | ||
|
||
const newFollow = await this.followService.followCorporation(follower, corporation); | ||
return new DFollow(FOLLOW_TYPE.FOLLOW); | ||
} | ||
|
||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
type: DFollow, | ||
description: 'Follow an Alliance', | ||
}) | ||
@ApiBearerAuth() | ||
@UseGuards(AuthenticationGuard) | ||
@Post('alliance/:allianceId') | ||
public async followAlliance( | ||
@Param('allianceId') allianceId: number, | ||
@AuthenticatedCharacter() follower: Character, | ||
): Promise<DFollow> { | ||
const alliance = await this.allianceService.get(allianceId); | ||
const follow = await this.followService.checkIfFolowingAlliance(follower, alliance); | ||
|
||
if (follow) { | ||
await this.followService.unfollow(follow); | ||
return new DFollow(FOLLOW_TYPE.UN_FOLLOW); | ||
} | ||
|
||
const newFollow = await this.followService.followAlliance(follower, alliance); | ||
return new DFollow(FOLLOW_TYPE.FOLLOW); | ||
} | ||
|
||
} |
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 { FOLLOW_TYPE } from "./follow.constants"; | ||
|
||
export class DFollow { | ||
type: FOLLOW_TYPE; | ||
|
||
constructor(type: FOLLOW_TYPE) { | ||
this.type = type; | ||
} | ||
} |
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,41 @@ | ||
import { Entity, PrimaryGeneratedColumn, ManyToOne } from "typeorm"; | ||
import { AggregateRoot } from "@nestjs/cqrs"; | ||
import { Character } from "../character/character.entity"; | ||
import { Corporation } from "../corporation/corporation.entity"; | ||
import { Alliance } from "../alliance/alliance.entity"; | ||
import { UnFollowEvent } from "./events/unfollow.event"; | ||
import { FollowEvent, FollowCharacterEvent, FollowCorporationEvent, FollowAllianceEvent } from "./events/follow.event"; | ||
|
||
@Entity() | ||
export class Follow extends AggregateRoot { | ||
|
||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@ManyToOne(type => Character, character => character.following) | ||
follower: Character; | ||
|
||
@ManyToOne(type => Character, character => character.followers) | ||
followingCharacter: Character; | ||
|
||
@ManyToOne(type => Corporation, corporation => corporation.followers) | ||
followingCorporation: Corporation; | ||
|
||
@ManyToOne(type => Alliance, alliance => alliance.followers) | ||
followingAlliance: Alliance; | ||
|
||
async unFollow(): Promise<void> { | ||
await this.apply(new UnFollowEvent(this)); | ||
} | ||
|
||
async follow(): Promise<void> { | ||
if (this.followingCharacter) { | ||
await this.apply(new FollowCharacterEvent(this)); | ||
} else if(this.followingCorporation) { | ||
await this.apply(new FollowCorporationEvent(this)); | ||
} else if(this.followingAlliance) { | ||
await this.apply(new FollowAllianceEvent(this)); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.