Remove some duplication in API handlers

This commit is contained in:
eikendev 2020-08-02 17:31:41 +02:00
parent 018ce2e537
commit a5418d9698
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
5 changed files with 66 additions and 54 deletions

View file

@ -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
}