The-Bugger-Ducks / owl-partners-back

API do projeto "Owl Partners" (5º DSM - 2023, FATEC Profº Jessen Vidal - SJC)
https://owlpartners.onrender.com/api
2 stars 0 forks source link

Feat/auth commentary #16

Closed GabrielCamargoL closed 1 year ago

GabrielCamargoL commented 1 year ago

12 #11 #10

CRUD comentarios sobre uma parceria

@Controller('partners')
@ApiTags('partnerComments')
export class PartnerCommentController {
  constructor(private readonly partnerCommentService: PartnerCommentService) { }

  @Post('comment')
  async addComment(@Body() commentData: CreateCommentDTO) {
    return await this.partnerCommentService.addComment(commentData);
  }

  @Get('comment/:partnerId')
  async list(@Param('partnerId') partnerId: string) {
    return await this.partnerCommentService.listCommentsByPartner(partnerId);
  }

  @Put('comment/:commentId')
  async update(@Param('commentId') commentId: string, @Body() commentData: UpdateCommentDTO) {
    return await this.partnerCommentService.update(commentId, commentData);
  }
}

@Injectable()
export class PartnerCommentService {
  constructor(private readonly prismaService: PrismaService) { }

  async addComment(comment: CreateCommentDTO) {
    return await this.prismaService.partnerComment.create({
      data: comment
    })
  }

  async listCommentsByPartner(id: string) {
    return await this.prismaService.partnerComment.findMany({
      where: {
        partnerId: id
      },
      orderBy: {
        // createdAt: 'asc',
        updatedAt: 'asc'
      },
    })
  }

  update(id: string, comment: UpdateCommentDTO) {
    return this.prismaService.partnerComment.update({
      data: comment,
      where: {
        id,
      },
    });
  }
}

Swagger


const config = new DocumentBuilder()
    .setTitle('OwlPartners')
    .setDescription('OwlPartners Service')
    .setVersion('1.0')
    .addTag('owlPartners')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);


Autenticação JWT feita, porém, desativada devido ao escopo da sprint não suportá-lo


@Controller('auth')
@ApiTags('Auth')
export class AuthController {
  constructor(private readonly authService: AuthService) { }

  @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Req() req: any) {
    return await this.authService.login(req.user);
  }
}
@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
  ) { }

  async login(user) {
    user.password = undefined;
    const payload = { sub: user.id, email: user.email, role: user.role };

    return {
      user,
      token: this.jwtService.sign(payload),
    };
  }

  async validateUser(email: string, password: string) {
    let user;
    try {
      user = await this.userService.findUserByEmail(email);
    } catch (error) {
      return null;
    }

    const isPasswordValid = compareSync(password, user.password);
    if (!isPasswordValid) return null;

    return user;
  }
}

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({ usernameField: 'email' })
  }

  async validate(email: string, password: string) {
    const user = await this.authService.validateUser(email, password);

    if (!user) throw new UnauthorizedException('E-mail e/ou são senha invalidos')

    return user;
  }

}