parent
436765613f
commit
6659c0b28f
1 changed files with 9 additions and 13 deletions
|
@ -17,7 +17,7 @@ type App struct {
|
|||
}
|
||||
|
||||
type urlEntry struct {
|
||||
URL string `gorm:"unique" json:"url"`
|
||||
URL string `json:"url"`
|
||||
Alias string `gorm:"unique" json:"alias"`
|
||||
}
|
||||
|
||||
|
@ -67,18 +67,14 @@ func (a *App) CreateShortURL(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, "Invalid Alias", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Check if URL entry already exists
|
||||
existingURL := &urlEntry{}
|
||||
a.DB.Where("url = ?", u.URL).Find(existingURL)
|
||||
if existingURL.URL != "" {
|
||||
u.Alias = existingURL.Alias
|
||||
} else {
|
||||
// Verify alias is unique
|
||||
for u.Alias == "" || !a.DB.Where("alias = ?", u.Alias).First(existingURL).RecordNotFound() {
|
||||
u.Alias = utils.RandString(6)
|
||||
}
|
||||
a.DB.Create(u)
|
||||
}
|
||||
|
||||
// Verify alias is unique
|
||||
existingURL := &urlEntry{}
|
||||
for u.Alias == "" || !a.DB.Where("alias = ?", u.Alias).First(existingURL).RecordNotFound() {
|
||||
u.Alias = utils.RandString(6)
|
||||
}
|
||||
a.DB.Create(u)
|
||||
|
||||
// Write HTTP Response
|
||||
shortlink := r.Host + "/s/" + u.Alias
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
|
Loading…
Reference in a new issue