Skip to content

Commit

Permalink
Merge pull request #19 from PickHD/SRS-12
Browse files Browse the repository at this point in the history
SRS-12
  • Loading branch information
PickHD authored May 17, 2023
2 parents 3889e2c + 26d2f4d commit 8e6b40f
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 40 deletions.
4 changes: 4 additions & 0 deletions shortener/api/v1/proto/shortener/shortener.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ message UpdateShortenerMessage {
string full_url=2;
}

message DeleteShortenerMessage {
string id = 1;
}

1 change: 1 addition & 0 deletions shortener/cmd/v1/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ AMQP_SERVER_URL=amqp://guest:guest@amqp:5672/
AMQP_QUEUE_CREATE_SHORTENER=create-shortener-queue
AMQP_QUEUE_UPDATE_VISITOR_COUNT=update-visitor-count-queue
AMQP_QUEUE_UPDATE_SHORTENER=update-shortener-queue
AMQP_QUEUE_DELETE_SHORTENER=delete-shortener-queue

GRPC_PORT=9091

Expand Down
2 changes: 1 addition & 1 deletion shortener/cmd/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func main() {
// Make a channel to receive messages into infinite loop.
forever := make(chan bool)

queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener}
queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener, app.Config.RabbitMQ.QueueDeleteShortener}

for _, q := range queues {
go infrastructure.ConsumeMessages(app, q)
Expand Down
2 changes: 1 addition & 1 deletion shortener/internal/v1/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func SetupApplication(ctx context.Context) (*App, error) {
return app, err
}

queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener}
queues := []string{app.Config.RabbitMQ.QueueCreateShortener, app.Config.RabbitMQ.QueueUpdateVisitor, app.Config.RabbitMQ.QueueUpdateShortener, app.Config.RabbitMQ.QueueDeleteShortener}

for _, q := range queues {
_, err = amqpClient.QueueDeclare(
Expand Down
2 changes: 2 additions & 0 deletions shortener/internal/v1/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type (
QueueCreateShortener string
QueueUpdateVisitor string
QueueUpdateShortener string
QueueDeleteShortener string
}

Tracer struct {
Expand Down Expand Up @@ -77,6 +78,7 @@ func loadConfiguration() *Configuration {
QueueCreateShortener: helper.GetEnvString("AMQP_QUEUE_CREATE_SHORTENER"),
QueueUpdateVisitor: helper.GetEnvString("AMQP_QUEUE_UPDATE_VISITOR_COUNT"),
QueueUpdateShortener: helper.GetEnvString("AMQP_QUEUE_UPDATE_SHORTENER"),
QueueDeleteShortener: helper.GetEnvString("AMQP_QUEUE_DELETE_SHORTENER"),
},
Tracer: &Tracer{
JaegerURL: helper.GetEnvString("JAEGER_URL"),
Expand Down
18 changes: 18 additions & 0 deletions shortener/internal/v1/controller/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
ProcessCreateShortUser(ctx context.Context, msg *shortenerpb.CreateShortenerMessage) error
ProcessUpdateVisitorCount(ctx context.Context, msg *shortenerpb.UpdateVisitorCountMessage) error
ProcessUpdateShortUser(ctx context.Context, msg *shortenerpb.UpdateShortenerMessage) error
ProcessDeleteShortUser(ctx context.Context, msg *shortenerpb.DeleteShortenerMessage) error
}

// ShortControllerImpl is an app short struct that consists of all the dependencies needed for short controller
Expand Down Expand Up @@ -169,3 +170,20 @@ func (sc *ShortControllerImpl) ProcessUpdateShortUser(ctx context.Context, msg *

return nil
}

func (sc *ShortControllerImpl) ProcessDeleteShortUser(ctx context.Context, msg *shortenerpb.DeleteShortenerMessage) error {
tr := sc.Tracer.Tracer("Shortener-ProcessDeleteShortUser Controller")
_, span := tr.Start(sc.Context, "Start ProcessDeleteShortUser")
defer span.End()

req := &model.DeleteShortRequest{
ID: msg.GetId(),
}

err := sc.ShortSvc.DeleteShort(ctx, req)
if err != nil {
return model.NewError(model.Internal, err.Error())
}

return nil
}
16 changes: 16 additions & 0 deletions shortener/internal/v1/infrastructure/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func ConsumeMessages(app *application.App, queueName string) {
app.Logger.Error("ProcessUpdateShortUser ERROR, ", err)
}

app.Logger.Info(fmt.Sprintf("[%s] Success Process Message :", queueName), req)
case app.Config.RabbitMQ.QueueDeleteShortener:
req := &shortenerpb.DeleteShortenerMessage{}

err := proto.Unmarshal(msg.Body, req)
if err != nil {
app.Logger.Error("Unmarshal proto DeleteShortenerMessage ERROR, ", err)
}

app.Logger.Info(fmt.Sprintf("[%s] Success Consume Message :", queueName), req)

err = dep.ShortController.ProcessDeleteShortUser(app.Context, req)
if err != nil {
app.Logger.Error("ProcessDeleteShortUser ERROR, ", err)
}

app.Logger.Info(fmt.Sprintf("[%s] Success Process Message :", queueName), req)
}
}
Expand Down
4 changes: 4 additions & 0 deletions shortener/internal/v1/model/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ type (
ID string `json:"id"`
FullURL string `json:"full_url"`
}

DeleteShortRequest struct {
ID string `json:"id"`
}
)
22 changes: 22 additions & 0 deletions shortener/internal/v1/repository/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
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
}

