Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL Create limit added #91

Merged
merged 16 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 111 additions & 94 deletions controllers/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,103 +13,105 @@ import (
)

func CreateTinyURL(ctx *gin.Context, db *bun.DB) {
var body models.Tinyurl

if err := ctx.BindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Invalid JSON format: " + err.Error(),
})
return
}

if body.OriginalUrl == "" {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Original URL is required",
})
return
}

var existingOriginalURL models.Tinyurl
if err := db.NewSelect().Model(&existingOriginalURL).Where("original_url = ?", body.OriginalUrl).Limit(1).Scan(ctx); err == nil {
ctx.JSON(http.StatusOK, dtos.URLCreationResponse{
Message: "Tiny URL already exists for the original URL",
ShortURL: existingOriginalURL.ShortUrl,
})
return
}

if body.ShortUrl != "" {
if len(body.ShortUrl) < 5 {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Custom short URL must be at least 5 characters long",
})
return
}

var existingURL models.Tinyurl
if err := db.NewSelect().Model(&existingURL).Where("short_url = ?", body.ShortUrl).Limit(1).Scan(ctx); err == nil {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Custom short URL already exists",
})
return
}
} else {
generatedShortURL := utils.GenerateMD5Hash(body.OriginalUrl)
var existingURL models.Tinyurl
if err := db.NewSelect().Model(&existingURL).Where("short_url = ?", generatedShortURL).Limit(1).Scan(ctx); err != nil {
body.ShortUrl = generatedShortURL
}
}

body.CreatedAt = time.Now().UTC()
if _, err := db.NewInsert().Model(&body).Exec(ctx); err != nil {
ctx.JSON(http.StatusInternalServerError, dtos.URLCreationResponse{
Message: "Failed to insert into the database: " + err.Error(),
})
return
}

ctx.JSON(http.StatusOK, dtos.URLCreationResponse{
Message: "Tiny URL created successfully",
ShortURL: body.ShortUrl,
})
var body models.Tinyurl

if err := ctx.BindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Invalid JSON format: " + err.Error(),
})
return
}

if body.OriginalUrl == "" {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Original URL is required",
})
return
}

var existingOriginalURL models.Tinyurl
if err := db.NewSelect().Model(&existingOriginalURL).Where("original_url = ?", body.OriginalUrl).Limit(1).Scan(ctx); err == nil {
ctx.JSON(http.StatusOK, dtos.URLCreationResponse{
Message: "Tiny URL already exists for the original URL",
ShortURL: existingOriginalURL.ShortUrl,
})
return
}

if body.ShortUrl != "" {
if len(body.ShortUrl) < 5 {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Custom short URL must be at least 5 characters long",
})
return
}

var existingURL models.Tinyurl
if err := db.NewSelect().Model(&existingURL).Where("short_url = ?", body.ShortUrl).Limit(1).Scan(ctx); err == nil {
ctx.JSON(http.StatusBadRequest, dtos.URLCreationResponse{
Message: "Custom short URL already exists",
})
return
}
} else {
generatedShortURL := utils.GenerateMD5Hash(body.OriginalUrl)
var existingURL models.Tinyurl
if err := db.NewSelect().Model(&existingURL).Where("short_url = ?", generatedShortURL).Limit(1).Scan(ctx); err != nil {
body.ShortUrl = generatedShortURL
}
}
count, _ := db.NewSelect().Model(models.Tinyurl{}).Where("user_id = ?", body.UserID).Count(ctx)
body.CreatedAt = time.Now().UTC()
if _, err := db.NewInsert().Model(&body).Exec(ctx); err != nil {
if count < 50 {
ctx.JSON(http.StatusInternalServerError, dtos.URLCreationResponse{
Message: "Failed to insert into the database: " + err.Error(),
})
return
}
}

ctx.JSON(http.StatusOK, dtos.URLCreationResponse{
Message: "Tiny URL created successfully",
ShortURL: body.ShortUrl,
})
}

