Skip to content

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Szotkowski committed Sep 14, 2023
1 parent faf3c42 commit e3b2563
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/controllers/instruction-step.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,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
9 changes: 6 additions & 3 deletions src/controllers/user-instruction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,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 @@ -663,10 +663,13 @@ export class UserInstructionController {
if (!keyMatch) {
throw new HttpErrors.Unauthorized('Invalid password');
}
const instruction = await this.instructionRepository.findById(instructionId);
const instruction =
await this.instructionRepository.findById(instructionId);
instruction.premiumUserIds = instruction.premiumUserIds ?? [];
if (instruction.premiumUserIds.includes(userId)) {
instruction.premiumUserIds = instruction.premiumUserIds.filter(id => id !== userId);
instruction.premiumUserIds = instruction.premiumUserIds.filter(
id => id !== userId,
);
} else {
instruction.premiumUserIds.push(userId);
}
Expand Down
37 changes: 16 additions & 21 deletions src/controllers/user-progress.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import {authenticate} from '@loopback/authentication';
import {inject} from '@loopback/core';
import {
repository
} from '@loopback/repository';
import {repository} from '@loopback/repository';
import {
HttpErrors,
del,
get,
param,
patch,
post,
requestBody
requestBody,
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {Progress, ProgressRelations} from '../models';
import {
Progress, ProgressRelations
} from '../models';
import {InstructionRepository, ProgressRepository, UserRepository} from '../repositories';
InstructionRepository,
ProgressRepository,
UserRepository,
} from '../repositories';
import {JWTService} from '../services';

export class UserProgressController {
Expand All @@ -28,8 +28,9 @@ export class UserProgressController {
@repository(InstructionRepository)
protected instructionRepository: InstructionRepository,
@repository(UserRepository) protected userRepository: UserRepository,
@repository(UserRepository) protected progressRepository: ProgressRepository,
) { }
@repository(UserRepository)
protected progressRepository: ProgressRepository,
) {}

@authenticate('jwt')
@post('/users/{id}/progresses/{progressId}', {
Expand Down Expand Up @@ -57,11 +58,7 @@ export class UserProgressController {
stepId: {type: 'number'},
descriptionId: {type: 'number'},
},
required: [
'instructionId',
'stepId',
'descriptionId',
],
required: ['instructionId', 'stepId', 'descriptionId'],
},
},
},
Expand All @@ -83,7 +80,7 @@ export class UserProgressController {
'Progress with this instructionId and userId already exists.',
);
}
this.progressRepository.create({
await this.progressRepository.create({
...progress,
userId: this.user.id,
});
Expand Down Expand Up @@ -129,7 +126,7 @@ export class UserProgressController {
const progressOriginal = await this.progressRepository.findOne({
where: {
instructionId: instructionId,
userId: this.user.id
userId: this.user.id,
},
});
if (!progressOriginal) {
Expand Down Expand Up @@ -165,7 +162,7 @@ export class UserProgressController {
const progress = await this.progressRepository.findOne({
where: {
instructionId: instructionId,
userId: this.user.id
userId: this.user.id,
},
});
if (!progress) {
Expand Down Expand Up @@ -200,7 +197,7 @@ export class UserProgressController {
})
async find(
@param.path.number('instructionId') instructionId: number,
): Promise<(Progress & ProgressRelations)> {
): Promise<Progress & ProgressRelations> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
throw new HttpErrors.NotFound('User not found');
Expand All @@ -219,9 +216,7 @@ export class UserProgressController {

private validateProgressOwnership(progress: Progress): void {
if (Number(progress.userId) !== Number(this.user.id)) {
throw new HttpErrors.Forbidden(
'You are not authorized to this progress',
);
throw new HttpErrors.Forbidden('You are not authorized to this progress');
}
}
}
4 changes: 1 addition & 3 deletions src/repositories/progress.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export class ProgressRepository extends DefaultCrudRepository<
typeof Progress.prototype.id,
ProgressRelations
> {
constructor(
@inject('datasources.db') dataSource: DbDataSource,
) {
constructor(@inject('datasources.db') dataSource: DbDataSource) {
super(Progress, dataSource);
}
}
19 changes: 15 additions & 4 deletions src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ export class UserRepository extends DefaultCrudRepository<
typeof User.prototype.id
>;

public readonly progresses: HasManyRepositoryFactory<Progress, typeof User.prototype.id>;
public readonly progresses: HasManyRepositoryFactory<
Progress,
typeof User.prototype.id
>;

constructor(
@inject('datasources.db') dataSource: DbDataSource,
@repository.getter('InstructionRepository')
protected instructionRepositoryGetter: Getter<InstructionRepository>, @repository.getter('ProgressRepository') protected progressRepositoryGetter: Getter<ProgressRepository>,
protected instructionRepositoryGetter: Getter<InstructionRepository>,
@repository.getter('ProgressRepository')
protected progressRepositoryGetter: Getter<ProgressRepository>,
) {
super(User, dataSource);
this.progresses = this.createHasManyRepositoryFactoryFor('progresses', progressRepositoryGetter,);
this.registerInclusionResolver('progresses', this.progresses.inclusionResolver);
this.progresses = this.createHasManyRepositoryFactoryFor(
'progresses',
progressRepositoryGetter,
);
this.registerInclusionResolver(
'progresses',
this.progresses.inclusionResolver,
);
this.instructions = this.createHasManyRepositoryFactoryFor(
'instructions',
instructionRepositoryGetter,
Expand Down

0 comments on commit e3b2563

Please sign in to comment.