Skip to content

Commit

Permalink
Merge pull request #30 from prateek-srivastava001/master
Browse files Browse the repository at this point in the history
feat: added admin routes for users
  • Loading branch information
Mr-Emerald-Wolf authored Mar 14, 2024
2 parents 2afa417 + 469e2de commit 77a19eb
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 0 deletions.
174 changes: 174 additions & 0 deletions internal/controllers/admin_user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package controllers

import (
"database/sql"
"errors"
"net/http"

"github.com/labstack/echo/v4"

"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
admin "github.com/CodeChefVIT/devsoc-backend-24/internal/services/admin"
services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/user"
)

func GetAllUsers(ctx echo.Context) error {
users, err := admin.GetAllUsers()
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to fetch users",
"error": err.Error(),
})
}

return ctx.JSON(http.StatusOK, map[string]interface{}{
"message": "Users fetched successfully",
"users": users,
})
}

func GetAllVitians(ctx echo.Context) error {
users, err := admin.GetAllVitians()
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to fetch users",
"error": err.Error(),
})
}

return ctx.JSON(http.StatusOK, map[string]interface{}{
"message": "Users fetched successfully",
"users": users,
})
}

func GetAllFemales(ctx echo.Context) error {
users, err := admin.GetAllFemales()
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to fetch users",
"error": err.Error(),
})
}

return ctx.JSON(http.StatusOK, map[string]interface{}{
"message": "Users fetched successfully",
"users": users,
})
}

func GetUserByEmail(ctx echo.Context) error {
email := ctx.Param("email")
user, err := services.FindUserByEmail(email)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"message": "User does not exist",
"status": "fail",
})
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "error",
})
}
return ctx.JSON(http.StatusOK, map[string]interface{}{
"message": "User fetched successfully",
"user": user,
})

}

func BanUser(ctx echo.Context) error {
var payload models.BanUser

if err := ctx.Bind(&payload); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

if err := ctx.Validate(&payload); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

user, err := services.FindUserByEmail(payload.Email)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"message": "User does not exist",
"status": "fail",
})
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "error",
})
}

user.IsBanned = true

err = services.UpdateUser(&user.User)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

return ctx.JSON(http.StatusOK, map[string]string{
"message": "User banned",
"status": "success",
})
}

func UnbanUser(ctx echo.Context) error {
var payload models.BanUser

if err := ctx.Bind(&payload); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

if err := ctx.Validate(&payload); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

user, err := services.FindUserByEmail(payload.Email)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"message": "User does not exist",
"status": "fail",
})
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "error",
})
}

user.IsBanned = false

err = services.UpdateUser(&user.User)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "fail",
})
}

return ctx.JSON(http.StatusOK, map[string]string{
"message": "User unbanned",
"status": "success",
})
}
4 changes: 4 additions & 0 deletions internal/models/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type ForgotPasswordRequest struct {
Email string `json:"email" validate:"required,email"`
}

type BanUser struct {
Email string `json:"email" validate:"required,email"`
}

type ResetPasswordRequest struct {
Email string `json:"email" validate:"required,email"`
OTP string `json:"otp" validate:"required,min=6,max=6"`
Expand Down
7 changes: 7 additions & 0 deletions internal/routes/admin_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ func AdminRoutes(incomingRoutes *echo.Echo) {
// admin.Use(middleware.AuthUser)
admin.Use(middleware.CheckAdmin)

admin.GET("/users", controllers.GetAllUsers)
admin.GET("/user/:email", controllers.GetUserByEmail)
admin.POST("/user/ban", controllers.BanUser)
admin.POST("/user/unban", controllers.UnbanUser)
admin.GET("/vitians", controllers.GetAllVitians)
admin.GET("/females", controllers.GetAllFemales)

admin.GET("/team/all", controllers.GetTeams)
admin.GET("/team/:id", controllers.GetTeamsByID)
admin.GET("/team/project/:id", controllers.GetProjectByTeamID)
Expand Down
113 changes: 113 additions & 0 deletions internal/services/admin/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package services

import (
"github.com/google/uuid"

"github.com/CodeChefVIT/devsoc-backend-24/internal/database"
"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
)

func GetAllUsers() ([]*models.User, error) {
rows, err := database.DB.Query("SELECT id, email, first_name, last_name, reg_no, password, phone, college, gender, role, is_banned, is_added, is_vitian, is_verified, is_profile_complete, is_leader, team_id, city, state, country FROM users")
if err != nil {
return nil, err
}
defer rows.Close()

var users []*models.User
for rows.Next() {
var user models.User
var teamID uuid.NullUUID

err := rows.Scan(&user.ID, &user.Email, &user.FirstName, &user.LastName, &user.RegNo, &user.Password, &user.Phone,
&user.College, &user.Gender, &user.Role,
&user.IsBanned, &user.IsAdded, &user.IsVitian, &user.IsVerified, &user.IsProfileComplete, &user.IsLeader, &teamID, &user.City, &user.State, &user.Country)
if err != nil {
return nil, err
}

if teamID.Valid {
user.TeamID = teamID.UUID
} else {
user.TeamID = uuid.Nil
}

users = append(users, &user)
}

if err := rows.Err(); err != nil {
return nil, err
}

return users, nil
}

func GetAllVitians() ([]*models.User, error) {
rows, err := database.DB.Query("SELECT id, email, first_name, last_name, reg_no, password, phone, college, gender, role, is_banned, is_added, is_vitian, is_verified, is_profile_complete, is_leader, team_id, city, state, country FROM users where is_vitian=true")
if err != nil {
return nil, err
}
defer rows.Close()

var users []*models.User
for rows.Next() {
var user models.User
var teamID uuid.NullUUID

err := rows.Scan(&user.ID, &user.Email, &user.FirstName, &user.LastName, &user.RegNo, &user.Password, &user.Phone,
&user.College, &user.Gender, &user.Role,
&user.IsBanned, &user.IsAdded, &user.IsVitian, &user.IsVerified, &user.IsProfileComplete, &user.IsLeader, &teamID, &user.City, &user.State, &user.Country)
if err != nil {
return nil, err
}

if teamID.Valid {
user.TeamID = teamID.UUID
} else {
user.TeamID = uuid.Nil
}

users = append(users, &user)
}

if err := rows.Err(); err != nil {
return nil, err
}

return users, nil
}

func GetAllFemales() ([]*models.User, error) {
rows, err := database.DB.Query("SELECT id, email, first_name, last_name, reg_no, password, phone, college, gender, role, is_banned, is_added, is_vitian, is_verified, is_profile_complete, is_leader, team_id, city, state, country FROM users where gender=female")
if err != nil {
return nil, err
}
defer rows.Close()

var users []*models.User
for rows.Next() {
var user models.User
var teamID uuid.NullUUID

err := rows.Scan(&user.ID, &user.Email, &user.FirstName, &user.LastName, &user.RegNo, &user.Password, &user.Phone,
&user.College, &user.Gender, &user.Role,
&user.IsBanned, &user.IsAdded, &user.IsVitian, &user.IsVerified, &user.IsProfileComplete, &user.IsLeader, &teamID, &user.City, &user.State, &user.Country)
if err != nil {
return nil, err
}

if teamID.Valid {
user.TeamID = teamID.UUID
} else {
user.TeamID = uuid.Nil
}

users = append(users, &user)
}

if err := rows.Err(); err != nil {
return nil, err
}

return users, nil
}

0 comments on commit 77a19eb

Please sign in to comment.