-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from prateek-srivastava001/master
feat: added admin routes for users
- Loading branch information
Showing
4 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |