Function upsert_user

Source
pub async fn upsert_user(
    __arg0: State<AppState>,
    __arg1: Json<UpsertUserRequest>,
) -> impl IntoResponse
Expand description

POST /api/test/users

Create-or-update (idempotent) a user.

  • If the username exists, updates email, password, and admin.
  • If it does not exist, creates a new user.
  • This endpoint is for test environments only.

§Request body

{
  "username": "student42",
  "email": "[email protected]",
  "password": "secret",
  "admin": false
}

§Field notes

  • admin is optional and defaults to false.
  • password is stored as a hashed value.

§Response examples

§201 Created (new user)

{
  "success": true,
  "data": {
    "id": 1,
    "username": "student42",
    "email": "[email protected]",
    "admin": false
  },
  "message": "User created"
}

§200 OK (updated existing user)

{
  "success": true,
  "data": {
    "id": 1,
    "username": "student42",
    "email": "[email protected]",
    "admin": true
  },
  "message": "User updated"
}

§400 Bad Request (validation error)

{
  "success": false,
  "data": null,
  "message": "Validation failed: email: must be a valid email"
}

§409 Conflict (username/email already exists)

{
  "success": false,
  "data": null,
  "message": "A user with this username or email already exists"
}

§500 Internal Server Error

{
  "success": false,
  "data": null,
  "message": "Database error: <details>"
}