mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 20:12:25 +00:00
Compare commits
4 Commits
4a58845211
...
975c52b58e
| Author | SHA1 | Date | |
|---|---|---|---|
| 975c52b58e | |||
| 4e75d48f1d | |||
| 65af07fffa | |||
| 1252634420 |
@@ -36,19 +36,28 @@ end
|
|||||||
local hashPass = crypt.generate(params.password, crypt.DefaultCost)
|
local hashPass = crypt.generate(params.password, crypt.DefaultCost)
|
||||||
local unitID = string.sub(sha256.hash(session.__seed), 1, 16)
|
local unitID = string.sub(sha256.hash(session.__seed), 1, 16)
|
||||||
|
|
||||||
-- First db query: check if username or email already exists
|
-- First db query: check if username or email already exists among active users
|
||||||
local existing, err = db:query("SELECT 1 FROM units WHERE email = ? OR username = ? LIMIT 1", {
|
local existing, err = db:query([[
|
||||||
|
SELECT 1
|
||||||
|
FROM units
|
||||||
|
WHERE (email = ? OR username = ?)
|
||||||
|
AND entry_status != 'deleted'
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
LIMIT 1
|
||||||
|
]], {
|
||||||
params.email,
|
params.email,
|
||||||
params.username
|
params.username
|
||||||
})
|
})
|
||||||
|
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
log.error("Email check failed: "..tostring(err))
|
log.error("Email check failed: "..tostring(err))
|
||||||
return session.response.send_error()
|
close_db()
|
||||||
|
session.response.send_error()
|
||||||
end
|
end
|
||||||
|
|
||||||
if existing and #existing > 0 then
|
if existing and #existing > 0 then
|
||||||
return session.response.send_error(-32101, "Unit already exists")
|
close_db()
|
||||||
|
session.response.send_error(-32101, "Unit is already exists")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Second db query: insert new unit
|
-- Second db query: insert new unit
|
||||||
@@ -63,15 +72,16 @@ local ctx, err = db:exec(
|
|||||||
)
|
)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
log.error("Insert failed: "..tostring(err))
|
log.error("Insert failed: "..tostring(err))
|
||||||
return session.response.send_error()
|
close_db()
|
||||||
|
session.response.send_error("Failed to create unit")
|
||||||
end
|
end
|
||||||
|
|
||||||
local res, err = ctx:wait()
|
local res, err = ctx:wait()
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
log.error("Insert confirmation failed: "..tostring(err))
|
log.error("Insert confirmation failed: "..tostring(err))
|
||||||
return session.response.send_error()
|
close_db()
|
||||||
|
session.response.send_error("Failed to create unit")
|
||||||
end
|
end
|
||||||
|
|
||||||
session.response.send({message = "Unit created successfully", unit_id = unitID})
|
|
||||||
|
|
||||||
close_db()
|
close_db()
|
||||||
|
session.response.send({message = "Unit created successfully", unit_id = unitID})
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
-- File com/Unit/Delete.lua
|
||||||
|
--
|
||||||
|
-- Created at 2025-05-10 19:18
|
||||||
|
--
|
||||||
|
-- Updated at -
|
||||||
|
|
||||||
|
local log = require("internal.log")
|
||||||
|
local db = require("internal.database.sqlite").connect("db/unit.db", {log = true})
|
||||||
|
local session = require("internal.session")
|
||||||
|
|
||||||
|
local common = require("com/Unit/_common")
|
||||||
|
|
||||||
|
-- Preparing for first db query
|
||||||
|
local function close_db()
|
||||||
|
if db then
|
||||||
|
log.debug("Closing DB connection")
|
||||||
|
db:close()
|
||||||
|
db = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local params = session.request.params.get()
|
||||||
|
|
||||||
|
local ok, mp = common.CheckMissingElement({"user_id"}, params)
|
||||||
|
if not ok then
|
||||||
|
close_db()
|
||||||
|
session.response.send_error(-32602, "Missing params", mp)
|
||||||
|
end
|
||||||
|
|
||||||
|
local existing, err = db:query([[
|
||||||
|
SELECT 1
|
||||||
|
FROM units
|
||||||
|
WHERE user_id = ?
|
||||||
|
AND entry_status != 'deleted'
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
LIMIT 1
|
||||||
|
]], {
|
||||||
|
params.user_id
|
||||||
|
})
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Email check failed: "..tostring(err))
|
||||||
|
close_db()
|
||||||
|
session.response.send_error()
|
||||||
|
end
|
||||||
|
|
||||||
|
if existing and #existing == 0 then
|
||||||
|
close_db()
|
||||||
|
session.response.send_error(-32102, "Unit is not exists")
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx, err = db:exec(
|
||||||
|
[[
|
||||||
|
UPDATE units
|
||||||
|
SET entry_status = 'deleted',
|
||||||
|
deleted_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE user_id = ? AND deleted_at is NULL
|
||||||
|
]],
|
||||||
|
{ params.user_id }
|
||||||
|
)
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Soft delete failed: " .. tostring(err))
|
||||||
|
close_db()
|
||||||
|
session.response.send_error("Failed to delete unit")
|
||||||
|
end
|
||||||
|
|
||||||
|
local res, err = ctx:wait()
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Soft delete confirmation failed: " .. tostring(err))
|
||||||
|
close_db()
|
||||||
|
session.response.send_error("Failed to delete unit")
|
||||||
|
end
|
||||||
|
|
||||||
|
close_db()
|
||||||
|
session.response.send({message = "Unit deleted successfully", unit_id = params.user_id})
|
||||||
@@ -29,6 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var NodeApp = app.New()
|
var NodeApp = app.New()
|
||||||
|
var AllowedCmdPattern = `^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*$`
|
||||||
|
|
||||||
func Run(cmd *cobra.Command, args []string) {
|
func Run(cmd *cobra.Command, args []string) {
|
||||||
NodeApp.InitialHooks(
|
NodeApp.InitialHooks(
|
||||||
@@ -60,7 +61,7 @@ func RunHook(ctx context.Context, cs *corestate.CoreState, x *app.AppX) error {
|
|||||||
serverv1 := sv1.InitV1Server(&sv1.HandlerV1InitStruct{
|
serverv1 := sv1.InitV1Server(&sv1.HandlerV1InitStruct{
|
||||||
X: x,
|
X: x,
|
||||||
CS: cs,
|
CS: cs,
|
||||||
AllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9]+(>[a-zA-Z0-9]+)*$`),
|
AllowedCmd: regexp.MustCompile(AllowedCmdPattern),
|
||||||
Ver: "v1",
|
Ver: "v1",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ import (
|
|||||||
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
|
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var RPCMethodSeparator = "."
|
||||||
|
|
||||||
func (h *HandlerV1) resolveMethodPath(method string) (string, error) {
|
func (h *HandlerV1) resolveMethodPath(method string) (string, error) {
|
||||||
if !h.allowedCmd.MatchString(method) {
|
if !h.allowedCmd.MatchString(method) {
|
||||||
return "", errors.New(rpc.ErrInvalidMethodFormatS)
|
return "", errors.New(rpc.ErrInvalidMethodFormatS)
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(method, ">")
|
parts := strings.Split(method, RPCMethodSeparator)
|
||||||
relPath := filepath.Join(parts...) + ".lua"
|
relPath := filepath.Join(parts...) + ".lua"
|
||||||
fullPath := filepath.Join(*h.x.Config.Conf.Node.ComDir, relPath)
|
fullPath := filepath.Join(*h.x.Config.Conf.Node.ComDir, relPath)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user