453 lines
20 KiB
Go
453 lines
20 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 (optional parents; link later via PUT /practices/:id/parents)"
|
|
// @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()})
|
|
case errors.Is(err, practices.ErrPracticeParentLimitExceeded):
|
|
return c.Status(fiber.StatusConflict).JSON(domain.ErrorResponse{
|
|
Message: "This course, module, or lesson already has a practice",
|
|
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,
|
|
})
|
|
}
|
|
|
|
// ListPractices godoc
|
|
// @Tags practices
|
|
// @Param limit query int false "Page size (default 20, max 200)"
|
|
// @Param offset query int false "Page offset (default 0)"
|
|
// @Param unlinked_only query bool false "When true, return only practices with no parent links (admin)"
|
|
// @Router /api/v1/practices [get]
|
|
func (h *Handler) ListPractices(c *fiber.Ctx) error {
|
|
limit, _ := strconv.Atoi(c.Query("limit", "20"))
|
|
offset, _ := strconv.Atoi(c.Query("offset", "0"))
|
|
publishedOnly := h.hideUnpublishedPractices(c)
|
|
unlinkedOnly := false
|
|
if h.canManageLMSPractices(c) {
|
|
unlinkedOnly, _ = strconv.ParseBool(c.Query("unlinked_only", "false"))
|
|
}
|
|
items, total, err := h.practiceSvc.ListAll(c.Context(), publishedOnly, unlinkedOnly, int32(limit), int32(offset))
|
|
if err != nil {
|
|
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,
|
|
})
|
|
}
|
|
|
|
// 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"))
|
|
publishedOnly := h.hideUnpublishedPractices(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"))
|
|
publishedOnly := h.hideUnpublishedPractices(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"))
|
|
publishedOnly := h.hideUnpublishedPractices(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,
|
|
})
|
|
}
|
|
|
|
type practiceWithStimulusRes struct {
|
|
domain.Practice
|
|
StimulusBlocks []domain.PracticeStimulusBlock `json:"stimulus_blocks,omitempty"`
|
|
}
|
|
|
|
// 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"}
|
|
}
|
|
}
|
|
stimulusBlocks, err := h.practiceContentSvc.GetLmsPracticeStimulusBlocks(c.Context(), p.ID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to load shared stimulus data",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
return c.JSON(domain.Response{
|
|
Message: "Practice retrieved successfully",
|
|
Data: practiceWithStimulusRes{
|
|
Practice: p,
|
|
StimulusBlocks: stimulusBlocks,
|
|
},
|
|
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 "Practice metadata fields to update"
|
|
// @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})
|
|
}
|
|
|
|
// UpdatePracticeParents godoc
|
|
// @Tags practices
|
|
// @Accept json
|
|
// @Param id path int true "Practice ID"
|
|
// @Param body body domain.UpdatePracticeParentsInput true "Parent links (replaces all; use empty array to detach)"
|
|
// @Router /api/v1/practices/{id}/parents [put]
|
|
func (h *Handler) UpdatePracticeParents(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.UpdatePracticeParentsInput
|
|
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.UpdateParents(c.Context(), id, req)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, practices.ErrPracticeNotFound):
|
|
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found", Error: err.Error()})
|
|
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.ErrInvalidPracticeParent):
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid parent", Error: err.Error()})
|
|
case errors.Is(err, practices.ErrPracticeParentLimitExceeded):
|
|
return c.Status(fiber.StatusConflict).JSON(domain.ErrorResponse{
|
|
Message: "This course, module, or lesson already has a practice",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to update practice parents", 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 parents: "+p.Title, nil, &ip, &ua)
|
|
return c.JSON(domain.Response{Message: "Practice parents updated successfully", Data: p, Success: true, StatusCode: fiber.StatusOK})
|
|
}
|
|
|
|
// UnlinkPracticeParent godoc
|
|
// @Tags practices
|
|
// @Param id path int true "Practice ID"
|
|
// @Param parentKind path string true "Parent kind (COURSE, MODULE, LESSON)"
|
|
// @Param parentId path int true "Parent entity ID"
|
|
// @Router /api/v1/practices/{id}/parents/{parentKind}/{parentId} [delete]
|
|
func (h *Handler) UnlinkPracticeParent(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()})
|
|
}
|
|
parentID, err := strconv.ParseInt(c.Params("parentId"), 10, 64)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "Invalid parent id", Error: err.Error()})
|
|
}
|
|
kind, ok := domain.ParsePracticeParentKind(c.Params("parentKind"))
|
|
if !ok {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Invalid parent kind",
|
|
Error: "parent_kind must be COURSE, MODULE, or LESSON",
|
|
})
|
|
}
|
|
p, err := h.practiceSvc.UnlinkParent(c.Context(), id, domain.PracticeParent{Kind: kind, ID: parentID})
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, practices.ErrPracticeNotFound):
|
|
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice 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()})
|
|
case errors.Is(err, practices.ErrPracticeParentLinkNotFound):
|
|
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
|
Message: "Practice is not linked to that parent",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to unlink practice parent", 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, "Unlinked practice parent "+string(kind)+": "+p.Title, nil, &ip, &ua)
|
|
return c.JSON(domain.Response{Message: "Practice parent unlinked 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})
|
|
}
|