Skip to content

Commit

Permalink
docs: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
blooroot committed Dec 6, 2024
1 parent b4b48fa commit de3ab14
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/get_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func GetGallery(w http.ResponseWriter, r *http.Request, params httprouter.Params

type ResPayload []dbutils.StillerGallery

// Decodificando el token del header
user_token := r.Header.Get("token")
user_tk, token_decode_err := jwt.Decode(user_token)
if loggers.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
Expand All @@ -31,13 +32,15 @@ func GetGallery(w http.ResponseWriter, r *http.Request, params httprouter.Params

user_id := user_tk.UserId

// Crear conexión a la base de datos
new_dbconn, dbconn_err := dbutils.NewConn()
if loggers.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
return
}

defer dbutils.CloseConn(new_dbconn)

// Construir consulta para obtener galerías
get_galleries := sqlf.
Select("*").
From("gallery").
Expand Down Expand Up @@ -76,6 +79,7 @@ func GetGallery(w http.ResponseWriter, r *http.Request, params httprouter.Params
return
}

// Enviar respuesta en formato JSON
writing_err := jsonexp.MarshalWrite(w, galleries, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(writing_err, "", http.StatusInternalServerError, &w) {
return
Expand Down
10 changes: 9 additions & 1 deletion stiller-backend/internal/handlers/post_auth.newuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,21 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
req_payload := ReqPayload{}
res_payload := ResPayload{}

// Parseo del cuerpo JSON
unmarshal_err := jsonexp.UnmarshalRead(r.Body, &req_payload, jsonexp.DefaultOptionsV2())
if loggers.RequestLog(unmarshal_err, "", http.StatusBadRequest, &w) {
return
}

// Creación de conexión a la base de datos
new_dbconn, dbconn_err := dbutils.NewConn()
if loggers.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
return
}

defer dbutils.CloseConn(new_dbconn)

// Registrar la función personalizada de SQLite
create_fn_err := new_dbconn.CreateFunction("newuser", &sqlite.FunctionImpl{
NArgs: 5,
Deterministic: true,
Expand All @@ -147,6 +150,7 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
UserId: -1,
}

// Llamada a la función personalizada para crear un usuario
query_stmt := sqlf.Select(
"newuser(?, ?, ?, ?, ?)",
req_payload.TierId,
Expand Down Expand Up @@ -181,18 +185,21 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
return
}

// Creación del directorio del usuario
new_dir := strconv.Itoa(new_tk.UserId)
mkdir_err := os.MkdirAll(stiller.StillerConfig.FilesPath + new_dir, os.ModePerm)
if loggers.RequestLog(mkdir_err, "", http.StatusInternalServerError, &w) {
return
}

// Obtención de los datos del nuevo usuario
new_user, newuser_err := dbutils.GetUserById(new_tk.UserId, new_dbconn)
new_user.Bpasswd = ""
new_user.Bpasswd = "" // No devolver la contraseña hash
if loggers.RequestLog(newuser_err, "", http.StatusInternalServerError, &w) {
return
}

// Generación del token JWT
sign_encoded, sign_err := new_tk.Encode()
if loggers.RequestLog(sign_err, "", http.StatusInternalServerError, &w) {
return
Expand All @@ -201,6 +208,7 @@ func PostAuthNewuser(w http.ResponseWriter, r *http.Request, _ httprouter.Params
res_payload.UserData = new_user
res_payload.Token = string(sign_encoded)

// Respuesta al cliente
jsonexp.MarshalWrite(w, res_payload, jsonexp.DefaultOptionsV2())
}

Expand Down

0 comments on commit de3ab14

Please sign in to comment.