questions count

main
alex 2024-09-15 00:26:07 +02:00
parent 6a9cde458a
commit 8b05982273
3 changed files with 22 additions and 6 deletions

View File

@ -9,12 +9,13 @@ const (
// swagger:model LessonResponse
type LessonResponse struct {
Id string
State uint8
Title string
ThumbnailUrl string
CreatorUserId string
CreatedAt time.Time
Id string
State uint8
Title string
ThumbnailUrl string
QuestionsCount int `gorm:"-"`
CreatorUserId string
CreatedAt time.Time
}
// swagger:model CreateLessonResponse

View File

@ -41,6 +41,20 @@ func GetLessons(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError)
}
// get all questions for each lesson
for i, lesson := range lessons {
var questionsCount int64
if err := database.DB.Model(&models.Question{}).
Where("lesson_id = ?", lesson.Id).
Count(&questionsCount).Error; err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
lessons[i].QuestionsCount = int(questionsCount)
}
return c.JSON(lessons)
}

View File

@ -66,6 +66,7 @@ func GetRoles(c *fiber.Ctx) error {
var users []structs.HelperRoleUser
if err := database.DB.Model(&models.User{}).
Select("first_name", "last_name", "profile_picture_url").
Where("role_id = ? AND organization_id = ?", key, c.Locals("organizationId").(string)).
Find(&users).Error; err != nil {
return c.SendStatus(fiber.StatusInternalServerError)