app "url-shortener" // ----------------------------------------------------------------------------- // Models // ----------------------------------------------------------------------------- model ShortLink { id ID slug string @min(2) @max(64) @unique target string @min(1) @max(2048) title string? @max(120) owner UserID @index enabled bool = true clickCount int = 0 expiresAt Time? createdAt Time = now() updatedAt Time = now() } model LinkClick { id ID link ShortLinkID @index owner UserID @index clickedAt Time = now() } // ----------------------------------------------------------------------------- // Input and response types // ----------------------------------------------------------------------------- type CreateShortLinkInput struct { slug string @min(2) @max(64) target string @min(1) @max(2048) title string? @max(120) expiresAt Time? } type UpdateShortLinkInput struct { target string @min(1) @max(2048) title string? @max(120) expiresAt Time? } type ResolveShortLinkResponse struct { link ShortLinkID slug string target string } type ShortLinkDetails struct { link ShortLink clicks []LinkClick } // ----------------------------------------------------------------------------- // Events // ----------------------------------------------------------------------------- event ShortLinkCreated { link ShortLinkID owner UserID slug string target string at Time = now() } event ShortLinkResolved { link ShortLinkID owner UserID slug string at Time = now() } event ShortLinkUpdated { link ShortLinkID owner UserID slug string at Time = now() } event ShortLinkDisabled { link ShortLinkID owner UserID slug string at Time = now() } event ShortLinkEnabled { link ShortLinkID owner UserID slug string at Time = now() } event ShortLinkDeleted { link ShortLinkID owner UserID slug string at Time = now() } // ----------------------------------------------------------------------------- // Link management // ----------------------------------------------------------------------------- // @api POST /links // @auth user // @cache invalidate ListShortLinks func CreateShortLink(input CreateShortLinkInput) ShortLink { existing := database.shortLinks. where(slug == input.slug). maybeOne() if existing != nil { fail.conflict("This short-link slug is already in use") } link := database.shortLinks.create(ShortLink{ slug: input.slug, target: input.target, title: input.title, owner: auth.user.id, expiresAt: input.expiresAt, }) emit(ShortLinkCreated{ link: link.id, owner: auth.user.id, slug: link.slug, target: link.target, }) return link } // @api GET /links // @auth user // @cache 30s key=user,params func ListShortLinks( enabled bool? = nil, ) []ShortLink { query := database.shortLinks. where(owner == auth.user.id) if enabled != nil { query = query.where(enabled == enabled) } return query. order(updatedAt, desc). limit(100). all() } // @api GET /links/:link // @auth user owns ShortLink.owner func GetShortLink(link ShortLinkID) ShortLinkDetails { currentLink := database.shortLinks.get(link) clicks := database.linkClicks. where(link == link). order(clickedAt, desc). limit(100). all() return ShortLinkDetails{ link: currentLink, clicks: clicks, } } // @api PATCH /links/:link // @auth user owns ShortLink.owner // @cache invalidate ListShortLinks func UpdateShortLink( link ShortLinkID, input UpdateShortLinkInput, ) ShortLink { currentLink := database.shortLinks.get(link) currentLink.target = input.target currentLink.title = input.title currentLink.expiresAt = input.expiresAt currentLink.updatedAt = now() updated := database.shortLinks.update(currentLink) emit(ShortLinkUpdated{ link: updated.id, owner: updated.owner, slug: updated.slug, }) return updated } // @api POST /links/:link/disable // @auth user owns ShortLink.owner // @cache invalidate ListShortLinks func DisableShortLink(link ShortLinkID) ShortLink { currentLink := database.shortLinks.get(link) if currentLink.enabled == false { fail.conflict("This short link is already disabled") } currentLink.enabled = false currentLink.updatedAt = now() updated := database.shortLinks.update(currentLink) emit(ShortLinkDisabled{ link: updated.id, owner: updated.owner, slug: updated.slug, }) return updated } // @api POST /links/:link/enable // @auth user owns ShortLink.owner // @cache invalidate ListShortLinks func EnableShortLink(link ShortLinkID) ShortLink { currentLink := database.shortLinks.get(link) if currentLink.enabled == true { fail.conflict("This short link is already enabled") } if currentLink.expiresAt != nil { if currentLink.expiresAt < now() { fail.conflict("An expired short link cannot be enabled") } } currentLink.enabled = true currentLink.updatedAt = now() updated := database.shortLinks.update(currentLink) emit(ShortLinkEnabled{ link: updated.id, owner: updated.owner, slug: updated.slug, }) return updated } // @api DELETE /links/:link // @auth user owns ShortLink.owner // @cache invalidate ListShortLinks func DeleteShortLink(link ShortLinkID) { currentLink := database.shortLinks.get(link) database.linkClicks. where(link == link). delete() database.shortLinks.delete(link) emit(ShortLinkDeleted{ link: currentLink.id, owner: currentLink.owner, slug: currentLink.slug, }) } // ----------------------------------------------------------------------------- // Public resolution // ----------------------------------------------------------------------------- // @api GET /r/:slug func ResolveShortLink(slug string) ResolveShortLinkResponse { link := database.shortLinks. where(slug == slug). maybeOne() if link == nil { fail.notFound("Short link not found") } if link.enabled == false { fail.notFound("Short link not found") } if link.expiresAt != nil { if link.expiresAt < now() { fail.notFound("Short link not found") } } database.linkClicks.create(LinkClick{ link: link.id, owner: link.owner, }) link.clickCount = link.clickCount + 1 link.updatedAt = now() database.shortLinks.update(link) emit(ShortLinkResolved{ link: link.id, owner: link.owner, slug: link.slug, }) return ResolveShortLinkResponse{ link: link.id, slug: link.slug, target: link.target, } }