// ShortRepositoryImpl is an app short struct that consists of all the dependencies needed for short repository
Expand Down Expand Up @@ -237,6 +238,27 @@ func (sr *ShortRepositoryImpl) UpdateFullURLByID(ctx context.Context, req *model
return nil
}

func (sr *ShortRepositoryImpl) DeleteByID(ctx context.Context, req *model.DeleteShortRequest) error {
tr := sr.Tracer.Tracer("Shortener-DeleteByID Repository")
ctx, span := tr.Start(ctx, "Start DeleteByID")
defer span.End()

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

_, err = sr.DB.Collection(sr.Config.Database.ShortenersCollection).DeleteOne(ctx,
bson.D{{Key: "_id", Value: objShortID}})
if err != nil {
sr.Logger.Error("ShortRepositoryImpl.DeleteByID DeleteOne ERROR, ", err)
return err
}

return nil
}

func (sr *ShortRepositoryImpl) prepareProtoPublishUpdateVisitorCountMessage(req *model.UpdateVisitorRequest) *shortenerpb.UpdateVisitorCountMessage {
return &shortenerpb.UpdateVisitorCountMessage{
ShortUrl: req.ShortURL,
Expand Down
9 changes: 9 additions & 0 deletions shortener/internal/v1/service/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
ClickShort(shortURL string) (*model.ClickShortResponse, error)
UpdateVisitorShort(ctx context.Context, req *model.UpdateVisitorRequest) error
UpdateShort(ctx context.Context, req *model.UpdateShortRequest) error
DeleteShort(ctx context.Context, req *model.DeleteShortRequest) error
}

// ShortServiceImpl is an app short struct that consists of all the dependencies needed for short repository
Expand Down Expand Up @@ -156,6 +157,14 @@ func (ss *ShortServiceImpl) UpdateShort(ctx context.Context, req *model.UpdateSh
return ss.ShortRepo.UpdateFullURLByID(ctx, req)
}

func (ss *ShortServiceImpl) DeleteShort(ctx context.Context, req *model.DeleteShortRequest) error {
tr := ss.Tracer.Tracer("Shortener-DeleteShort Service")
ctx, span := tr.Start(ctx, "Start DeleteShort")
defer span.End()

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

func (ss *ShortServiceImpl) validateCreateShort(req *model.CreateShortRequest) error {
if _, err := url.ParseRequestURI(req.FullURL); err != nil {
return model.NewError(model.Validation, err.Error())
Expand Down
98 changes: 80 additions & 18 deletions shortener/pkg/api/v1/proto/shortener/shortener.pb.go

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

4 changes: 4 additions & 0 deletions user/api/v1/proto/shortener/shortener.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ message UpdateVisitorCountMessage {
message UpdateShortenerMessage {
string id =1;
string full_url=2;
}

message DeleteShortenerMessage {
string id = 1;
}
1 change: 1 addition & 0 deletions user/cmd/v1/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ AMQP_QUEUE_CREATE_SHORTENER=create-shortener-queue
AMQP_QUEUE_UPDATE_VISITOR_COUNT=update-visitor-count-queue
AMQP_QUEUE_UPLOAD_AVATAR=upload-avatar-queue
AMQP_QUEUE_UPDATE_SHORTENER=update-shortener-queue
AMQP_QUEUE_DELETE_SHORTENER=delete-shortener-queue

JWT_SECRET=secret
JWT_EXPIRE=7
Expand Down
54 changes: 54 additions & 0 deletions user/docs/docs.go

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

Loading

0 comments on commit 8e6b40f

Please sign in to comment.