Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add api for getting a single subscription #998

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/controller/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func CreateSubscription(dataBase moira.Database, userLogin, teamID string, subsc
return nil
}

// GetSubscription returns subscription by it's id.
func GetSubscription(dataBase moira.Database, subscriptionID string) (*dto.Subscription, *api.ErrorResponse) {
subscription, err := dataBase.GetSubscription(subscriptionID)
if err != nil {
return nil, api.ErrorInternalServer(err)
}
dto := dto.Subscription(subscription)
return &dto, nil
}

// UpdateSubscription updates existing subscription.
func UpdateSubscription(dataBase moira.Database, subscriptionID string, userLogin string, subscription *dto.Subscription) *api.ErrorResponse {
subscription.ID = subscriptionID
Expand Down
26 changes: 26 additions & 0 deletions api/handler/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func subscription(router chi.Router) {
router.Route("/{subscriptionId}", func(router chi.Router) {
router.Use(middleware.SubscriptionContext)
router.Use(subscriptionFilter)
router.Get("/", getSubscription)
router.Put("/", updateSubscription)
router.Delete("/", removeSubscription)
router.Put("/test", sendTestNotification)
Expand Down Expand Up @@ -102,6 +103,31 @@ func subscriptionFilter(next http.Handler) http.Handler {
})
}

// nolint: gofmt,goimports
//
// @summary Get subscription by id
// @id get-subscription
// @tags subscription
// @produce json
// @param subscriptionID path string true "ID of the subscription to get" default(bcba82f5-48cf-44c0-b7d6-e1d32c64a88c)
// @success 200 {object} dto.Subscription "Subscription fetched successfully"
// @failure 403 {object} api.ErrorForbiddenExample "Forbidden"
// @failure 422 {object} api.ErrorRenderExample "Render error"
// @failure 500 {object} api.ErrorInternalServerExample "Internal server error"
// @router /subscription/{subscriptionID} [get]
func getSubscription(writer http.ResponseWriter, request *http.Request) {
subscriptionID := middleware.GetSubscriptionID(request)
subscription, err := controller.GetSubscription(database, subscriptionID)
if err != nil {
render.Render(writer, request, err) //nolint
return
}
if err := render.Render(writer, request, subscription); err != nil {
render.Render(writer, request, api.ErrorRender(err)) //nolint
return
}
}

// nolint: gofmt,goimports
//
// @summary Update a subscription
Expand Down
Loading