Skip to content

Commit

Permalink
support update/delete by id
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jul 21, 2022
1 parent 488ddf1 commit 1c3226e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
37 changes: 33 additions & 4 deletions url/handler/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
const hostPrefix = "https://m3o.one/u/"

var (
idRegex = regexp.MustCompile("[a-z-]+")
idRegex = regexp.MustCompile("[a-zA-Z0-9_-]+")
)

type Url struct {
Expand Down Expand Up @@ -58,7 +58,15 @@ func (e *Url) Delete(ctx context.Context, req *url.DeleteRequest, rsp *url.Delet
return errors.Unauthorized("url.shorten", "not authorized")
}

id := strings.TrimPrefix(req.ShortURL, e.hostPrefix)
if len(req.Id) == 0 && len(req.ShortURL) == 0 {
return errors.BadRequest("url.delete", "missing id or short url")
}

id := req.Id

if len(id) == 0 {
id = strings.TrimPrefix(req.ShortURL, e.hostPrefix)
}

// check if exists
recs, err := store.Read("urlOwner/" + tenantId + "/" + id)
Expand All @@ -82,7 +90,20 @@ func (e *Url) Create(ctx context.Context, req *url.CreateRequest, rsp *url.Creat
return errors.Unauthorized("url.create", "not authorized")
}

if len(req.Id) == 0 || !idRegex.MatchString(req.Id) {
if len(req.Id) == 0 {
sid, err := shortid.New(1, shortid.DefaultABC, 2342)
if err != nil {
return err
}

id, err := sid.Generate()
if err != nil {
return err
}
req.Id = id
}

if !idRegex.MatchString(req.Id) {
return errors.BadRequest("url.create", "invalid id")
}
_, err := u.Parse(req.DestinationURL)
Expand Down Expand Up @@ -132,7 +153,15 @@ func (e *Url) Update(ctx context.Context, req *url.UpdateRequest, rsp *url.Updat
return errors.Unauthorized("url.shorten", "not authorized")
}

id := strings.Replace(req.ShortURL, e.hostPrefix, "", -1)
if len(req.Id) == 0 && len(req.ShortURL) == 0 {
return errors.BadRequest("url.update", "missing id or short url")
}

id := req.Id

if len(id) == 0 {
id = strings.Replace(req.ShortURL, e.hostPrefix, "", -1)
}

// check the owner has this short url
records, err := store.Read("urlOwner/" + tenantId + "/" + id)
Expand Down
44 changes: 32 additions & 12 deletions url/proto/url.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions url/proto/url.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ message CreateResponse {

// Delete a URL
message DeleteRequest {
string shortURL = 1;
// specify id or shortURL
string id = 1;
// optional shortURL
string shortURL = 2;
}

message DeleteResponse {
Expand Down Expand Up @@ -69,18 +72,19 @@ message ListResponse {

// Update the destination for a short URL
message UpdateRequest {
// the short url to update
string shortURL = 1;
// update by id
string id = 1;
// update by short url
string shortURL = 2;
// the destination to update to
string destinationURL = 2;
string destinationURL = 3;
}

message UpdateResponse {}

// Resolve returns the destination URL of a short URL.
message ResolveRequest {
// short url ID, without the domain, eg. if your short URL is
// `m3o.one/u/someshorturlid` then pass in `someshorturlid`
// short url to resolve
string shortURL = 1;
}

Expand Down

0 comments on commit 1c3226e

Please sign in to comment.