func RedirectShortURL(ctx *gin.Context, db *bun.DB) {
shortURL := ctx.Param("shortURL")

var tinyURL models.Tinyurl
err := db.NewSelect().
Model(&tinyURL).
Where("short_url = ?", shortURL).
Scan(ctx, &tinyURL)
if err != nil {
ctx.JSON(http.StatusNotFound, dtos.URLDetailsResponse{
Message: "Short URL not found",
})
return
}

if !strings.HasPrefix(tinyURL.OriginalUrl, "http://") && !strings.HasPrefix(tinyURL.OriginalUrl, "https://") {
tinyURL.OriginalUrl = "http://" + tinyURL.OriginalUrl
}

tinyURL.AccessCount++
tinyURL.LastAccessedAt = time.Now().UTC()

_, err = db.NewUpdate().
Model(&tinyURL).
Column("access_count", "last_accessed_at").
WherePK().
Exec(ctx)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to update access count and timestamp",
})
return
}

ctx.Redirect(http.StatusMovedPermanently, tinyURL.OriginalUrl)
shortURL := ctx.Param("shortURL")

var tinyURL models.Tinyurl
err := db.NewSelect().
Model(&tinyURL).
Where("short_url = ?", shortURL).
Scan(ctx, &tinyURL)
if err != nil {
ctx.JSON(http.StatusNotFound, dtos.URLDetailsResponse{
Message: "Short URL not found",
})
return
}

if !strings.HasPrefix(tinyURL.OriginalUrl, "http://") && !strings.HasPrefix(tinyURL.OriginalUrl, "https://") {
tinyURL.OriginalUrl = "http://" + tinyURL.OriginalUrl
}

tinyURL.AccessCount++
tinyURL.LastAccessedAt = time.Now().UTC()

_, err = db.NewUpdate().
Model(&tinyURL).
Column("access_count", "last_accessed_at").
WherePK().
Exec(ctx)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to update access count and timestamp",
})
return
}

ctx.Redirect(http.StatusMovedPermanently, tinyURL.OriginalUrl)
}

func GetAllURLs(ctx *gin.Context, db *bun.DB) {
Expand Down Expand Up @@ -156,6 +158,21 @@ func GetAllURLs(ctx *gin.Context, db *bun.DB) {
URLs: urlDetails,
})
}
func DeleteURL(ctx *gin.Context, db *bun.DB) {
id, _ := ctx.Params.Get("id")
_, err := db.NewUpdate().Model(&models.Tinyurl{}).Set("is_deleted=?", true).Where("id = ?", id).Exec(ctx)

if err != nil {
ctx.JSON(http.StatusNotFound, dtos.UserURLsResponse{
Message: "No URLs found for the user",
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
})
return
}

ctx.JSON(http.StatusOK, dtos.UserURLsResponse{
Message: "Url Set to deleted",
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
})
}

func GetURLDetails(ctx *gin.Context, db *bun.DB) {
shortURL := ctx.Param("shortURL")
Expand Down
3 changes: 2 additions & 1 deletion migrations/20231007120000_create_tiny_url.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CREATE TABLE tiny_url (
created_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'),
created_by text NOT NULL,
access_count bigint DEFAULT 0,
last_accessed_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC')
last_accessed_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'),
is_deleted bit null DEFAULT 0
);

COMMIT;
5 changes: 3 additions & 2 deletions models/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type Tinyurl struct {
OriginalUrl string `bun:"original_url,notnull" json:"originalUrl"`
ShortUrl string `bun:"short_url,unique,notnull" json:"shortUrl"`
Comment string `bun:"comment" json:"comment"`
UserID int64 `bun:"user_id"`
User *User `bun:"rel:belongs-to,join:user_id=id"`
UserID int64 `bun:"user_id"`
User *User `bun:"rel:belongs-to,join:user_id=id"`
ExpiredAt time.Time `bun:"expired_at,notnull" json:"expiredAt"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"createdAt"`
CreatedBy string `bun:"created_by,notnull" json:"createdBy"`
AccessCount int64 `bun:"access_count,default:0" json:"accessCount"`
LastAccessedAt time.Time `bun:"last_accessed_at,nullzero" json:"lastAccessedAt"`
IsDeleted bool `bun:"is_deleted,null" json:"isDeleted"`
}
3 changes: 3 additions & 0 deletions routes/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ func TinyURLRoutes(rg *gin.RouterGroup, db *bun.DB) {
redirect.GET("/:shortURL", func(ctx *gin.Context) {
controller.RedirectShortURL(ctx, db)
})
urls.DELETE("/:id", func(ctx *gin.Context) {
controller.DeleteURL(ctx, db)
})
}
Loading