Skip to content
Snippets Groups Projects
Commit 541d4f6c authored by Steffen Schulze's avatar Steffen Schulze
Browse files

update connection added

parent 0cd87fe8
No related branches found
No related tags found
1 merge request!10Refactoring
Pipeline #40714 passed with warnings with stages
in 3 minutes and 52 seconds
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"gitlab.eclipse.org/eclipse/xfsc/common-services/didcomm-connector/didcomm"
"gitlab.eclipse.org/eclipse/xfsc/common-services/didcomm-connector/internal/config"
"gitlab.eclipse.org/eclipse/xfsc/common-services/didcomm-connector/mediator/database"
"github.com/gin-gonic/gin"
)
......@@ -156,6 +160,72 @@ func (app *application) DeleteConnection(context *gin.Context) {
}
// @Summary Updates a connection
// @Schemes
// @Description Updates a connection
// @Tags Connections
// @Accept json
// @Produce json
// @Param did path string true "DID"
// @Success 200 "OK"
// @Failure 500 "Internal Server Error"
// @Router /admin/connections/{did} [delete]
func (app *application) UpdateConnection(context *gin.Context) {
logTag := "/admin/connections/{did} [update]"
did := context.Param("did")
config.Logger.Info(logTag, "did", did, "Start", true)
body, err := io.ReadAll(context.Request.Body)
if err != nil {
config.Logger.Error(logTag, "Error", err)
context.Status(http.StatusBadRequest)
return
}
err = context.Request.Body.Close()
if err != nil {
config.Logger.Error(logTag, "Error", err)
context.Status(http.StatusBadRequest)
return
}
if len(body) == 0 {
config.Logger.Error(logTag, "Error", errors.New("no body"))
context.Status(http.StatusBadRequest)
return
}
var mediatee database.Mediatee
err = json.Unmarshal(body, &mediatee)
if err != nil {
config.Logger.Error(logTag, "Error", err)
context.Status(http.StatusBadRequest)
return
}
if mediatee.RemoteDid != "" && mediatee.RemoteDid != did {
config.Logger.Error(logTag, "Error", errors.New("did mismatch"))
context.Status(http.StatusBadRequest)
return
}
mediatee.RemoteDid = did
err = app.mediator.Database.UpdateMediatee(mediatee)
if err != nil {
config.Logger.Error(logTag, "Error", err)
context.Status(http.StatusInternalServerError)
return
}
config.Logger.Info(logTag, "End", true)
context.Status(http.StatusOK)
}
// @Summary Blocks connection
// @Schemes
// @Description Blocks connection
......
......@@ -24,7 +24,7 @@ func (app *application) NewRouter() *gin.Engine {
connectionsGroup := adminGroup.Group("connections")
connectionsGroup.GET("", app.GetConnections)
connectionsGroup.GET(":did", app.GetConnection)
//connectionsGroup.POST("", app.CreateConnection)
connectionsGroup.PUT(":did", app.UpdateConnection)
connectionsGroup.DELETE(":did", app.DeleteConnection)
// Block Connections (Mediatees)
connectionsGroup.POST("block/:did", app.BlockConnection)
......
......@@ -9,6 +9,7 @@ type Adapter interface {
// Connections (Mediatees)
GetMediatees(group *string) ([]Mediatee, error)
GetMediatee(remoteDid string) (*Mediatee, error)
UpdateMediatee(mediatee Mediatee) error
AddMediatee(mediatee Mediatee) (err error)
DeleteMediatee(remoteDid string) (err error)
IsMediated(remoteDid string) (isMediated bool, err error)
......
......@@ -2,6 +2,7 @@ package database
import (
"errors"
"strings"
"time"
"gitlab.eclipse.org/eclipse/xfsc/common-services/didcomm-connector/didcomm"
......@@ -98,6 +99,58 @@ func (db *Cassandra) GetMediatee(remoteDid string) (*Mediatee, error) {
return dataset, nil
}
func (db *Cassandra) UpdateMediatee(mediatee Mediatee) error {
logTag := "UpdateMediatee"
config.Logger.Info(logTag, "Start", true, "mediatee", mediatee)
query := "UPDATE mediatees SET "
var values []interface{} = make([]interface{}, 0)
if mediatee.RoutingKey != "" {
query = query + "routing_key=?,"
values = append(values, mediatee.RoutingKey)
}
if mediatee.Protocol != "" {
query = query + "protocol=?,"
values = append(values, mediatee.Protocol)
}
if mediatee.RecipientDids != nil {
query = query + "recipient_dids=?,"
values = append(values, mediatee.RecipientDids)
}
if mediatee.Topic != "" {
query = query + "topic=?,"
values = append(values, mediatee.Topic)
}
if mediatee.Properties != nil {
query = query + "properties=?,"
values = append(values, mediatee.Properties)
}
if mediatee.EventType != "" {
query = query + "eventtype=?,"
values = append(values, mediatee.EventType)
}
if mediatee.Group != "" {
query = query + "group=?,"
values = append(values, mediatee.Group)
}
values = append(values, mediatee.RemoteDid)
query = strings.Trim(query, ",") + " WHERE remote_did=?"
if err := db.session.Query(query, values...).Exec(); err != nil {
config.Logger.Error(logTag, "Error while executing the query", err)
return errors.New(logTag + ". Error while executing the query: " + query + ". " + err.Error())
}
config.Logger.Info(logTag, "End", true)
return nil
}
func (db *Cassandra) AddMediatee(mediatee Mediatee) error {
logTag := "AddMediatee"
config.Logger.Info(logTag, "Start", true, "mediatee", mediatee)
......
......@@ -79,6 +79,16 @@ func (d *Demo) AddMediatee(mediatee Mediatee) error {
return nil
}
func (d *Demo) UpdateMediatee(mediatee Mediatee) error {
for i, m := range d.mediatees {
if m.RemoteDid == mediatee.RemoteDid {
d.mediatees[i] = mediatee
return nil
}
}
return errors.New("could not find mediatee")
}
func (d *Demo) DeleteMediatee(remoteDid string) (err error) {
for i, mediatee := range d.mediatees {
if mediatee.RemoteDid == remoteDid {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment