Yimaru-BackEnd/internal/web_server/handlers/practice_handler.go
Yared Yemane c77a97b40d Return learner-visible practices with access metadata.
Expose practices to learner roles based on practice shell publish state and include per-practice access fields derived from linked question set readiness so clients can manage completion/access UX explicitly.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-28 00:32:44 -07:00

297 lines
13 KiB
Go

package handlers
import (
"Yimaru-Backend/internal/domain"
"Yimaru-Backend/internal/services/courses"
"Yimaru-Backend/internal/services/lessons"
"Yimaru-Backend/internal/services/modules"
"Yimaru-Backend/internal/services/practices"
"context"
"errors"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
)
// CreatePractice godoc
// @Tags practices
// @Accept json
// @Param body body domain.CreatePracticeInput true "Practice (parent_kind: COURSE | MODULE | LESSON)"
// @Router /api/v1/practices [post]
func (h *Handler) CreatePractice(c *fiber.Ctx) error {
var req domain.CreatePracticeInput
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid request body",
Error: err.Error(),
})
}
if valErrs, ok := h.validator.Validate(c, req); !ok {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Validation failed",
Error: firstValidationError(valErrs),
})
}
p, err := h.practiceSvc.Create(c.Context(), req)
if err != nil {
switch {
case errors.Is(err, courses.ErrCourseNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Course not found", Error: err.Error()})
case errors.Is(err, modules.ErrModuleNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Module not found", Error: err.Error()})
case errors.Is(err, lessons.ErrLessonNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Lesson not found", Error: err.Error()})
case errors.Is(err, practices.ErrQuestionSetNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Question set not found", Error: err.Error()})
case errors.Is(err, domain.ErrPersonaNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Persona not found", Error: err.Error()})
case errors.Is(err, practices.ErrInvalidPracticeParent):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid parent", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to create practice", Error: err.Error()})
}
actorID := c.Locals("user_id").(int64)
actorRole := string(c.Locals("role").(domain.Role))
ip := c.IP()
ua := c.Get("User-Agent")
rid := p.ID
go h.activityLogSvc.RecordAction(context.Background(), &actorID, &actorRole, domain.ActionPracticeCreated, domain.ResourcePractice, &rid, "Created practice: "+p.Title, nil, &ip, &ua)
return c.Status(fiber.StatusCreated).JSON(domain.Response{
Message: "Practice created successfully",
Data: p,
Success: true,
StatusCode: fiber.StatusCreated,
})
}
// ListPracticesByCourse godoc
// @Tags practices
// @Param id path int true "Course ID"
// @Router /api/v1/courses/{id}/practices [get]
func (h *Handler) ListPracticesByCourse(c *fiber.Ctx) error {
courseID, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid course id", Error: err.Error()})
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
items, total, err := h.practiceSvc.ListByCourse(c.Context(), courseID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, courses.ErrCourseNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Course not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to list practices", Error: err.Error()})
}
if err := h.applyPracticeAccess(c.Context(), c, items); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to build practices", Error: err.Error()})
}
return c.JSON(domain.Response{
Message: "Practices retrieved successfully",
Data: fiber.Map{
"practices": items,
"total_count": total,
"limit": limit,
"offset": offset,
},
Success: true,
StatusCode: fiber.StatusOK,
})
}
// ListPracticesByModule godoc
// @Tags practices
// @Param id path int true "Module ID"
// @Router /api/v1/modules/{id}/practices [get]
func (h *Handler) ListPracticesByModule(c *fiber.Ctx) error {
moduleID, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid module id", Error: err.Error()})
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
items, total, err := h.practiceSvc.ListByModule(c.Context(), moduleID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, modules.ErrModuleNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Module not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to list practices", Error: err.Error()})
}
if err := h.applyPracticeAccess(c.Context(), c, items); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to build practices", Error: err.Error()})
}
return c.JSON(domain.Response{
Message: "Practices retrieved successfully",
Data: fiber.Map{
"practices": items,
"total_count": total,
"limit": limit,
"offset": offset,
},
Success: true,
StatusCode: fiber.StatusOK,
})
}
// ListPracticesByLesson godoc
// @Tags practices
// @Param id path int true "Lesson ID"
// @Router /api/v1/lessons/{id}/practices [get]
func (h *Handler) ListPracticesByLesson(c *fiber.Ctx) error {
lessonID, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid lesson id", Error: err.Error()})
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
items, total, err := h.practiceSvc.ListByLesson(c.Context(), lessonID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, lessons.ErrLessonNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Lesson not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to list practices", Error: err.Error()})
}
if err := h.applyPracticeAccess(c.Context(), c, items); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to build practices", Error: err.Error()})
}
return c.JSON(domain.Response{
Message: "Practices retrieved successfully",
Data: fiber.Map{
"practices": items,
"total_count": total,
"limit": limit,
"offset": offset,
},
Success: true,
StatusCode: fiber.StatusOK,
})
}
// GetPractice godoc
// @Tags practices
// @Param id path int true "Practice ID"
// @Router /api/v1/practices/{id} [get]
func (h *Handler) GetPractice(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()})
}
p, err := h.practiceSvc.GetByID(c.Context(), id)
if err != nil {
if errors.Is(err, practices.ErrPracticeNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to load practice", Error: err.Error()})
}
if !p.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{p}); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to build practice", Error: err.Error()})
}
role, _ := c.Locals("role").(domain.Role)
if role.IsCustomerLearnerRole() {
if set, err := h.questionsSvc.GetQuestionSetByID(c.Context(), p.QuestionSetID); err == nil {
p.Access = practiceAccessForQuestionSet(set)
} else {
p.Access = &domain.PracticeAccess{IsAccessible: false, Reason: "Question set not found"}
}
}
return c.JSON(domain.Response{Message: "Practice retrieved successfully", Data: p, Success: true, StatusCode: fiber.StatusOK})
}
func (h *Handler) applyPracticeAccess(ctx context.Context, c *fiber.Ctx, items []domain.Practice) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
for i := range items {
items[i].Access = nil
}
return nil
}
for i := range items {
set, err := h.questionsSvc.GetQuestionSetByID(ctx, items[i].QuestionSetID)
if err != nil {
items[i].Access = &domain.PracticeAccess{IsAccessible: false, Reason: "Question set not found"}
continue
}
items[i].Access = practiceAccessForQuestionSet(set)
}
return nil
}
func practiceAccessForQuestionSet(set domain.QuestionSet) *domain.PracticeAccess {
if !strings.EqualFold(set.SetType, string(domain.QuestionSetTypePractice)) {
return &domain.PracticeAccess{IsAccessible: false, Reason: "Question set is not a practice"}
}
if !strings.EqualFold(set.Status, "PUBLISHED") {
return &domain.PracticeAccess{IsAccessible: false, Reason: "Practice is not published yet"}
}
return &domain.PracticeAccess{IsAccessible: true}
}
// UpdatePractice godoc
// @Tags practices
// @Param id path int true "Practice ID"
// @Param body body domain.UpdatePracticeInput true "Fields to update (parent is immutable)"
// @Router /api/v1/practices/{id} [put]
func (h *Handler) UpdatePractice(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.UpdatePracticeInput
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid request body", Error: err.Error()})
}
p, err := h.practiceSvc.Update(c.Context(), id, req)
if err != nil {
if errors.Is(err, practices.ErrPracticeNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found", Error: err.Error()})
}
if errors.Is(err, practices.ErrQuestionSetNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Question set not found", Error: err.Error()})
}
if errors.Is(err, domain.ErrPersonaNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Persona not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to update practice", Error: err.Error()})
}
actorID := c.Locals("user_id").(int64)
actorRole := string(c.Locals("role").(domain.Role))
ip := c.IP()
ua := c.Get("User-Agent")
rid := p.ID
go h.activityLogSvc.RecordAction(context.Background(), &actorID, &actorRole, domain.ActionPracticeUpdated, domain.ResourcePractice, &rid, "Updated practice: "+p.Title, nil, &ip, &ua)
return c.JSON(domain.Response{Message: "Practice updated successfully", Data: p, Success: true, StatusCode: fiber.StatusOK})
}
// DeletePractice godoc
// @Tags practices
// @Param id path int true "Practice ID"
// @Router /api/v1/practices/{id} [delete]
func (h *Handler) DeletePractice(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()})
}
if err := h.practiceSvc.Delete(c.Context(), id); err != nil {
if errors.Is(err, practices.ErrPracticeNotFound) {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found", Error: err.Error()})
}
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to delete practice", Error: err.Error()})
}
actorID := c.Locals("user_id").(int64)
actorRole := string(c.Locals("role").(domain.Role))
ip := c.IP()
ua := c.Get("User-Agent")
go h.activityLogSvc.RecordAction(context.Background(), &actorID, &actorRole, domain.ActionPracticeDeleted, domain.ResourcePractice, &id, "Deleted practice", nil, &ip, &ua)
return c.JSON(domain.Response{Message: "Practice deleted successfully", Success: true, StatusCode: fiber.StatusOK})
}