|
1 | | -import { Injectable, NotFoundException } from '@nestjs/common'; |
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + NotFoundException, |
| 4 | + BadRequestException, |
| 5 | + ConflictException, |
| 6 | +} from '@nestjs/common'; |
2 | 7 | import { InjectRepository } from '@nestjs/typeorm'; |
3 | 8 | import { Repository, SelectQueryBuilder } from 'typeorm'; |
4 | 9 | import { |
@@ -281,4 +286,110 @@ export class CompetitionsService { |
281 | 286 | this.rankCache.set(cacheKey, { data: result, timestamp: Date.now() }); |
282 | 287 | return result; |
283 | 288 | } |
| 289 | + |
| 290 | + async joinCompetition( |
| 291 | + competitionId: string, |
| 292 | + user: User, |
| 293 | + ): Promise<CompetitionParticipant> { |
| 294 | + const competition = await this.competitionsRepository.findOne({ |
| 295 | + where: { id: competitionId }, |
| 296 | + }); |
| 297 | + |
| 298 | + if (!competition) { |
| 299 | + throw new NotFoundException( |
| 300 | + `Competition with ID "${competitionId}" not found`, |
| 301 | + ); |
| 302 | + } |
| 303 | + |
| 304 | + // Check if competition is active |
| 305 | + const now = new Date(); |
| 306 | + if (now >= competition.end_time) { |
| 307 | + throw new BadRequestException('Competition has already ended'); |
| 308 | + } |
| 309 | + |
| 310 | + // Check if user already joined |
| 311 | + const existing = await this.participantsRepository.findOne({ |
| 312 | + where: { |
| 313 | + user_id: user.id, |
| 314 | + competition_id: competitionId, |
| 315 | + }, |
| 316 | + }); |
| 317 | + |
| 318 | + if (existing) { |
| 319 | + throw new ConflictException('You have already joined this competition'); |
| 320 | + } |
| 321 | + |
| 322 | + // Check max participants |
| 323 | + if (competition.max_participants > 0) { |
| 324 | + const currentCount = await this.participantsRepository.count({ |
| 325 | + where: { competition_id: competitionId }, |
| 326 | + }); |
| 327 | + |
| 328 | + if (currentCount >= competition.max_participants) { |
| 329 | + throw new BadRequestException('Competition is full'); |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + // Create participant |
| 334 | + const participant = this.participantsRepository.create({ |
| 335 | + user_id: user.id, |
| 336 | + competition_id: competitionId, |
| 337 | + score: 0, |
| 338 | + }); |
| 339 | + |
| 340 | + const saved = await this.participantsRepository.save(participant); |
| 341 | + |
| 342 | + // Update participant count |
| 343 | + await this.competitionsRepository.increment( |
| 344 | + { id: competitionId }, |
| 345 | + 'participant_count', |
| 346 | + 1, |
| 347 | + ); |
| 348 | + |
| 349 | + return saved; |
| 350 | + } |
| 351 | + |
| 352 | + async leaveCompetition(competitionId: string, user: User): Promise<void> { |
| 353 | + const competition = await this.competitionsRepository.findOne({ |
| 354 | + where: { id: competitionId }, |
| 355 | + }); |
| 356 | + |
| 357 | + if (!competition) { |
| 358 | + throw new NotFoundException( |
| 359 | + `Competition with ID "${competitionId}" not found`, |
| 360 | + ); |
| 361 | + } |
| 362 | + |
| 363 | + // Check if competition has started |
| 364 | + const now = new Date(); |
| 365 | + if (now >= competition.start_time) { |
| 366 | + throw new BadRequestException( |
| 367 | + 'Cannot leave competition after it has started', |
| 368 | + ); |
| 369 | + } |
| 370 | + |
| 371 | + // Find participant |
| 372 | + const participant = await this.participantsRepository.findOne({ |
| 373 | + where: { |
| 374 | + user_id: user.id, |
| 375 | + competition_id: competitionId, |
| 376 | + }, |
| 377 | + }); |
| 378 | + |
| 379 | + if (!participant) { |
| 380 | + throw new NotFoundException( |
| 381 | + 'You are not a participant in this competition', |
| 382 | + ); |
| 383 | + } |
| 384 | + |
| 385 | + // Remove participant |
| 386 | + await this.participantsRepository.remove(participant); |
| 387 | + |
| 388 | + // Update participant count |
| 389 | + await this.competitionsRepository.decrement( |
| 390 | + { id: competitionId }, |
| 391 | + 'participant_count', |
| 392 | + 1, |
| 393 | + ); |
| 394 | + } |
284 | 395 | } |
0 commit comments