184 lines
4.9 KiB
Go
184 lines
4.9 KiB
Go
package Controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"larago/app/Model"
|
|
"larago/config"
|
|
"strconv"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
)
|
|
|
|
func Pacient(router *gin.RouterGroup) {
|
|
|
|
router.GET("/api", ApiViewPacient)
|
|
router.GET("/courses", ApiPacientCourse)
|
|
router.GET("/:pacient_id/:course_id", ApiPacientCourseView)
|
|
router.GET("/:pacient_id/:course_id/:exercise_id", ApiPacientExerciseView)
|
|
router.POST("/:pacient_id/:course_id/:exercise_id", ApiPacientExercisePost)
|
|
|
|
}
|
|
|
|
type PacientStartExerciseValidation struct {
|
|
TimeUsers string `form:"TimeUsers" json:"TimeUsers" binding:"required"`
|
|
}
|
|
|
|
func ApiPacientExercisePost(c *gin.Context) {
|
|
// Validate input
|
|
var user_course Model.UserCourseModel
|
|
var input PacientStartExerciseValidation
|
|
|
|
if err := config.DB.Where("IDUsers = ?", c.Param("pacient_id")).Where("IDCourse = ?", c.Param("course_id")).First(&user_course).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
uint_pacient, err := strconv.ParseUint(c.Param("pacient_id"), 10, 0)
|
|
if err != nil {
|
|
fmt.Println("Ошибка преобразования pacient:", err)
|
|
return
|
|
}
|
|
uint_exercise, err := strconv.ParseUint(c.Param("exercise_id"), 10, 0)
|
|
if err != nil {
|
|
fmt.Println("Ошибка преобразования exercise:", err)
|
|
return
|
|
}
|
|
course_exercise := Model.UserExerciseCourseModel{
|
|
IDUser: uint(uint_pacient),
|
|
IDExerciseCourse: uint(uint_exercise),
|
|
TimeUsers: input.TimeUsers,
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Save(&course_exercise)
|
|
|
|
c.IndentedJSON(http.StatusCreated, course_exercise)
|
|
|
|
}
|
|
func ApiPacientExerciseView(c *gin.Context) { // Get model if exist
|
|
|
|
var user_course Model.UserCourseModel
|
|
var model Model.ExerciseModel
|
|
var course_exercise Model.ExerciseCourseModel
|
|
|
|
claims, exists := c.Get("claims")
|
|
|
|
if !exists {
|
|
c.IndentedJSON(http.StatusOK, gin.H{"redirect": "redirect_auth_login"})
|
|
c.Abort()
|
|
}
|
|
|
|
userClaims := claims.(*jwt.MapClaims)
|
|
|
|
user_name := (*userClaims)["user_name"].(string)
|
|
|
|
//Gorm_SQL
|
|
if err := config.DB.Where("IDUsers = ?", c.Param("pacient_id")).Where("IDCourse = ?", c.Param("course_id")).First(&user_course).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Where("id = ?", c.Param("exercise_id")).First(&model)
|
|
config.DB.Where("IDCourse = ?", c.Param("course_id")).Where("IDExercise = ?", c.Param("exercise_id")).First(&course_exercise)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"ID": model.ID,
|
|
"Title": model.Title,
|
|
"Desc": model.Desc,
|
|
"URLFileImg": model.URLFileImg,
|
|
"URLFile": model.URLFile,
|
|
"Day": course_exercise.Day,
|
|
"Position": course_exercise.Position,
|
|
"Time": course_exercise.Time,
|
|
})
|
|
|
|
}
|
|
func ApiPacientCourseView(c *gin.Context) { // Get model if exist
|
|
|
|
var user_course Model.UserCourseModel
|
|
var model Model.CourseModel
|
|
var model_pacients Model.UserModel
|
|
|
|
claims, exists := c.Get("claims")
|
|
|
|
if !exists {
|
|
c.IndentedJSON(http.StatusOK, gin.H{"redirect": "redirect_auth_login"})
|
|
c.Abort()
|
|
}
|
|
|
|
userClaims := claims.(*jwt.MapClaims)
|
|
|
|
user_name := (*userClaims)["user_name"].(string)
|
|
|
|
//Gorm_SQL
|
|
if err := config.DB.Where("IDUsers = ?", c.Param("pacient_id")).Where("IDCourse = ?", c.Param("course_id")).First(&user_course).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Where("id = ?", c.Param("course_id")).Preload("Exercises").First(&model)
|
|
config.DB.Where("id = ?", c.Param("pacient_id")).Preload("Courses", "id = ?", c.Param("course_id")).Preload("ExerciseCourses", "IDUser = ?", c.Param("pacient_id")).First(&model_pacients)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"ID": model.ID,
|
|
"Title": model.Title,
|
|
"Desc": model.Desc,
|
|
"URLFileImg": model.URLFileImg,
|
|
"CourseExercises": model.Exercises,
|
|
"CoursePacient": model_pacients,
|
|
})
|
|
|
|
}
|
|
func ApiPacientCourse(c *gin.Context) {
|
|
|
|
var model_courses []Model.UserModel
|
|
|
|
claims, exists := c.Get("claims")
|
|
|
|
if !exists {
|
|
c.IndentedJSON(http.StatusOK, gin.H{"redirect": "redirect_auth_login"})
|
|
c.Abort()
|
|
}
|
|
|
|
userClaims := claims.(*jwt.MapClaims)
|
|
|
|
user_name := (*userClaims)["user_name"].(string)
|
|
|
|
config.DB.Preload("Courses").Find(&model_courses)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"Courses": model_courses,
|
|
})
|
|
|
|
}
|
|
func ApiViewPacient(c *gin.Context) {
|
|
|
|
claims, exists := c.Get("claims")
|
|
|
|
if !exists {
|
|
c.IndentedJSON(http.StatusOK, gin.H{"redirect": "redirect_auth_login"})
|
|
c.Abort()
|
|
}
|
|
|
|
userClaims := claims.(*jwt.MapClaims)
|
|
|
|
user_name := (*userClaims)["user_name"].(string)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"session_name": user_name,
|
|
})
|
|
|
|
// DashboardAdmin.vue
|
|
|
|
}
|