Skip to content

Commit

Permalink
✨ Setting custom short code
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
  • Loading branch information
mhkarimi1383 committed Oct 18, 2024
1 parent 03544a7 commit 1125739
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func init() {

func start(_ *cobra.Command, _ []string) {
log.Logger.Info("Setting and Validating configuration parameters")

cfg.BaseURI = strings.TrimSuffix(cfg.BaseURI, "/")
cfg.RootRedirect = strings.ReplaceAll(cfg.RootRedirect, "/BASE_URI/", cfg.BaseURI+"/")

if err := configuration.SetConfig(&cfg); err != nil {
if vErrs, ok := err.(validator.ValidationErrors); ok {
for _, vErr := range vErrs {
Expand All @@ -113,10 +117,6 @@ func start(_ *cobra.Command, _ []string) {
}
log.Logger.Panic(err.Error())
}

cfg.BaseURI = strings.TrimSuffix(cfg.BaseURI, "/")
cfg.RootRedirect = strings.ReplaceAll(cfg.RootRedirect, "/BASE_URI/", cfg.BaseURI+"/")

log.Logger.Info("Initializing database engine")
database.Init()

Expand Down
9 changes: 6 additions & 3 deletions internal/controller/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ func CreateUrl(r *requestschemas.CreateURL, creator databasemodels.User) (string
if _, err := database.Engine.Insert(&u); err != nil {
return "", err
}
shortant := shortcode.Generate(u.Id, u.CreatedAt)
u.ShortCode = shortant
if len(r.ShortCode) > 0 {
u.ShortCode = r.ShortCode
} else {
u.ShortCode = shortcode.Generate(u.Id, u.CreatedAt)
}
if _, err := database.Engine.Update(&u); err != nil {
return "", err
}
return shortant, nil
return u.ShortCode, nil
}

func DeleteUrl(id int64, user databasemodels.User) error {
Expand Down
5 changes: 3 additions & 2 deletions types/request_schemas/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type CreateEntity struct {
}

type CreateURL struct {
FullUrl string `validate:"required,http_url"`
Entity int64
FullUrl string `validate:"required,http_url"`
Entity int64
ShortCode string `validate:"min=0,max=10"`
}
1 change: 1 addition & 0 deletions ui/src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface url {
export interface urlCreateRequest {
FullUrl: string;
Entity: number;
ShortCode?: string;
}

export interface urlCreateResponse {
Expand Down
7 changes: 7 additions & 0 deletions ui/src/views/URLView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ loadListUrls();
<a-form-item>
<a-input v-model:value="formState.FullUrl" placeholder="Long URL"> </a-input>
</a-form-item>
<a-form-item>
<a-input
v-model:value="formState.ShortCode"
placeholder="Custom Short Code (optional)"
>
</a-input>
</a-form-item>
<a-form-item>
<a-button type="primary" html-type="submit" :disabled="formState.FullUrl === ''">
Create
Expand Down

0 comments on commit 1125739

Please sign in to comment.