Implement deletion of applications

This commit is contained in:
eikendev 2020-07-26 22:23:13 +02:00
parent 653e57fe03
commit 18d11677ac
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
7 changed files with 80 additions and 9 deletions

View file

@ -13,12 +13,15 @@ import (
// The ApplicationDatabase interface for encapsulating database access.
type ApplicationDatabase interface {
CreateApplication(application *model.Application) error
DeleteApplication(application *model.Application) error
GetApplicationByID(ID uint) (*model.Application, error)
GetApplicationByToken(token string) (*model.Application, error)
}
// The ApplicationDispatcher interface for relaying notifications.
type ApplicationDispatcher interface {
RegisterApplication(name, user string) (string, error)
DeregisterApplication(matrixID string) error
}
// ApplicationHandler holds information for processing requests about applications.
@ -32,16 +35,17 @@ func (h *ApplicationHandler) applicationExists(token string) bool {
return application != nil
}
// CreateApplication is used to create a new user.
// CreateApplication creates a user.
func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
application := model.Application{}
var createApplication model.CreateApplication
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&application)); !success {
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&createApplication)); !success {
return
}
user := authentication.GetUser(ctx)
application := model.Application{}
application.Token = authentication.GenerateNotExistingToken(authentication.GenerateApplicationToken, h.applicationExists)
application.UserID = user.ID
@ -61,3 +65,30 @@ func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &application)
}
// DeleteApplication deletes a user with a certain ID.
func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
var deleteApplication model.DeleteApplication
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.BindUri(&deleteApplication)); !success {
return
}
application, err := h.DB.GetApplicationByID(deleteApplication.ID)
log.Printf("Deleting application %s.\n", application.Name)
if success := successOrAbort(ctx, http.StatusBadRequest, err); !success {
return
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.Dispatcher.DeregisterApplication(application.MatrixID)); !success {
return
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DB.DeleteApplication(application)); !success {
return
}
ctx.JSON(http.StatusOK, gin.H{})
}

View file

@ -29,7 +29,7 @@ type NotificationHandler struct {
// CreateNotification is used to create a new notification for a user.
func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
notification := model.Notification{}
var notification model.Notification
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&notification)); !success {
return

View file

@ -27,7 +27,7 @@ func (h *UserHandler) userExists(name string) bool {
// CreateUser creates a new user.
func (h *UserHandler) CreateUser(ctx *gin.Context) {
externalUser := model.ExternalUserWithCredentials{}
var externalUser model.ExternalUserWithCredentials
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&externalUser)); !success {
return