-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from totegamma/feat/add-notification-service
Feat/add notification service
- Loading branch information
Showing
13 changed files
with
493 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package notification | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"go.opentelemetry.io/otel" | ||
|
||
"github.com/totegamma/concurrent/core" | ||
) | ||
|
||
var tracer = otel.Tracer("notification") | ||
|
||
type Handler interface { | ||
Subscribe(c echo.Context) error | ||
Delete(c echo.Context) error | ||
Get(c echo.Context) error | ||
} | ||
|
||
type handler struct { | ||
service core.NotificationService | ||
} | ||
|
||
func NewHandler(service core.NotificationService) Handler { | ||
return &handler{service: service} | ||
} | ||
|
||
func (h *handler) Subscribe(c echo.Context) error { | ||
ctx, span := tracer.Start(c.Request().Context(), "Notification.Handler.Subscribe") | ||
defer span.End() | ||
|
||
var subscription core.NotificationSubscription | ||
err := c.Bind(&subscription) | ||
if err != nil { | ||
span.RecordError(err) | ||
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()}) | ||
} | ||
|
||
subscription, err = h.service.Subscribe(ctx, subscription) | ||
if err != nil { | ||
span.RecordError(err) | ||
return c.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()}) | ||
} | ||
|
||
return c.JSON(http.StatusCreated, echo.Map{"content": subscription}) | ||
} | ||
|
||
func (h *handler) Delete(c echo.Context) error { | ||
ctx, span := tracer.Start(c.Request().Context(), "Notification.Handler.Delete") | ||
defer span.End() | ||
|
||
owner := c.Param("owner") | ||
vendorID := c.Param("vendor_id") | ||
|
||
err := h.service.Delete(ctx, vendorID, owner) | ||
if err != nil { | ||
span.RecordError(err) | ||
return c.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()}) | ||
} | ||
|
||
return c.NoContent(http.StatusNoContent) | ||
} | ||
|
||
func (h *handler) Get(c echo.Context) error { | ||
ctx, span := tracer.Start(c.Request().Context(), "Notification.Handler.Get") | ||
defer span.End() | ||
|
||
owner := c.Param("owner") | ||
vendorID := c.Param("vendor_id") | ||
|
||
subscription, err := h.service.Get(ctx, vendorID, owner) | ||
if err != nil { | ||
span.RecordError(err) | ||
return c.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()}) | ||
} | ||
|
||
return c.JSON(http.StatusOK, echo.Map{"content": subscription}) | ||
} |
Oops, something went wrong.