Skip to content

Commit

Permalink
Merge pull request #21 from PickHD/SRI-03
Browse files Browse the repository at this point in the history
SRI-03
  • Loading branch information
PickHD authored Jul 3, 2023
2 parents 304117b + 47262ad commit 96a2c75
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
43 changes: 43 additions & 0 deletions shortener/internal/v1/repository/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ type (
Create(ctx context.Context, req *model.Short) error
GetByShortURL(ctx context.Context, shortURL string) (*model.Short, error)
GetFullURLByKey(ctx context.Context, shortURL string) (string, error)
GetByID(ctx context.Context, ID string) (*model.Short, error)
SetFullURLByKey(ctx context.Context, shortURL string, fullURL string, duration time.Duration) error
PublishUpdateVisitorCount(ctx context.Context, req *model.UpdateVisitorRequest) error
UpdateVisitorByShortURL(ctx context.Context, req *model.UpdateVisitorRequest, lastVisitedCount int64) error
UpdateFullURLByID(ctx context.Context, req *model.UpdateShortRequest) error
DeleteByID(ctx context.Context, req *model.DeleteShortRequest) error
DeleteFullURLByKey(ctx context.Context, shortURL string) error
}

// ShortRepositoryImpl is an app short struct that consists of all the dependencies needed for short repository
Expand Down Expand Up @@ -145,6 +147,32 @@ func (sr *ShortRepositoryImpl) GetFullURLByKey(ctx context.Context, shortURL str
return result.Val(), nil
}

func (sr *ShortRepositoryImpl) GetByID(ctx context.Context, id string) (*model.Short, error) {
tr := sr.Tracer.Tracer("Shortener-GetByID Repository")
ctx, span := tr.Start(ctx, "Start GetByID")
defer span.End()

objShortID, err := primitive.ObjectIDFromHex(id)
if err != nil {
sr.Logger.Error("ShortRepositoryImpl.GetByID primitive.ObjectIDFromHex ERROR, ", err)
return nil, err
}

short := &model.Short{}

err = sr.DB.Collection(sr.Config.Database.ShortenersCollection).FindOne(ctx, bson.D{{Key: "_id", Value: objShortID}}).Decode(&short)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, model.NewError(model.NotFound, "short_url not found")
}

sr.Logger.Error("ShortRepositoryImpl.GetByID FindOne ERROR,", err)
return nil, err
}

return short, nil
}

func (sr *ShortRepositoryImpl) SetFullURLByKey(ctx context.Context, shortURL string, fullURL string, duration time.Duration) error {
tr := sr.Tracer.Tracer("Shortener-SetFullURLByKey Repository")
ctx, span := tr.Start(ctx, "Start SetFullURLByKey")
Expand Down Expand Up @@ -259,6 +287,21 @@ func (sr *ShortRepositoryImpl) DeleteByID(ctx context.Context, req *model.Delete
return nil
}

func (sr *ShortRepositoryImpl) DeleteFullURLByKey(ctx context.Context, shortURL string) error {
tr := sr.Tracer.Tracer("Shortener-DeleteFullURLByKey Repository")
ctx, span := tr.Start(ctx, "Start DeleteFullURLByKey")
defer span.End()

err := sr.Redis.Del(ctx, fmt.Sprintf(model.KeyShortURL, shortURL)).Err()
if err != nil {
sr.Logger.Error("ShortRepositoryImpl.DeleteFullURLByKey Del ERROR, ", err)

return err
}

return nil
}

func (sr *ShortRepositoryImpl) prepareProtoPublishUpdateVisitorCountMessage(req *model.UpdateVisitorRequest) *shortenerpb.UpdateVisitorCountMessage {
return &shortenerpb.UpdateVisitorCountMessage{
ShortUrl: req.ShortURL,
Expand Down
22 changes: 22 additions & 0 deletions shortener/internal/v1/service/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ func (ss *ShortServiceImpl) UpdateShort(ctx context.Context, req *model.UpdateSh
return model.NewError(model.Validation, err.Error())
}

data, err := ss.ShortRepo.GetByID(ctx, req.ID)
if err != nil {
return err
}

// delete cache if any
err = ss.ShortRepo.DeleteFullURLByKey(ctx, data.ShortURL)
if err != nil {
return err
}

return ss.ShortRepo.UpdateFullURLByID(ctx, req)
}

Expand All @@ -162,6 +173,17 @@ func (ss *ShortServiceImpl) DeleteShort(ctx context.Context, req *model.DeleteSh
ctx, span := tr.Start(ctx, "Start DeleteShort")
defer span.End()

data, err := ss.ShortRepo.GetByID(ctx, req.ID)
if err != nil {
return err
}

// delete cache if any
err = ss.ShortRepo.DeleteFullURLByKey(ctx, data.ShortURL)
if err != nil {
return err
}

return ss.ShortRepo.DeleteByID(ctx, req)
}

Expand Down
4 changes: 2 additions & 2 deletions user/internal/v1/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (us *UserServiceImpl) GenerateUserShorts(userID string, req *model.ShortUse
msg := model.GenerateShortUserMessage{
FullURL: req.FullURL,
UserID: userID,
ShortURL: fmt.Sprintf("%s/%s", us.Config.HttpService.ShortenerBaseAPIURL, helper.RandomStringBytesMaskImprSrcSB(8)),
ShortURL: helper.RandomStringBytesMaskImprSrcSB(8),
}

err := us.UserRepo.PublishCreateUserShortener(us.Context, &msg)
Expand All @@ -107,7 +107,7 @@ func (us *UserServiceImpl) GenerateUserShorts(userID string, req *model.ShortUse
}

return &model.ShortUserResponse{
ShortURL: msg.ShortURL,
ShortURL: fmt.Sprintf("%s/%s", us.Config.HttpService.ShortenerBaseAPIURL, msg.ShortURL),
Method: "GET",
}, nil
}
Expand Down

0 comments on commit 96a2c75

Please sign in to comment.