513 lines
12 KiB
Go
513 lines
12 KiB
Go
package Controllers
|
|
|
|
import (
|
|
"larago/app/Model"
|
|
"larago/config"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
)
|
|
|
|
func Doctor(router *gin.RouterGroup) {
|
|
|
|
router.GET("/api", ApiViewDoctor)
|
|
//Exercises
|
|
router.GET("/exercises", Exercises)
|
|
router.POST("/exercise/add", ExercisesAdd)
|
|
router.GET("/exercise/:id", ExercisesPrev)
|
|
router.PATCH("/exercise/:id", ExercisesEdit)
|
|
router.DELETE("/exercise/:id", ExercisesDelete)
|
|
//Courses
|
|
router.GET("/courses", Courses)
|
|
router.POST("/course/add", CoursesAdd)
|
|
router.GET("/course/:id", CourcesPrev)
|
|
router.PATCH("/course/:id", CourseEdit)
|
|
router.DELETE("/course/:id", CoursesDelete)
|
|
router.POST("/course-ex/:id/exercise-add", CoursesAddExerciseCourse)
|
|
router.DELETE("/course-ex/:course_id/:exercis_id/exercise-delete", CoursesDeleteExerciseCourse)
|
|
//Courses_Pacients
|
|
router.GET("/pacients", PacientsCourses)
|
|
router.GET("/pacient/:id", PacientPrev)
|
|
router.PATCH("/pacient/:id", PacientEdit)
|
|
router.POST("/pacient-cr/:id/course-add", PacientAddCourse)
|
|
router.DELETE("/pacient-cr/:pacient_id/:course_id/exercise-delete", PacientDeleteCourse)
|
|
|
|
}
|
|
|
|
type PacientCourseAddValidation struct {
|
|
IDCourse uint `form:"IDCourse" json:"IDCourse" binding:"required"`
|
|
}
|
|
type CoursesExerciseAddValidation struct {
|
|
Day int `form:"Day" json:"Day" binding:"required"`
|
|
Position int `form:"Position" json:"Position" binding:"required"`
|
|
Time string `form:"Time" json:"Time" binding:"required"`
|
|
IDExercise uint `form:"IDExercise" json:"IDExercise" binding:"required"`
|
|
}
|
|
type PacinetDoctorValidation struct {
|
|
UserDoctorID uint `form:"UserDoctorID" json:"UserDoctorID" binding:"required"`
|
|
}
|
|
type CoursesAddValidation struct {
|
|
Title string `form:"Title" json:"Title" binding:"required"`
|
|
Desc string `form:"Desc" json:"Desc" binding:"required"`
|
|
URLFileImg string `form:"URLFileImg" json:"URLFileImg"`
|
|
}
|
|
type ExercisesAddValidation struct {
|
|
Title string `form:"Title" json:"Title" binding:"required"`
|
|
Desc string `form:"Desc" json:"Desc" binding:"required"`
|
|
URLFile string `form:"URLFile" json:"URLFile" binding:"required"`
|
|
URLFileImg string `form:"URLFileImg" json:"URLFileImg"`
|
|
}
|
|
|
|
func PacientPrev(c *gin.Context) { // Get model if exist
|
|
|
|
var model 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("id = ?", c.Param("id")).Preload("Courses").First(&model).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"ID": model.ID,
|
|
"Name": model.Name,
|
|
"Email": model.Email,
|
|
"CoursePacient": model.Courses,
|
|
})
|
|
|
|
}
|
|
func Courses(c *gin.Context) {
|
|
|
|
var model_courses []Model.CourseModel
|
|
|
|
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.Find(&model_courses)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"Courses": model_courses,
|
|
})
|
|
|
|
}
|
|
func PacientsCourses(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 Exercises(c *gin.Context) {
|
|
|
|
var model_exercises []Model.ExerciseModel
|
|
|
|
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.Find(&model_exercises)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"Exercises": model_exercises,
|
|
})
|
|
|
|
}
|
|
|
|
func CoursesAdd(c *gin.Context) {
|
|
// Validate input
|
|
var input CoursesAddValidation
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
course_add := Model.CourseModel{
|
|
Title: input.Title,
|
|
Desc: input.Desc,
|
|
URLFileImg: input.URLFileImg,
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Save(&course_add)
|
|
|
|
c.IndentedJSON(http.StatusCreated, course_add)
|
|
|
|
}
|
|
func CoursesAddExerciseCourse(c *gin.Context) {
|
|
// Validate input
|
|
var input CoursesExerciseAddValidation
|
|
|
|
var model Model.CourseModel
|
|
|
|
//Gorm_SQL
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).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
|
|
}
|
|
|
|
course_exercise := Model.ExerciseCourseModel{
|
|
IDCourse: model.ID,
|
|
IDExercise: input.IDExercise,
|
|
Day: input.Day,
|
|
Position: input.Position,
|
|
Time: input.Time,
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Save(&course_exercise)
|
|
|
|
c.IndentedJSON(http.StatusCreated, course_exercise)
|
|
|
|
}
|
|
func PacientAddCourse(c *gin.Context) {
|
|
// Validate input
|
|
var input PacientCourseAddValidation
|
|
|
|
var model Model.UserModel
|
|
|
|
//Gorm_SQL
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).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
|
|
}
|
|
|
|
course_pacient := Model.UserCourseModel{
|
|
IDUsers: model.ID,
|
|
IDCourse: input.IDCourse,
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Save(&course_pacient)
|
|
|
|
c.IndentedJSON(http.StatusCreated, course_pacient)
|
|
|
|
}
|
|
func ExercisesAdd(c *gin.Context) {
|
|
// Validate input
|
|
var input ExercisesAddValidation
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
exercise_add := Model.ExerciseModel{
|
|
Title: input.Title,
|
|
Desc: input.Desc,
|
|
URLFile: input.URLFile,
|
|
URLFileImg: input.URLFileImg,
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Save(&exercise_add)
|
|
|
|
c.IndentedJSON(http.StatusCreated, exercise_add)
|
|
|
|
}
|
|
func ExercisesPrev(c *gin.Context) { // Get model if exist
|
|
|
|
var model Model.ExerciseModel
|
|
|
|
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("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"ID": model.ID,
|
|
"Title": model.Title,
|
|
"Desc": model.Desc,
|
|
"URLFile": model.URLFile,
|
|
"URLFileImg": model.URLFileImg,
|
|
})
|
|
|
|
}
|
|
func CourcesPrev(c *gin.Context) { // Get model if exist
|
|
|
|
var model Model.CourseModel
|
|
|
|
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("id = ?", c.Param("id")).Preload("Exercises").First(&model).Error; err != nil {
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
"SessionName": user_name,
|
|
"ID": model.ID,
|
|
"Title": model.Title,
|
|
"Desc": model.Desc,
|
|
"URLFileImg": model.URLFileImg,
|
|
"CourseExercises": model.Exercises,
|
|
})
|
|
|
|
}
|
|
func ExercisesEdit(c *gin.Context) {
|
|
|
|
//Gorm_SQL
|
|
var model Model.ExerciseModel
|
|
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
// Validate input
|
|
var input ExercisesAddValidation
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Model(&model).Select(
|
|
"title",
|
|
"desc",
|
|
"url_file",
|
|
"url_file_img",
|
|
).Updates(Model.ExerciseModel{
|
|
Title: input.Title,
|
|
Desc: input.Desc,
|
|
URLFile: input.URLFile,
|
|
URLFileImg: input.URLFileImg,
|
|
})
|
|
|
|
c.IndentedJSON(http.StatusOK, model)
|
|
|
|
}
|
|
func CourseEdit(c *gin.Context) {
|
|
|
|
//Gorm_SQL
|
|
var model Model.CourseModel
|
|
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
// Validate input
|
|
var input CoursesAddValidation
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Model(&model).Select(
|
|
"title",
|
|
"desc",
|
|
"url_file_img",
|
|
).Updates(Model.ExerciseModel{
|
|
Title: input.Title,
|
|
Desc: input.Desc,
|
|
URLFileImg: input.URLFileImg,
|
|
})
|
|
|
|
c.IndentedJSON(http.StatusOK, model)
|
|
|
|
}
|
|
func PacientEdit(c *gin.Context) {
|
|
|
|
//Gorm_SQL
|
|
var model Model.UserModel
|
|
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
// Validate input
|
|
var input PacinetDoctorValidation
|
|
|
|
if err := c.ShouldBind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
//Gorm_SQL
|
|
config.DB.Model(&model).Select(
|
|
"user_doctor_id",
|
|
).Updates(Model.UserModel{
|
|
UserDoctorID: input.UserDoctorID,
|
|
})
|
|
|
|
c.IndentedJSON(http.StatusOK, model)
|
|
|
|
}
|
|
func ExercisesDelete(c *gin.Context) {
|
|
|
|
var model_course_exercise Model.ExerciseCourseModel
|
|
|
|
if err := config.DB.Where("IDExercise = ?", c.Param("id")).First(&model_course_exercise).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model_course_exercise)
|
|
//Gorm_SQL
|
|
var model Model.ExerciseModel
|
|
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"data": true})
|
|
}
|
|
func CoursesDelete(c *gin.Context) {
|
|
|
|
var model_course_exercise Model.ExerciseCourseModel
|
|
|
|
if err := config.DB.Where("IDCourse = ?", c.Param("id")).First(&model_course_exercise).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model_course_exercise)
|
|
//Gorm_SQL
|
|
var model Model.CourseModel
|
|
|
|
if err := config.DB.Where("id = ?", c.Param("id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"data": true})
|
|
}
|
|
func CoursesDeleteExerciseCourse(c *gin.Context) {
|
|
|
|
//Gorm_SQL
|
|
var model Model.ExerciseCourseModel
|
|
|
|
if err := config.DB.Where("IDCourse = ?", c.Param("course_id")).Where("IDExercise = ?", c.Param("exercis_id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"data": true})
|
|
}
|
|
func PacientDeleteCourse(c *gin.Context) {
|
|
|
|
//Gorm_SQL
|
|
var model Model.UserCourseModel
|
|
|
|
if err := config.DB.Where("IDUsers = ?", c.Param("pacient_id")).Where("IDCourse = ?", c.Param("course_id")).First(&model).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
config.DB.Delete(&model)
|
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"data": true})
|
|
}
|
|
func ApiViewDoctor(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
|
|
|
|
}
|