Skip to content

Commit

Permalink
edit gets
Browse files Browse the repository at this point in the history
  • Loading branch information
Szotkowski committed Sep 10, 2023
1 parent 05f1682 commit 1982f2c
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 49 deletions.
40 changes: 34 additions & 6 deletions src/controllers/instruction-step.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
RestBindings,
del,
get,
getModelSchemaRef,
param,
patch,
post,
requestBody,
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {Instruction, Step, StepRelations} from '../models';
import {Instruction, Step} from '../models';
import {InstructionRepository, StepRepository} from '../repositories';
import {ImgurService} from '../services';

Expand All @@ -32,7 +31,7 @@ export class InstructionStepController {
@repository(InstructionRepository)
public instructionRepository: InstructionRepository,
@repository(StepRepository) public stepRepository: StepRepository,
) {}
) { }

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}/steps/{stepId}', {
Expand Down Expand Up @@ -208,15 +207,40 @@ export class InstructionStepController {
description: 'Get steps instruction',
content: {
'application/json': {
schema: getModelSchemaRef(Step),
schema: {
type: 'object',
properties: {
steps: {
type: 'object',
items: {
id: {type: 'number'},
titleCz: {type: 'string'},
titleEn: {type: 'string'},
descriptionCz: {
type: 'array',
items: {
type: 'string',
},
},
descriptionEn: {
type: 'array',
items: {
type: 'string',
},
},
link: {type: 'string'},
},
},
},
},
},
},
},
},
})
async getPublicInstructions(
@param.path.number('instructionId') instructionId: number,
): Promise<(Step & StepRelations)[]> {
): Promise<Omit<Step, 'deleteHash'>[]> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
throw new HttpErrors.NotFound('User not found');
Expand All @@ -226,7 +250,11 @@ export class InstructionStepController {
if (!instruction) {
throw new HttpErrors.NotFound('Instruction not found');
}
const data = await this.stepRepository.find();
const data = await this.stepRepository.find({
fields: {
deleteHash: false,
},
});
return data;
}

Expand Down
126 changes: 119 additions & 7 deletions src/controllers/user-instruction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import {
RestBindings,
del,
get,
getModelSchemaRef,
param,
patch,
post,
requestBody,
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import * as dotenv from 'dotenv';
import {Difficulty, Instruction, InstructionRelations} from '../models';
import {Difficulty, Instruction} from '../models';
import {
InstructionRepository,
StepRepository,
Expand All @@ -39,7 +38,7 @@ export class UserInstructionController {
@repository(InstructionRepository)
protected instructionRepository: InstructionRepository,
@repository(StepRepository) public stepRepository: StepRepository,
) {}
) { }

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}', {
Expand Down Expand Up @@ -214,15 +213,51 @@ export class UserInstructionController {
description: 'Get users instructions',
content: {
'application/json': {
schema: getModelSchemaRef(Instruction),
schema: {
type: 'object',
properties: {
instructions: {
type: 'object',
items: {
id: {type: 'number'},
titleCz: {type: 'string'},
titleEn: {type: 'string'},
difficulty: {enum: Object.values(Difficulty)},
link: {type: 'string'},
private: {type: 'boolean'},
premium: {type: 'boolean'},
date: {type: 'string'},
steps: {
type: 'object',
items: {
id: {type: 'number'},
titleCz: {type: 'string'},
titleEn: {type: 'string'},
descriptionCz: {
type: 'array',
items: {
type: 'string',
},
},
descriptionEn: {
type: 'array',
items: {
type: 'string',
},
},
link: {type: 'string'},
},
},
},
},
},
},
},
},
},
},
})
async getUsersInstructions(): Promise<
(Instruction & InstructionRelations)[]
> {
async getUsersInstructions(): Promise<Omit<Instruction, 'deleteHash'>[]> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
throw new HttpErrors.NotFound('User not found');
Expand All @@ -234,8 +269,16 @@ export class UserInstructionController {
include: [
{
relation: 'steps',
scope: {
fields: {
deleteHash: false,
},
},
},
],
fields: {
deleteHash: false,
},
});
return data;
}
Expand Down Expand Up @@ -383,6 +426,75 @@ export class UserInstructionController {
return true;
}

@get('/public-instructions', {
responses: {
'200': {
description: 'Get public instructions',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
instructions: {
type: 'object',
items: {
id: {type: 'number'},
titleCz: {type: 'string'},
titleEn: {type: 'string'},
difficulty: {enum: Object.values(Difficulty)},
link: {type: 'string'},
private: {type: 'boolean'},
premium: {type: 'boolean'},
date: {type: 'string'},
steps: {
type: 'object',
items: {
id: {type: 'number'},
titleCz: {type: 'string'},
titleEn: {type: 'string'},
descriptionCz: {
type: 'array',
items: {
type: 'string',
},
},
descriptionEn: {
type: 'array',
items: {
type: 'string',
},
},
link: {type: 'string'},
},
},
},
},
},
},
},
},
},
},
})
async getPublicInstructions(
@param.query.number('limit') limit: number = 10,
@param.query.number('offset') offset: number = 0,
): Promise<Omit<Instruction, 'deleteHash'>[]> {
const data = await this.instructionRepository.find({
where: {
private: false,
},
include: [
{
relation: 'steps',
},
],
limit,
skip: offset,
});
return data;
}

private validateInstructionOwnership(instruction: Instruction): void {
if (Number(instruction.userId) !== Number(this.user.id)) {
throw new HttpErrors.Forbidden(
Expand Down
Loading

0 comments on commit 1982f2c

Please sign in to comment.