Spaces:
Running
Running
File size: 27,012 Bytes
c606a38 0722970 30a4447 c606a38 875ef91 c606a38 875ef91 c606a38 0722970 c606a38 0722970 9743abc 0722970 30a4447 0722970 c606a38 0722970 c606a38 0722970 c606a38 0722970 c606a38 0722970 c606a38 0722970 853717a 0722970 853717a 0722970 c606a38 0722970 c606a38 853717a c606a38 853717a c606a38 0722970 c606a38 853717a c606a38 853717a c606a38 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | package controllers
import (
"strconv"
"abdanhafidz.com/go-boilerplate/models/dto"
entity "abdanhafidz.com/go-boilerplate/models/entity"
http_error "abdanhafidz.com/go-boilerplate/models/error"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type AdminEventController interface {
ListEvents(ctx *gin.Context)
CreateEvent(ctx *gin.Context)
UpdateEvent(ctx *gin.Context)
DeleteEvent(ctx *gin.Context)
ListParticipants(ctx *gin.Context)
ListCandidates(ctx *gin.Context)
ListExamCandidates(ctx *gin.Context)
AddParticipant(ctx *gin.Context)
RemoveParticipant(ctx *gin.Context)
ListEventExams(ctx *gin.Context)
RemoveExam(ctx *gin.Context)
ListResults(ctx *gin.Context)
GetAttemptReview(ctx *gin.Context)
UpdateResult(ctx *gin.Context)
DeleteResult(ctx *gin.Context)
}
type adminEventController struct {
adminEventService services.AdminEventService
}
func NewAdminEventController(adminEventService services.AdminEventService) AdminEventController {
return &adminEventController{adminEventService: adminEventService}
}
// ListEvents godoc
// @Summary Admin: List Events
// @Description Admin view of all events with participant count, exam count and optional filters/pagination
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by title / slug / event code"
// @Param sortBy query string false "Sort field (title, start_event, end_event, created_at, participant_count, exam_count)"
// @Param order query string false "Sort direction (asc / desc)"
// @Param status query string false "Filter by status (UPCOMING, ONGOING, ENDED)"
// @Success 200 {object} dto.SuccessResponse[[]dto.AdminEventResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events [get]
func (c *adminEventController) ListEvents(ctx *gin.Context) {
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
order := ctx.DefaultQuery("order", "")
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
var status *string
if val := ctx.Query("status"); val != "" {
if val == entity.EventStatusUpcoming || val == entity.EventStatusOngoing || val == entity.EventStatusEnded {
status = &val
}
}
offset := (page - 1) * limit
p := entity.Pagination{
Limit: limit,
Offset: offset,
Search: search,
SortBy: sortBy,
Order: order,
Status: status,
}
list, total, err := c.adminEventService.ListEvents(ctx.Request.Context(), p)
if err != nil {
ResponseJSON[any, any](ctx, nil, nil, err)
return
}
var totalPages int
if total == 0 {
totalPages = 1
} else {
totalPages = int((total + int64(limit) - 1) / int64(limit))
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// GetAttemptReview godoc
// @Summary Admin: Review Exam Attempt
// @Description Review a specific participant attempt for an event exam
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param attempt_id path string true "Attempt ID"
// @Param user_id path string true "User ID"
// @Success 200 {object} dto.SuccessResponse[dto.AdminExamReviewResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/attempts/{attempt_id}/{user_id}/review [get]
func (c *adminEventController) GetAttemptReview(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
attemptId, err := uuid.Parse(ctx.Param("attempt_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"attempt_id": ctx.Param("attempt_id")}, nil, http_error.INVALID_TOKEN)
return
}
userId, err := uuid.Parse(ctx.Param("user_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"user_id": ctx.Param("user_id")}, nil, http_error.INVALID_TOKEN)
return
}
review, reviewErr := c.adminEventService.GetAttemptReview(ctx.Request.Context(), eventId, attemptId, userId)
ResponseJSON(ctx, gin.H{"event_id": eventId, "attempt_id": attemptId, "user_id": userId}, review, reviewErr)
}
// CreateEvent godoc
// @Summary Admin: Create Event
// @Description Create a new event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body dto.CreateEventRequest true "Create Event Request"
// @Success 200 {object} dto.SuccessResponse[entity.Events]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events [post]
func (c *adminEventController) CreateEvent(ctx *gin.Context) {
req := RequestJSON[dto.CreateEventRequest](ctx)
res, err := c.adminEventService.CreateEvent(ctx.Request.Context(), req)
ResponseJSON(ctx, req, res, err)
}
// UpdateEvent godoc
// @Summary Admin: Update Event
// @Description Update an existing event by ID
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param request body dto.UpdateEventRequest true "Update Event Request"
// @Success 200 {object} dto.SuccessResponse[entity.Events]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id} [put]
func (c *adminEventController) UpdateEvent(ctx *gin.Context) {
id, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
req := RequestJSON[dto.UpdateEventRequest](ctx)
res, err := c.adminEventService.UpdateEvent(ctx.Request.Context(), id, req)
ResponseJSON(ctx, req, res, err)
}
// DeleteEvent godoc
// @Summary Admin: Delete Event
// @Description Delete an event by ID
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Success 200 {object} dto.SuccessResponse[map[string]bool]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id} [delete]
func (c *adminEventController) DeleteEvent(ctx *gin.Context) {
id, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
delErr := c.adminEventService.DeleteEvent(ctx.Request.Context(), id)
ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": delErr == nil}, delErr)
}
// ListParticipants godoc
// @Summary Admin: List Event Participants
// @Description List all participants assigned to an event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by username / full name"
// @Param sortBy query string false "Sort field (assigned_at, username, email, full_name)"
// @Param order query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.EventAssign]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/participants [get]
func (c *adminEventController) ListParticipants(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
order := ctx.DefaultQuery("order", "")
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
offset := (page - 1) * limit
p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order}
list, total, listErr := c.adminEventService.ListParticipants(ctx.Request.Context(), eventId, p)
if listErr != nil {
ResponseJSON[any, any](ctx, nil, nil, listErr)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"event_id": eventId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// ListCandidates godoc
// @Summary Admin: List of Event Candidates
// @Description List accounts that are not yet assigned to the event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by username / full name"
// @Param sortBy query string false "Sort field (username, full_name, created_at)"
// @Param orderBy query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.Account]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/candidate [get]
func (c *adminEventController) ListCandidates(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
order := ctx.DefaultQuery("orderBy", "")
if order == "" {
order = ctx.DefaultQuery("order", "")
}
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
offset := (page - 1) * limit
p := entity.Pagination{
Limit: limit,
Offset: offset,
Search: search,
SortBy: sortBy,
Order: order,
}
list, total, listErr := c.adminEventService.ListCandidates(ctx.Request.Context(), eventId, p)
if listErr != nil {
ResponseJSON[any, any](ctx, nil, nil, listErr)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"event_id": eventId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// ListExamCandidates godoc
// @Summary Admin: List Exam Candidates for Event
// @Description List exams that are not yet assigned to the event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by exam title / slug"
// @Param sortBy query string false "Sort field (title, slug, created_at, duration)"
// @Param orderBy query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.Exam]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams/candidate [get]
func (c *adminEventController) ListExamCandidates(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
if sortBy == "" {
sortBy = ctx.DefaultQuery("sortby", "")
}
order := ctx.DefaultQuery("orderBy", "")
if order == "" {
order = ctx.DefaultQuery("orderby", "")
}
if order == "" {
order = ctx.DefaultQuery("order", "")
}
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
offset := (page - 1) * limit
p := entity.Pagination{
Limit: limit,
Offset: offset,
Search: search,
SortBy: sortBy,
Order: order,
}
list, total, listErr := c.adminEventService.ListExamCandidates(ctx.Request.Context(), eventId, p)
if listErr != nil {
ResponseJSON[any, any](ctx, nil, nil, listErr)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"event_id": eventId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// AddParticipant godoc
// @Summary Admin: Add Participant to Event
// @Description Manually assign a user to an event (bypasses payment)
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param request body dto.AddParticipantRequest true "Add Participant Request"
// @Success 200 {object} dto.SuccessResponse[entity.EventAssign]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/participants [post]
func (c *adminEventController) AddParticipant(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
req := RequestJSON[dto.AddParticipantRequest](ctx)
userId, parseErr := uuid.Parse(req.UserId)
if parseErr != nil {
ResponseJSON[any](ctx, req, nil, http_error.INVALID_TOKEN)
return
}
assign, err := c.adminEventService.AddParticipant(ctx.Request.Context(), eventId, userId)
ResponseJSON(ctx, req, assign, err)
}
// RemoveParticipant godoc
// @Summary Admin: Remove Participant from Event
// @Description Unassign a user from an event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param user_id path string true "User ID"
// @Success 200 {object} dto.SuccessResponse[map[string]bool]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/participants/{user_id} [delete]
func (c *adminEventController) RemoveParticipant(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
userId, err := uuid.Parse(ctx.Param("user_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"user_id": ctx.Param("user_id")}, nil, http_error.INVALID_TOKEN)
return
}
delErr := c.adminEventService.RemoveParticipant(ctx.Request.Context(), eventId, userId)
ResponseJSON(ctx, gin.H{"event_id": eventId, "user_id": userId}, gin.H{"removed": delErr == nil}, delErr)
}
// ListEventExams godoc
// @Summary Admin: List Event Exams
// @Description List all exams assigned to an event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by exam title / slug"
// @Param sortBy query string false "Sort field (title, slug, created_at, duration)"
// @Param order query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.EventExamAssign]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams [get]
func (c *adminEventController) ListEventExams(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
order := ctx.DefaultQuery("order", "")
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
offset := (page - 1) * limit
p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order}
list, total, listErr := c.adminEventService.ListEventExams(ctx.Request.Context(), eventId, p)
if listErr != nil {
ResponseJSON[any, any](ctx, nil, nil, listErr)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"event_id": eventId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// RemoveExam godoc
// @Summary Admin: Remove Exam from Event
// @Description Unassign an exam from an event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param exam_id path string true "Exam ID"
// @Success 200 {object} dto.SuccessResponse[map[string]bool]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams/{exam_id} [delete]
func (c *adminEventController) RemoveExam(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
examId, err := uuid.Parse(ctx.Param("exam_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN)
return
}
delErr := c.adminEventService.RemoveExam(ctx.Request.Context(), eventId, examId)
ResponseJSON(ctx, gin.H{"event_id": eventId, "exam_id": examId}, gin.H{"removed": delErr == nil}, delErr)
}
// ListResults godoc
// @Summary Admin: List Exam Results for an Event
// @Description Retrieve all participant results for a specific exam within an event
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param exam_id path string true "Exam ID"
// @Param limit query int false "Items per page" default(10)
// @Param page query int false "Page number" default(1)
// @Param search query string false "Search by username / full name"
// @Param sortBy query string false "Sort field (final_score, created_at, username, full_name)"
// @Param order query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.Result]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams/{exam_id}/results [get]
func (c *adminEventController) ListResults(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
examId, err := uuid.Parse(ctx.Param("exam_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN)
return
}
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
search := ctx.DefaultQuery("search", "")
sortBy := ctx.DefaultQuery("sortBy", "")
if sortBy == "" {
sortBy = ctx.DefaultQuery("sortby", "")
}
order := ctx.DefaultQuery("order", "")
if order == "" {
order = ctx.DefaultQuery("orderBy", "")
}
if order == "" {
order = ctx.DefaultQuery("orderby", "")
}
if limit < 1 {
limit = 10
} else if limit > 100 {
limit = 100
}
if page < 1 {
page = 1
}
offset := (page - 1) * limit
p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order}
list, total, listErr := c.adminEventService.ListResults(ctx.Request.Context(), eventId, examId, p)
if listErr != nil {
ResponseJSON[any, any](ctx, nil, nil, listErr)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"event_id": eventId,
"exam_id": examId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// UpdateResult godoc
// @Summary Admin: Update Exam Result
// @Description Edit the final score of a participant's exam result
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param exam_id path string true "Exam ID"
// @Param result_id path string true "Result ID"
// @Param request body dto.UpdateResultRequest true "Update Result Request"
// @Success 200 {object} dto.SuccessResponse[entity.Result]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams/{exam_id}/results/{result_id} [put]
func (c *adminEventController) UpdateResult(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
examId, err := uuid.Parse(ctx.Param("exam_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN)
return
}
resultId, err := uuid.Parse(ctx.Param("result_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"result_id": ctx.Param("result_id")}, nil, http_error.INVALID_TOKEN)
return
}
req := RequestJSON[dto.UpdateResultRequest](ctx)
res, err := c.adminEventService.UpdateResult(ctx.Request.Context(), eventId, examId, resultId, req)
ResponseJSON(ctx, req, res, err)
}
// DeleteResult godoc
// @Summary Admin: Delete Exam Result
// @Description Delete a participant's exam result
// @Tags AdminEvent
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param event_id path string true "Event ID"
// @Param exam_id path string true "Exam ID"
// @Param result_id path string true "Result ID"
// @Success 200 {object} dto.SuccessResponse[map[string]bool]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/events/{event_id}/exams/{exam_id}/results/{result_id} [delete]
func (c *adminEventController) DeleteResult(ctx *gin.Context) {
eventId, err := uuid.Parse(ctx.Param("event_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN)
return
}
examId, err := uuid.Parse(ctx.Param("exam_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN)
return
}
resultId, err := uuid.Parse(ctx.Param("result_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"result_id": ctx.Param("result_id")}, nil, http_error.INVALID_TOKEN)
return
}
delErr := c.adminEventService.DeleteResult(ctx.Request.Context(), eventId, examId, resultId)
ResponseJSON(ctx, gin.H{"result_id": resultId}, gin.H{"deleted": delErr == nil}, delErr)
}
|