347 lines
11 KiB
Go
347 lines
11 KiB
Go
package handlers
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
"Yimaru-Backend/internal/services/practicecontent"
|
|
"context"
|
|
"errors"
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func fullPracticeQuestionResponses(
|
|
questions []domain.QuestionWithDetails,
|
|
displayOrderByQuestionID map[int64]int32,
|
|
catalog []domain.QuestionTypeDefinition,
|
|
) []questionRes {
|
|
out := make([]questionRes, 0, len(questions))
|
|
for _, question := range questions {
|
|
res := buildQuestionRes(question, catalog)
|
|
if displayOrderByQuestionID != nil {
|
|
if order, ok := displayOrderByQuestionID[question.ID]; ok {
|
|
res.DisplayOrder = &order
|
|
}
|
|
}
|
|
out = append(out, res)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (h *Handler) buildFullPracticeResponse(
|
|
ctx context.Context,
|
|
result domain.FullUpdatePracticeResult,
|
|
catalog []domain.QuestionTypeDefinition,
|
|
) (fiber.Map, error) {
|
|
items, err := h.questionsSvc.GetQuestionSetItems(ctx, result.QuestionSet.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
displayOrderByQuestionID := make(map[int64]int32, len(items))
|
|
for _, item := range items {
|
|
displayOrderByQuestionID[item.QuestionID] = item.DisplayOrder
|
|
}
|
|
questionCount := int64(len(items))
|
|
qsRes := questionSetResFromDomain(result.QuestionSet)
|
|
qsRes.QuestionCount = &questionCount
|
|
return fiber.Map{
|
|
"practice": result.Practice,
|
|
"question_set": qsRes,
|
|
"questions": fullPracticeQuestionResponses(result.Questions, displayOrderByQuestionID, catalog),
|
|
}, nil
|
|
}
|
|
|
|
func questionSetResFromDomain(set domain.QuestionSet) questionSetRes {
|
|
return questionSetRes{
|
|
ID: set.ID,
|
|
Title: set.Title,
|
|
Description: set.Description,
|
|
SetType: set.SetType,
|
|
OwnerType: set.OwnerType,
|
|
OwnerID: set.OwnerID,
|
|
BannerImage: set.BannerImage,
|
|
Persona: set.Persona,
|
|
TimeLimitMinutes: set.TimeLimitMinutes,
|
|
PassingScore: set.PassingScore,
|
|
ShuffleQuestions: set.ShuffleQuestions,
|
|
Status: set.Status,
|
|
IntroVideoURL: set.IntroVideoURL,
|
|
CreatedAt: set.CreatedAt.String(),
|
|
}
|
|
}
|
|
|
|
func mapPracticeFullError(err error, action string) (int, string) {
|
|
switch {
|
|
case errors.Is(err, practicecontent.ErrPracticeNotFound):
|
|
return fiber.StatusNotFound, "Practice not found"
|
|
case errors.Is(err, practicecontent.ErrQuestionSetNotFound):
|
|
return fiber.StatusNotFound, "Question set not found"
|
|
case errors.Is(err, practicecontent.ErrQuestionNotFound):
|
|
return fiber.StatusNotFound, "Question not found"
|
|
case errors.Is(err, domain.ErrPersonaNotFound):
|
|
return fiber.StatusNotFound, "Persona not found"
|
|
case errors.Is(err, practicecontent.ErrInvalidQuestionItem):
|
|
return fiber.StatusBadRequest, "Invalid question item"
|
|
default:
|
|
if action == "load" {
|
|
return fiber.StatusInternalServerError, "Failed to load practice"
|
|
}
|
|
return fiber.StatusBadRequest, "Failed to update practice"
|
|
}
|
|
}
|
|
|
|
// GetLmsPracticeFull godoc
|
|
// @Summary Get LMS practice with question set and questions
|
|
// @Description Returns practice metadata, linked question set settings, and all questions in the set (same shape as PUT /practices/{id}/full).
|
|
// @Tags practices
|
|
// @Produce json
|
|
// @Param id path int true "Practice ID"
|
|
// @Success 200 {object} domain.Response
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Router /api/v1/practices/{id}/full [get]
|
|
func (h *Handler) GetLmsPracticeFull(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid practice id",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
result, err := h.practiceContentSvc.GetLmsPracticeFull(c.Context(), id)
|
|
if err != nil {
|
|
status, msg := mapPracticeFullError(err, "load")
|
|
return c.Status(status).JSON(domain.ErrorResponse{
|
|
Message: msg,
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
practice, ok := result.Practice.(domain.Practice)
|
|
if !ok {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice",
|
|
})
|
|
}
|
|
if !practice.VisibleToLearners() && !h.canManageLMSPractices(c) {
|
|
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
|
|
}
|
|
if err := h.applyPracticeAccess(c.Context(), c, []domain.Practice{practice}); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to build practice",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
result.Practice = practice
|
|
|
|
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load question type catalog",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
data, err := h.buildFullPracticeResponse(c.Context(), result, catalog)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice questions",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.JSON(domain.Response{
|
|
Message: "Practice retrieved successfully",
|
|
Data: data,
|
|
Success: true,
|
|
StatusCode: fiber.StatusOK,
|
|
})
|
|
}
|
|
|
|
// GetExamPrepPracticeFull godoc
|
|
// @Summary Get exam-prep practice with question set and questions
|
|
// @Description Returns exam-prep practice metadata, linked question set settings, and all questions in the set.
|
|
// @Tags exam-prep
|
|
// @Produce json
|
|
// @Param id path int true "Exam prep practice ID"
|
|
// @Success 200 {object} domain.Response
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Router /api/v1/exam-prep/practices/{id}/full [get]
|
|
func (h *Handler) GetExamPrepPracticeFull(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid practice id",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
result, err := h.practiceContentSvc.GetExamPrepPracticeFull(c.Context(), id)
|
|
if err != nil {
|
|
status, msg := mapPracticeFullError(err, "load")
|
|
return c.Status(status).JSON(domain.ErrorResponse{
|
|
Message: msg,
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
practice, ok := result.Practice.(domain.ExamPrepPractice)
|
|
if !ok {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice",
|
|
})
|
|
}
|
|
if !practice.VisibleToLearners() && !h.canManageExamPrepPractices(c) {
|
|
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
|
|
}
|
|
|
|
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load question type catalog",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
data, err := h.buildFullPracticeResponse(c.Context(), result, catalog)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice questions",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.JSON(domain.Response{
|
|
Message: "Practice retrieved successfully",
|
|
Data: data,
|
|
Success: true,
|
|
StatusCode: fiber.StatusOK,
|
|
})
|
|
}
|
|
|
|
// UpdateLmsPracticeFull godoc
|
|
// @Summary Full update LMS practice with question set and questions
|
|
// @Description Updates practice metadata, linked question set settings, and syncs questions in one request. Questions omitted from the questions array are removed from the set (not deleted from the bank).
|
|
// @Tags practices
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Practice ID"
|
|
// @Param body body domain.FullUpdatePracticeInput true "Full practice update payload"
|
|
// @Success 200 {object} domain.Response
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Router /api/v1/practices/{id}/full [put]
|
|
func (h *Handler) UpdateLmsPracticeFull(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid practice id",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
var req domain.FullUpdatePracticeInput
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid request body",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
result, err := h.practiceContentSvc.UpdateLmsPracticeFull(c.Context(), id, req)
|
|
if err != nil {
|
|
status, msg := mapPracticeFullError(err, "update")
|
|
return c.Status(status).JSON(domain.ErrorResponse{
|
|
Message: msg,
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load question type catalog",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
data, err := h.buildFullPracticeResponse(c.Context(), result, catalog)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice questions",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.JSON(domain.Response{
|
|
Message: "Practice updated successfully",
|
|
Data: data,
|
|
Success: true,
|
|
StatusCode: fiber.StatusOK,
|
|
})
|
|
}
|
|
|
|
// UpdateExamPrepPracticeFull godoc
|
|
// @Summary Full update exam-prep practice with question set and questions
|
|
// @Description Updates exam-prep practice metadata, linked question set settings, and syncs questions in one request.
|
|
// @Tags exam-prep
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Exam prep practice ID"
|
|
// @Param body body domain.FullUpdatePracticeInput true "Full practice update payload"
|
|
// @Success 200 {object} domain.Response
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Router /api/v1/exam-prep/practices/{id}/full [put]
|
|
func (h *Handler) UpdateExamPrepPracticeFull(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid practice id",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
var req domain.FullUpdatePracticeInput
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid request body",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
result, err := h.practiceContentSvc.UpdateExamPrepPracticeFull(c.Context(), id, req)
|
|
if err != nil {
|
|
status, msg := mapPracticeFullError(err, "update")
|
|
return c.Status(status).JSON(domain.ErrorResponse{
|
|
Message: msg,
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load question type catalog",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
data, err := h.buildFullPracticeResponse(c.Context(), result, catalog)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load practice questions",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.JSON(domain.Response{
|
|
Message: "Practice updated successfully",
|
|
Data: data,
|
|
Success: true,
|
|
StatusCode: fiber.StatusOK,
|
|
})
|
|
}
|