mirror of
https://github.com/pushbits/server.git
synced 2025-06-08 13:42:05 +02:00
Remove some duplication in API handlers
This commit is contained in:
parent
018ce2e537
commit
a5418d9698
5 changed files with 66 additions and 54 deletions
|
@ -36,6 +36,20 @@ func (h *ApplicationHandler) applicationExists(token string) bool {
|
|||
return application != nil
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) getApplication(ctx *gin.Context) (*model.Application, error) {
|
||||
id, err := getID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
application, err := h.DB.GetApplicationByID(id)
|
||||
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return application, nil
|
||||
}
|
||||
|
||||
// CreateApplication creates an application.
|
||||
func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
|
||||
var createApplication model.CreateApplication
|
||||
|
@ -69,16 +83,11 @@ func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
|
|||
|
||||
// DeleteApplication deletes an application with a certain ID.
|
||||
func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
|
||||
id, err := getID(ctx)
|
||||
application, err := h.getApplication(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
application, err := h.DB.GetApplicationByID(id)
|
||||
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
if !isCurrentUser(ctx, application.UserID) {
|
||||
return
|
||||
}
|
||||
|
@ -98,22 +107,16 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
|
|||
|
||||
// UpdateApplication updates an application with a certain ID.
|
||||
func (h *ApplicationHandler) UpdateApplication(ctx *gin.Context) {
|
||||
id, err := getID(ctx)
|
||||
application, err := h.getApplication(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
application, err := h.DB.GetApplicationByID(id)
|
||||
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
if !isCurrentUser(ctx, application.UserID) {
|
||||
return
|
||||
}
|
||||
|
||||
var updateApplication model.UpdateApplication
|
||||
|
||||
if err := ctx.BindUri(&updateApplication); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue