diff --git a/shortener/internal/v1/repository/short.go b/shortener/internal/v1/repository/short.go index a13d52e..43bc644 100644 --- a/shortener/internal/v1/repository/short.go +++ b/shortener/internal/v1/repository/short.go @@ -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 @@ -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") @@ -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, diff --git a/shortener/internal/v1/service/short.go b/shortener/internal/v1/service/short.go index 85823cb..ddb2aa2 100644 --- a/shortener/internal/v1/service/short.go +++ b/shortener/internal/v1/service/short.go @@ -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) } @@ -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) } diff --git a/user/internal/v1/service/user.go b/user/internal/v1/service/user.go index 3dc5e70..6677558 100644 --- a/user/internal/v1/service/user.go +++ b/user/internal/v1/service/user.go @@ -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) @@ -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 }