Spaces:
Sleeping
Sleeping
File size: 16,429 Bytes
c606a38 0722970 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 c1321a3 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 c606a38 c1321a3 0722970 c606a38 c1321a3 c606a38 c1321a3 c606a38 0722970 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 | 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 AdminExamController interface {
CreateExam(ctx *gin.Context)
UpdateExam(ctx *gin.Context)
DeleteExam(ctx *gin.Context)
GetExamDetail(ctx *gin.Context)
ListExams(ctx *gin.Context)
AssignToEvent(ctx *gin.Context)
UnassignFromEvent(ctx *gin.Context)
AssignToAcademy(ctx *gin.Context)
UnassignFromAcademy(ctx *gin.Context)
ListEventsByExam(ctx *gin.Context)
ListAcademiesByExam(ctx *gin.Context)
}
type adminExamController struct {
adminExamService services.AdminExamService
}
func NewAdminExamController(adminExamService services.AdminExamService) AdminExamController {
return &adminExamController{adminExamService: adminExamService}
}
// CreateExam godoc
// @Summary Admin: Create Exam
// @Description Create a new exam with configuration and proctoring settings
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body dto.CreateExamRequest true "Create Exam Request"
// @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/exams [post]
func (c *adminExamController) CreateExam(ctx *gin.Context) {
req := RequestJSON[dto.CreateExamRequest](ctx)
res, err := c.adminExamService.CreateExam(ctx.Request.Context(), req)
ResponseJSON(ctx, req, res, err)
}
// UpdateExam godoc
// @Summary Admin: Update Exam
// @Description Update an existing exam including configuration and proctoring settings
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @Param request body dto.CreateExamRequest true "Update Exam Request"
// @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/exams/{exam_id} [put]
func (c *adminExamController) UpdateExam(ctx *gin.Context) {
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
}
req := RequestJSON[dto.CreateExamRequest](ctx)
res, updateErr := c.adminExamService.UpdateExam(ctx.Request.Context(), examId, req)
ResponseJSON(ctx, req, res, updateErr)
}
// DeleteExam godoc
// @Summary Admin: Delete Exam
// @Description Soft delete an exam by ID
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @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/exams/{exam_id} [delete]
func (c *adminExamController) DeleteExam(ctx *gin.Context) {
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.adminExamService.DeleteExam(ctx.Request.Context(), examId)
ResponseJSON(ctx, gin.H{"exam_id": examId}, gin.H{"deleted": delErr == nil}, delErr)
}
// GetExamDetail godoc
// @Summary Admin: Get Exam Detail
// @Description Retrieve full exam details including configuration and proctoring
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @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/exams/{exam_id} [get]
func (c *adminExamController) GetExamDetail(ctx *gin.Context) {
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
}
res, getErr := c.adminExamService.GetExamDetail(ctx.Request.Context(), examId)
ResponseJSON(ctx, gin.H{"exam_id": examId}, res, getErr)
}
// ListExams godoc
// @Summary Admin: List Exams
// @Description Retrieve a paginated list of exams with event and academy assignment counts
// @Tags AdminExam
// @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 or slug"
// @Param sortBy query string false "Sort field (title, slug, created_at, duration, event_count, academy_count)"
// @Param order query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]dto.AdminExamResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/exams [get]
func (c *adminExamController) ListExams(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
}
offset := (page - 1) * limit
p := entity.Pagination{
Limit: limit,
Offset: offset,
Search: search,
SortBy: sortBy,
Order: order,
}
list, total, err := c.adminExamService.ListExams(ctx.Request.Context(), p)
if err != nil {
ResponseJSON[any, any](ctx, nil, nil, err)
return
}
totalPages := int((total + int64(limit) - 1) / int64(limit))
if total == 0 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// AssignToEvent godoc
// @Summary Admin: Assign Exam to Event
// @Description Assign an exam to an event
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @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/exams/{exam_id}/events/{event_id} [post]
func (c *adminExamController) AssignToEvent(ctx *gin.Context) {
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
}
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
}
assignErr := c.adminExamService.AssignToEvent(ctx.Request.Context(), examId, eventId)
ResponseJSON(ctx, gin.H{"exam_id": examId, "event_id": eventId}, gin.H{"assigned": assignErr == nil}, assignErr)
}
// UnassignFromEvent godoc
// @Summary Admin: Unassign Exam from Event
// @Description Remove an exam assignment from an event
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @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/exams/{exam_id}/events/{event_id} [delete]
func (c *adminExamController) UnassignFromEvent(ctx *gin.Context) {
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
}
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
}
delErr := c.adminExamService.UnassignFromEvent(ctx.Request.Context(), examId, eventId)
ResponseJSON(ctx, gin.H{"exam_id": examId, "event_id": eventId}, gin.H{"removed": delErr == nil}, delErr)
}
// AssignToAcademy godoc
// @Summary Admin: Assign Exam to Academy
// @Description Assign an exam to an academy
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @Param academy_id path string true "Academy 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/exams/{exam_id}/academies/{academy_id} [post]
func (c *adminExamController) AssignToAcademy(ctx *gin.Context) {
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
}
academyId, err := uuid.Parse(ctx.Param("academy_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Param("academy_id")}, nil, http_error.INVALID_TOKEN)
return
}
assignErr := c.adminExamService.AssignToAcademy(ctx.Request.Context(), examId, academyId)
ResponseJSON(ctx, gin.H{"exam_id": examId, "academy_id": academyId}, gin.H{"assigned": assignErr == nil}, assignErr)
}
// UnassignFromAcademy godoc
// @Summary Admin: Unassign Exam from Academy
// @Description Remove an exam assignment from an academy
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param exam_id path string true "Exam ID"
// @Param academy_id path string true "Academy 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/exams/{exam_id}/academies/{academy_id} [delete]
func (c *adminExamController) UnassignFromAcademy(ctx *gin.Context) {
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
}
academyId, err := uuid.Parse(ctx.Param("academy_id"))
if err != nil {
ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Param("academy_id")}, nil, http_error.INVALID_TOKEN)
return
}
delErr := c.adminExamService.UnassignFromAcademy(ctx.Request.Context(), examId, academyId)
ResponseJSON(ctx, gin.H{"exam_id": examId, "academy_id": academyId}, gin.H{"removed": delErr == nil}, delErr)
}
// ListEventsByExam godoc
// @Summary Admin: List Events by Exam
// @Description List all events that have this exam assigned
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @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 event title / slug / event code"
// @Param sortBy query string false "Sort field (title, slug, event_code, start_event, end_event, created_at)"
// @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/exams/{exam_id}/events [get]
func (c *adminExamController) ListEventsByExam(ctx *gin.Context) {
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", "")
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.adminExamService.ListEventsByExam(ctx.Request.Context(), 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{
"exam_id": examId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
// ListAcademiesByExam godoc
// @Summary Admin: List Academies by Exam
// @Description List all academies that have this exam assigned
// @Tags AdminExam
// @Accept json
// @Produce json
// @Security BearerAuth
// @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 academy title / slug / code"
// @Param sortBy query string false "Sort field (title, slug, code, created_at)"
// @Param order query string false "Sort direction (asc / desc)"
// @Success 200 {object} dto.SuccessResponse[[]entity.AcademyExamAssign]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/admin/exams/{exam_id}/academies [get]
func (c *adminExamController) ListAcademiesByExam(ctx *gin.Context) {
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", "")
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.adminExamService.ListAcademiesByExam(ctx.Request.Context(), 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{
"exam_id": examId,
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, list, nil)
}
|