diff --git a/api/people/controllers.go b/api/people/controllers.go index 0e89a417..a1a9a349 100644 --- a/api/people/controllers.go +++ b/api/people/controllers.go @@ -174,3 +174,30 @@ func DeletePerson(c *gin.Context) { }) pkg.Log.SuccessCtx(c) } + +func GetAllStudents(c *gin.Context) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + conn, err := cmd.DBPool.Acquire(ctx) + if pkg.HandleDbAcquireErr(c, err, "STUDENTS") { + return + } + defer conn.Release() + + q := db.New() + students, err := q.GetAllStudents(ctx, conn) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "Oops! Something happened. Please try again later", + }) + pkg.Log.ErrorCtx(c, "[STUDENTS-ERROR]: Failed to fetch students", err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "message": "Students list fetched successfully", + "students": students, + }) + pkg.Log.SuccessCtx(c) +} diff --git a/api/people/routes.go b/api/people/routes.go index e506f52e..5dd45bbd 100644 --- a/api/people/routes.go +++ b/api/people/routes.go @@ -6,8 +6,12 @@ import ( ) func PeopleRoutes(r *gin.RouterGroup) { + // Digniatries r.GET("/", mw.Auth, mw.CheckAdmin, FetchAllPeople) r.POST("/", mw.Auth, mw.CheckAdmin, AddNewPerson) r.PUT("/:personId", mw.Auth, mw.CheckAdmin, UpdatePersonDetails) r.DELETE("/:personId", mw.Auth, mw.CheckAdmin, DeletePerson) + + // Student + r.GET("/students", mw.Auth, mw.CheckAdmin, GetAllStudents) } diff --git a/db/gen/students-for-admin-panel.sql.go b/db/gen/students-for-admin-panel.sql.go new file mode 100644 index 00000000..da9ce418 --- /dev/null +++ b/db/gen/students-for-admin-panel.sql.go @@ -0,0 +1,52 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 +// source: students-for-admin-panel.sql + +package db + +import ( + "context" +) + +const getAllStudents = `-- name: GetAllStudents :many +SELECT + name AS student_name, + email, + phone_number, + college_name AS college +FROM student +ORDER BY student_name ASC +` + +type GetAllStudentsRow struct { + StudentName string `json:"student_name"` + Email string `json:"email"` + PhoneNumber string `json:"phone_number"` + College string `json:"college"` +} + +func (q *Queries) GetAllStudents(ctx context.Context, db DBTX) ([]GetAllStudentsRow, error) { + rows, err := db.Query(ctx, getAllStudents) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetAllStudentsRow + for rows.Next() { + var i GetAllStudentsRow + if err := rows.Scan( + &i.StudentName, + &i.Email, + &i.PhoneNumber, + &i.College, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/queries/students-for-admin-panel.sql b/db/queries/students-for-admin-panel.sql new file mode 100644 index 00000000..8123c61b --- /dev/null +++ b/db/queries/students-for-admin-panel.sql @@ -0,0 +1,8 @@ +-- name: GetAllStudents :many +SELECT + name AS student_name, + email, + phone_number, + college_name AS college +FROM student +ORDER BY student_name ASC;