Skip to content

Commit

Permalink
Merge pull request #442 from BinaryStudioAcademy/task/OV-439-generate…
Browse files Browse the repository at this point in the history
…-preview-for-template

OV-439: generate preview for templates
  • Loading branch information
nikita-remeslov authored Sep 27, 2024
2 parents ce9380e + a74dc6d commit 7bb52f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
25 changes: 21 additions & 4 deletions backend/src/bundles/templates/templates.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { HTTPCode, HttpError } from '~/common/http/http.js';
import { type ImageService } from '~/common/services/image/image.service.js';
import { type Service } from '~/common/types/types.js';

import { type UpdateVideoRequestDto } from '../videos/types/types.js';
import {
type Scene,
type UpdateVideoRequestDto,
} from '../videos/types/types.js';
import { templateErrorMessage } from './enums/enums.js';
import { TemplateEntity } from './templates.entity.js';
import { type TemplateRepository } from './templates.repository.js';
Expand All @@ -14,9 +18,14 @@ import {

class TemplateService implements Service {
private templateRepository: TemplateRepository;
private imageService: ImageService;

public constructor(templateRepository: TemplateRepository) {
public constructor(
templateRepository: TemplateRepository,
imageService: ImageService,
) {
this.templateRepository = templateRepository;
this.imageService = imageService;
}

public async findById(id: string): Promise<TemplateEntity | null> {
Expand Down Expand Up @@ -54,7 +63,9 @@ class TemplateService implements Service {
const { composition, name, userId } = payload;

// TODO: CREATE PREVIEW
const compositionPreviewUrl = composition.scenes[0]?.avatar?.url ?? '';
const compositionPreviewUrl = await this.imageService.generatePreview(
composition.scenes[0] as Scene,
);

const user = await this.templateRepository.create(
TemplateEntity.initializeNew({
Expand All @@ -70,7 +81,7 @@ class TemplateService implements Service {

public async updateTemplate(
id: string,
payload: UpdateVideoRequestDto,
payload: UpdateVideoRequestDto & { previewUrl?: string },
userId: string,
): Promise<Template> {
const template = await this.findById(id);
Expand All @@ -82,6 +93,12 @@ class TemplateService implements Service {
});
}

if (payload.composition) {
payload.previewUrl = await this.imageService.generatePreview(
payload.composition.scenes[0] as Scene,
);
}

return await this.update(id, payload);
}

Expand Down
3 changes: 2 additions & 1 deletion backend/src/bundles/templates/templates.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { logger } from '~/common/logger/logger.js';
import { imageService } from '~/common/services/services.js';

import { TemplateController } from './templates.controller.js';
import { TemplateModel } from './templates.model.js';
import { TemplateRepository } from './templates.repository.js';
import { TemplateService } from './templates.service.js';

const templateRepository = new TemplateRepository(TemplateModel);
const templateService = new TemplateService(templateRepository);
const templateService = new TemplateService(templateRepository, imageService);
const templateController = new TemplateController(logger, templateService);

export { templateController };
10 changes: 5 additions & 5 deletions backend/src/seed-data/templates-seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const templatesSeed = [
name: 'Landscape with Max',
user_id: null,
preview_url:
'https://speech.microsoft.com/assets/avatar/max/max-business-thumbnail.png',
'https://d2tm5q3cg1nlwf.cloudfront.net/preview_1727352836443.jpg',
composition: {
scenes: [
{
Expand Down Expand Up @@ -40,7 +40,7 @@ const templatesSeed = [
name: 'Portrait with Lori',
user_id: null,
preview_url:
'https://speech.microsoft.com/assets/avatar/lori/lori-casual-thumbnail.png',
'https://d2tm5q3cg1nlwf.cloudfront.net/preview_1727352905018.jpg',
composition: {
scenes: [
{
Expand Down Expand Up @@ -72,7 +72,7 @@ const templatesSeed = [
name: 'Landscape with Harry and Max',
user_id: null,
preview_url:
'https://speech.microsoft.com/assets/avatar/harry/harry-casual-thumbnail.png',
'https://d2tm5q3cg1nlwf.cloudfront.net/preview_1727352956648.jpg',
composition: {
scenes: [
{
Expand Down Expand Up @@ -123,7 +123,7 @@ const templatesSeed = [
name: 'Portrait with Lisa',
user_id: null,
preview_url:
'https://speech.microsoft.com/assets/avatar/lisa/lisa-casual-sitting-thumbnail.png',
'https://d2tm5q3cg1nlwf.cloudfront.net/preview_1727353044738.jpg',
composition: {
scenes: [
{
Expand Down Expand Up @@ -155,7 +155,7 @@ const templatesSeed = [
name: 'Landscape with Jeff',
user_id: null,
preview_url:
'https://speech.microsoft.com/assets/avatar/jeff/jeff-formal-thumbnail-bg.png',
'https://d2tm5q3cg1nlwf.cloudfront.net/preview_1727353097406.jpg',
composition: {
scenes: [
{
Expand Down

0 comments on commit 7bb52f9

Please sign in to comment.