drogonframework / drogonframework/drogon

[ feature propose ] - genRandomNumber & genLetterCaseString

Open
#1,685 0 comments 0 reactions 1 assignee Claimed by @marty1885 View on GitHub
Dominant language
C++
Stars
14.3k
Forks
1.4k
Avg merge
1d 13h
Merged PRs (30d)
14

Description

Ladies & gentlemen, I want to propose a new feature from [this branch](https://github.com/prothegee/drogon/tree/feature/generate_random_number-and-generate_random_letter_case_string)


---

### case, motivation & usage

In some c++ string generator, some return value as string always return with __uppercase letter/ lowercase letter__

either from *drogon::utils* or from *another library* for whatever the case developer want to use.


In some of my backend cases, I want to utilize string value where we can store it to *somewhere (e.g. database value, file, etc.)*

also use it as [end user media/upload path](#end-user-media-path---code).


note:
- drogon::utils::genLetterCaseString(origin, 2) is invovled with:
- drogon::utils::genRandomNumber(0, 1)

---

#### `common usage - code`

```c++
std::string
origin = (utils::getUuid() + "-" + utils::getUuid()),
lowerCase = utils::genLetterCaseString(origin, 0),
upperCase = utils::genLetterCaseString(lowerCase, 1),
mixedCase = utils::genLetterCaseString(origin, 2)
;

std::cout << "origin : " << origin << "\n";
std::cout << "lowerCase: " << lowerCase << "\n";
std::cout << "upperCase: " << upperCase << "\n";
std::cout << "mixedCase: " << mixedCase << "\n";
```

#### `common usage - result`
![image](https://github.com/drogonframework/drogon/assets/72252553/84602eb3-f380-400c-adf6-db9487816c98)


#### `end user media path - code`

end user username/email mostly unique,

when backend has the ability where end user able to upload a file,

we can store it as string/directory such as:
```
domain.tld
|
|__/audio/** // public audio file access
|
|
|__/img/** // public images file access
|
|
|__/upload
| |
| |__/user
| | |
| | |__/username1
| | |__/username...
| |
| |__/organization
| |
| |__/organizationA
| |__/organization...
|
|...
```

if we want to access __*username1*__ profile-picture,

this would be `domain.tld/upload/user/username1/{filename}.png`

those paths way to obvious, but we can also use hashing to generate those paths.

```c++
// NOTE:
// - this is body function
// - logic may applied after/before end user email has been verified & activated, it's all up to you

// will return "username1"
const std::string username = usernameParam;

// generate hash from end user email
// you can use any algorithm with your own taste
// also asume will return "05C0042DF9A7793B7BDE3AB9724C08CF37398652"
const std::string userHashPath = SomeHashDigestFunction(emailParam);

// something to validate
bool isValid{};

// another logic where isValid

if (!isValid)
{
// do something where it's not valid
}
else
{
// email has been verified, let's do some stuff

// will return 05c0042df9a7793b7bde3ab9724c08cf37398652
const std::string userHashPathNormalized = drogon::utils::genLetterCaseString(userHashPath, 0);

const std::string userUploadPath =
drogon::app().getUploadPath() + "/user/" + userHashPathNormalized

// create end user path access
drogon::utils::createPath(userUploadPath);

// anything else?
}
```

later on when we want to access *username1* profile picture

that would be `domain.tld/upload/user/05c0042df9a7793b7bde3ab9724c08cf37398652/{filename}.png`

__*or do something else what suits you.*__


---

###### end of propose

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.