Skip to content

Commit

Permalink
chore: Change repositories to app layer
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Nov 23, 2022
1 parent 633f5cd commit 673782e
Show file tree
Hide file tree
Showing 53 changed files with 188 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';

import { IChannelRepository } from '../../app';
import {
Channel,
ChannelId,
ChannelYoutube,
IChannelRepository,
OrganizationId,
PerformerId,
YoutubeChannelId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';
import { getPlaiceholder } from 'plaiceholder';

import { IStorage } from '../../app';
import { IMediaAttachmentRepository, IStorage } from '../../app';
import {
IMediaAttachmentRepository,
MediaAttachment,
MediaAttachmentFilename,
MediaAttachmentId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Prisma, PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';

import { FindOrganizationParams, IOrganizationRepository } from '../../app';
import {
FindOrganizationParams,
IOrganizationRepository,
Organization,
OrganizationId,
PerformerId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Prisma, PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';

import {
FindPerformerParams,
IPerformerRepository,
Performer,
PerformerId,
unwrap,
YoutubeChannelId,
} from '../../domain';
import { FindPerformerParams, IPerformerRepository } from '../../app';
import { Performer, PerformerId, unwrap, YoutubeChannelId } from '../../domain';
import { TYPES } from '../../types';
import { rehydratePerformerFromPrisma } from '../mappers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
IStreamRepository,
ITokenRepository,
IUserRepository,
} from '../../domain';
} from '../../app';
import { TYPES } from '../../types';
import { ChannelRepositoryPrisma } from './channel-repository-prisma';
import { MediaAttachmentRepositoryPrisma } from './media-attachment-repository-prisma';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { CloudTasksClient } from '@google-cloud/tasks';
import { google } from '@google-cloud/tasks/build/protos/protos';
import { inject, injectable } from 'inversify';

import { IConfig, ILogger, utils } from '../../app';
import {
IConfig,
ILogger,
IResubscriptionTaskRepository,
ResubscriptionTask,
} from '../../domain';
utils,
} from '../../app';
import { ResubscriptionTask } from '../../domain';
import { TYPES } from '../../types';

@injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { injectable } from 'inversify';

import {
IResubscriptionTaskRepository,
ResubscriptionTask,
} from '../../domain';
import { IResubscriptionTaskRepository } from '../../app';
import { ResubscriptionTask } from '../../domain';

@injectable()
export class ResubscriptionTaskRepositoryInMemory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import { PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';
import { URL } from 'url';

import {
IStreamRepository,
ListStreamsParams,
Stream,
StreamId,
unwrap,
} from '../../domain';
import { IStreamRepository, ListStreamsParams } from '../../app';
import { Stream, StreamId, unwrap } from '../../domain';
import { TYPES } from '../../types';
import { rehydrateStreamFromPrisma } from '../mappers';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';

import { ITokenRepository, Token, TokenId } from '../../domain';
import { ITokenRepository } from '../../app';
import { Token, TokenId } from '../../domain';
import { TYPES } from '../../types';
import { rehydrateTokenFromPrisma } from '../mappers';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PrismaClient } from '@prisma/client';
import { inject, injectable } from 'inversify';

import { IUserRepository, User, UserEmail, UserId } from '../../domain';
import { IUserRepository } from '../../app';
import { User, UserEmail, UserId } from '../../domain';
import { TYPES } from '../../types';
import { rehydrateUserFromPrisma } from '../mappers';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { YoutubeChannelId } from '../_shared';
import { OrganizationId } from '../organization';
import { PerformerId } from '../performer';
import { Channel } from './channel';
import { ChannelId } from './channel-id';
import { ChannelYoutube } from './channel-youtube';
import {
Channel,
ChannelId,
ChannelYoutube,
OrganizationId,
PerformerId,
YoutubeChannelId,
} from '../../domain';

export interface IChannelRepository {
create(channel: Channel): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions packages/@neet/vschedule-api/src/app/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* @file Automatically generated by barrelsby.
*/

export * from './channel-repository';
export * from './request-channel-subscription';
export * from './verify-youtube-channel-subscription';
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { inject, injectable } from 'inversify';

import { ChannelId, ChannelService, ChannelYoutube } from '../../domain';
import { ChannelId } from '../../domain';
import { TYPES } from '../../types';
import { IYoutubeWebsubService } from '../_external';
import { AppError } from '../_shared';
import { IChannelRepository } from './channel-repository';

export class RequestChannelSubscriptionNotFoundError extends AppError {
readonly name = 'RequestChannelSubscriptionNotFoundError';

constructor(readonly id: ChannelId) {
super(`Channel with id ${id} was not found`);
}
}

export type RequestChannelSubscriptionCommand = {
readonly channelId: string;
Expand All @@ -11,22 +21,31 @@ export type RequestChannelSubscriptionCommand = {
@injectable()
export class RequestChannelSubscription {
constructor(
@inject(ChannelService)
private readonly _channelService: ChannelService,
@inject(TYPES.ChannelRepository)
private readonly _channelRepository: IChannelRepository,

@inject(TYPES.YoutubeWebsubService)
private readonly _youtubeWebsubService: IYoutubeWebsubService,
) {}

async invoke(params: RequestChannelSubscriptionCommand): Promise<void> {
const channelId = new ChannelId(params.channelId);
const channel = await this._channelService.requestSubscription(channelId);
const channel = await this._channelRepository.findById(channelId);
if (channel == null) {
throw new RequestChannelSubscriptionNotFoundError(channelId);
}

try {
const updatedChannel = channel.requestSubscription();
await this._channelRepository.update(updatedChannel);

// FIXME リポジトリでやるのが正解な気がする
if (channel instanceof ChannelYoutube) {
// FIXME リポジトリでやるのが正解な気がする
await this._youtubeWebsubService.subscribeToChannel(
channel.youtubeChannelId.value,
updatedChannel.youtubeChannelId.value,
);
} catch (error) {
const updatedChannel = channel.resetSubscription();
await this._channelRepository.update(updatedChannel);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import dayjs from 'dayjs';
import { inject, injectable } from 'inversify';

import { YoutubeChannelId } from '../../domain';
import { IChannelRepository } from '../../domain/entities/channel';
import { ChannelService } from '../../domain/services/channel-service';
import { ChannelService, YoutubeChannelId } from '../../domain';
import { TYPES } from '../../types';
import { AppError, ILogger } from '../_shared';
import { IResubscriptionTaskRepository } from '../resubscription-task';
import { ITokenRepository } from '../token';
import { IChannelRepository } from './channel-repository';

export class VerifyYoutubeChannelSubscriptionInvalidTopicError extends AppError {
public readonly name = 'VerifyYoutubeChannelSubscriptionInvalidTopicError';
Expand Down Expand Up @@ -39,6 +40,12 @@ export class VerifyYoutubeChannelSubscription {
@inject(TYPES.ChannelRepository)
private readonly _channelRepository: IChannelRepository,

@inject(TYPES.TokenRepository)
private readonly _tokenRepository: ITokenRepository,

@inject(TYPES.ResubscriptionTaskRepository)
private readonly _resubscriptionTaskRepository: IResubscriptionTaskRepository,

@inject(TYPES.Logger)
private readonly _logger: ILogger,
) {}
Expand All @@ -64,11 +71,19 @@ export class VerifyYoutubeChannelSubscription {
);
}

await this._channelService.verifySubscription(
channel.id,
const {
channel: updatedChannel,
token,
resubscriptionTask,
} = this._channelService.verifySubscription(
channel,
dayjs().add(command.leaseSeconds, 'seconds'),
);

await this._channelRepository.update(updatedChannel);
await this._tokenRepository.create(token);
await this._resubscriptionTaskRepository.create(resubscriptionTask);

this._logger.info(
`Scheduled resubscription for ${topic} in ${command.leaseSeconds} secs`,
);
Expand Down
1 change: 1 addition & 0 deletions packages/@neet/vschedule-api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './factories';
export * from './media-attachment/index';
export * from './organization/index';
export * from './performer/index';
export * from './resubscription-task/index';
export * from './stream/index';
export * from './token/index';
export * from './user/index';
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* @file Automatically generated by barrelsby.
*/

export * from './media-attachment-repository';
export * from './show-media-attachment';
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MediaAttachment } from './media-attachment';
import { MediaAttachmentFilename } from './media-attachment-filename';
import { MediaAttachmentId } from './media-attachment-id';
import {
MediaAttachment,
MediaAttachmentFilename,
MediaAttachmentId,
} from '../../domain';

export interface IMediaAttachmentRepository {
findById(id: MediaAttachmentId): Promise<MediaAttachment | undefined>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { inject, injectable } from 'inversify';

import {
IMediaAttachmentRepository,
MediaAttachment,
MediaAttachmentFilename,
} from '../../domain';
import { MediaAttachment, MediaAttachmentFilename } from '../../domain';
import { TYPES } from '../../types';
import { AppError } from '../_shared';
import { IMediaAttachmentRepository } from './media-attachment-repository';

export class ShowMediaAttachmentNotFoundError extends AppError {
public readonly name = 'ShowMediaAttachmentNotFoundError';
Expand Down
1 change: 1 addition & 0 deletions packages/@neet/vschedule-api/src/app/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
export * from './list-organizations';
export * from './organization-factory-impl';
export * from './organization-query-service';
export * from './organization-repository';
export * from './show-organization';
export * from './upsert-organization';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fetch from 'node-fetch';
import sharp from 'sharp';

import {
IMediaAttachmentRepository,
IOrganizationFactory,
MediaAttachmentFilename,
Organization,
Expand All @@ -15,6 +14,7 @@ import {
import { TYPES } from '../../types';
import { IYoutubeApiService } from '../_external';
import { AppError, ILogger, UnexpectedError } from '../_shared';
import { IMediaAttachmentRepository } from '../media-attachment';

export class OrganizationFactoryChannelNotFoundError extends AppError {
public readonly name = 'OrganizationFactoryChannelNotFoundError';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { YoutubeChannelId } from '../_shared';
import { PerformerId } from '../performer';
import { Organization } from './organization';
import { OrganizationId } from './organization-id';
import {
Organization,
OrganizationId,
PerformerId,
YoutubeChannelId,
} from '../../domain';

export interface FindOrganizationParams {
export type FindOrganizationParams = {
readonly limit?: number;
readonly offset?: number;
}
};

export interface IOrganizationRepository {
create(organization: Organization): Promise<Organization>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { inject, injectable } from 'inversify';

import {
IOrganizationFactory,
IOrganizationRepository,
Organization,
OrganizationDescription,
OrganizationName,
Expand All @@ -12,6 +11,7 @@ import {
} from '../../domain';
import { TYPES } from '../../types';
import { ILogger } from '../_shared';
import { IOrganizationRepository } from './organization-repository';

export type UpsertOrganizationCommand = {
readonly name: string;
Expand Down
1 change: 1 addition & 0 deletions packages/@neet/vschedule-api/src/app/performer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
export * from './list-performers';
export * from './performer-factory-impl';
export * from './performer-query-service';
export * from './performer-repository';
export * from './show-performer';
export * from './upsert-performer';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fetch from 'node-fetch';
import sharp from 'sharp';

import {
IMediaAttachmentRepository,
IPerformerFactory,
MediaAttachmentFilename,
Performer,
Expand All @@ -15,6 +14,7 @@ import {
import { TYPES } from '../../types';
import { IYoutubeApiService } from '../_external';
import { AppError, ILogger, UnexpectedError } from '../_shared';
import { IMediaAttachmentRepository } from '../media-attachment';

export class PerformerFactoryChannelNotFoundError extends AppError {
public readonly name = 'PerformerFactoryChannelNotFoundError';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { YoutubeChannelId } from '../_shared';
import { Performer } from './performer';
import { PerformerId } from './performer-id';
import { Performer, PerformerId, YoutubeChannelId } from '../../domain';

export interface FindPerformerParams {
readonly limit?: number;
Expand Down
Loading

0 comments on commit 673782e

Please sign in to comment.