-
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.
add user handler, user view fix small mistake
- Loading branch information
1 parent
fe45470
commit 1378419
Showing
9 changed files
with
127 additions
and
37 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
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
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 |
---|---|---|
@@ -1,31 +1,106 @@ | ||
package api | ||
|
||
import ( | ||
"crypto/sha1" | ||
"ctf01d/lib/models" | ||
"ctf01d/lib/repository" | ||
"ctf01d/lib/view" | ||
"database/sql" | ||
"encoding/hex" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type RequestUser struct { | ||
Username string `json:"user_name"` | ||
Role string `json:"role"` | ||
AvatarUrl string `json:"avatar_url"` | ||
Status string `json:"status"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func CreateUserHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
w.WriteHeader(http.StatusOK) | ||
var user RequestUser | ||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { | ||
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) | ||
return | ||
} | ||
userRepo := repository.NewUserRepository(db) | ||
newUser := &models.User{ | ||
Username: user.Username, | ||
Role: user.Role, | ||
Status: user.Status, | ||
PasswordHash: HashPassword(user.Password), | ||
} | ||
if err := userRepo.Create(r.Context(), newUser); err != nil { | ||
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create user: " + err.Error()}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, map[string]string{"data": "User created successfully"}) | ||
} | ||
|
||
// ей тут не место, вынести в - tool, добавить соль в конфиг и солить пароли | ||
func HashPassword(s string) string { | ||
h := sha1.New() | ||
h.Write([]byte(s)) | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
func DeleteUserHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
w.WriteHeader(http.StatusOK) | ||
vars := mux.Vars(r) | ||
id := vars["id"] | ||
userRepo := repository.NewUserRepository(db) | ||
if err := userRepo.Delete(r.Context(), id); err != nil { | ||
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete user"}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, map[string]string{"data": "User deleted successfully"}) | ||
} | ||
|
||
func GetUserByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
w.WriteHeader(http.StatusOK) | ||
vars := mux.Vars(r) | ||
id := vars["id"] | ||
userRepo := repository.NewUserRepository(db) | ||
user, err := userRepo.GetById(r.Context(), id) | ||
if err != nil { | ||
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch user"}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, view.NewUserFromModel(user)) | ||
} | ||
|
||
func ListUsersHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
w.WriteHeader(http.StatusOK) | ||
userRepo := repository.NewUserRepository(db) | ||
users, err := userRepo.List(r.Context()) | ||
if err != nil { | ||
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, view.NewUsersFromModels(users)) | ||
} | ||
|
||
func UpdateUserHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
w.WriteHeader(http.StatusOK) | ||
// fixme update не проверяет есть ли запись в бд | ||
var user RequestUser | ||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { | ||
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"}) | ||
return | ||
} | ||
userRepo := repository.NewUserRepository(db) | ||
updateUser := &models.User{ | ||
Username: user.Username, | ||
Role: user.Role, | ||
Status: user.Status, | ||
} | ||
vars := mux.Vars(r) | ||
id := vars["id"] | ||
updateUser.Id = id | ||
err := userRepo.Update(r.Context(), updateUser) | ||
if err != nil { | ||
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, map[string]string{"data": "User updated successfully"}) | ||
} |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package models | ||
|
||
type User struct { | ||
Id string `db:"id"` | ||
Username string `db:"username"` | ||
Role string `db:"role"` | ||
AvatarUrl string `db:"avatar_url"` | ||
Status string `db:"status"` | ||
Id string `db:"id"` | ||
Username string `db:"user_name"` | ||
Role string `db:"role"` | ||
AvatarUrl string `db:"avatar_url"` | ||
Status string `db:"status"` | ||
PasswordHash string `db:"password_hash"` | ||
} |
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 |
---|---|---|
@@ -1,9 +1,29 @@ | ||
package view | ||
|
||
import "ctf01d/lib/models" | ||
|
||
type User struct { | ||
Id string `json:"id,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
Username string `json:"user_name,omitempty"` | ||
Role string `json:"role,omitempty"` | ||
AvatarUrl string `json:"avatar_url,omitempty"` | ||
Status string `json:"status,omitempty"` | ||
} | ||
|
||
func NewUserFromModel(u *models.User) *User { | ||
return &User{ | ||
Id: u.Id, | ||
Username: u.Username, | ||
Role: u.Role, | ||
AvatarUrl: u.AvatarUrl, | ||
Status: u.Status, | ||
} | ||
} | ||
|
||
func NewUsersFromModels(ms []*models.User) []*User { | ||
var users []*User | ||
for _, m := range ms { | ||
users = append(users, NewUserFromModel(m)) | ||
} | ||
return users | ||
} |