Skip to content

Commit

Permalink
cleaned up DB interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Dec 30, 2023
1 parent 7dff5c5 commit 9373978
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 21 deletions.
16 changes: 10 additions & 6 deletions server/registration/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import "go.dedis.ch/hbt/server/registration/registry"
// Database defines a generic CRUD interface to the database
type Database interface {
// Create creates a new document in the database
Create(registry.RegistrationData) (*registry.RegistrationId, error)
// it takes the document as an argument
// and returns the document ID or an error
Create(*registry.RegistrationData) (*registry.RegistrationId, error)

// Read retrieves a document from the database
// it takes the document ID as an argument
// and returns the document
// it takes the document ID and the hash as arguments
// and returns the document or an error
Read(registry.RegistrationId, []byte) (*registry.RegistrationData, error)

// Update updates a document in the database
// it takes the document ID and the updated document as an argument
Update(registry.RegistrationId, []byte, *registry.RegistrationData) error
// and returns nil or an error
Update(registry.RegistrationId, *registry.RegistrationData) error

// DeleteDocument deletes a document from the database
// it takes the document ID as an argument
// Delete deletes a document from the database
// it takes the document ID and the hash as arguments
// and returns nil or an error
Delete(registry.RegistrationId, []byte) error

// Disconnect disconnects from the database
Expand Down
3 changes: 1 addition & 2 deletions server/registration/database/mongodb/admindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewAdminDbAccess() *AdminDbAccess {
}

// Create creates a new document in the DB
func (a AdminDbAccess) Create(doc registry.RegistrationData) (*registry.RegistrationId, error) {
func (a AdminDbAccess) Create(doc *registry.RegistrationData) (*registry.RegistrationId, error) {
return nil, errors.New("admin cannot create user documents")
}

Expand All @@ -49,7 +49,6 @@ func (a AdminDbAccess) Read(docId registry.RegistrationId, hash []byte) (
// Update updates a document in the DB
func (a AdminDbAccess) Update(
docId registry.RegistrationId,
hash []byte,
reg *registry.RegistrationData,
) error {
return nil
Expand Down
5 changes: 2 additions & 3 deletions server/registration/database/mongodb/userdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewUserDbAccess() database.Database {
}

// Create creates a new document in the DB
func (u UserDbAccess) Create(data registry.RegistrationData) (*registry.RegistrationId, error) {
func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.RegistrationId, error) {
doc := Document{
Name: data.Name,
Passport: data.Passport,
Expand Down Expand Up @@ -105,7 +105,6 @@ func (u UserDbAccess) Read(docId registry.RegistrationId, hash []byte) (
// Update updates a document in the DB
func (u UserDbAccess) Update(
docId registry.RegistrationId,
hash []byte,
reg *registry.RegistrationData,
) error {
var doc Document
Expand All @@ -116,7 +115,7 @@ func (u UserDbAccess) Update(
return err
}

if !bytes.Equal(hash, doc.Hash) {
if !bytes.Equal(reg.Hash, doc.Hash) {
return errors.New("hashes do not match")
}

Expand Down
5 changes: 3 additions & 2 deletions server/registration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ func newApp() *application {
userRouter := mux.NewRouter()
userRouter.HandleFunc("/document", user.CreateDocument).Methods("POST")
userRouter.HandleFunc("/document", user.GetDocument).Methods("GET")
userRouter.HandleFunc("/document", user.UpdateDocument).Methods("PUT")
userRouter.HandleFunc("/document", user.DeleteDocument).Methods("DELETE")

adminRouter := mux.NewRouter()
adminRouter.HandleFunc("/admin/document", admin.GetDocument).Methods("GET")
adminRouter.HandleFunc("/admin/document/{id}", admin.UpdateDocument).Methods("PUT")
adminRouter.HandleFunc("/admin/document/{id}", admin.DeleteDocument).Methods("DELETE")
adminRouter.HandleFunc("/admin/document", admin.UpdateDocument).Methods("PUT")
adminRouter.HandleFunc("/admin/document", admin.DeleteDocument).Methods("DELETE")

userDb := mongodb.NewUserDbAccess()
user.RegisterDb(userDb)
Expand Down
Binary file removed server/registration/registration
Binary file not shown.
76 changes: 68 additions & 8 deletions server/registration/registry/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func RegisterDb(db database.Database) {

// CreateDocument translates the http request to create a new document in the database
func CreateDocument(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Fatal(err)
Expand All @@ -48,12 +46,13 @@ func CreateDocument(w http.ResponseWriter, r *http.Request) {

hash := r.FormValue("hash")

regData := registry.RegistrationData{
Name: name,
Passport: passport,
Role: uint(role),
Picture: picData,
Hash: []byte(hash),
regData := &registry.RegistrationData{
Name: name,
Passport: passport,
Role: uint(role),
Picture: picData,
Hash: []byte(hash),
Registered: false,
}

docId, err := userDb.Create(regData)
Expand All @@ -64,6 +63,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(docId)
fmt.Println(docId)
Expand Down Expand Up @@ -93,6 +93,66 @@ func GetDocument(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(data)
}

// UpdateDocument translates the http request to update a document in the database
func UpdateDocument(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing id"))
}

regId := registry.RegistrationId{
Id: []byte(id),
}

err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Fatal(err)
}

name := r.FormValue("name")
passport := r.FormValue("passport")
role, err := strconv.ParseUint(r.FormValue("role"), 10, 32)
if err != nil {
log.Fatal(err)
}
picture, fileHeader, err := r.FormFile("image")
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
hash := r.FormValue("hash")

fmt.Println(fileHeader)

picData := make([]byte, fileHeader.Size)
picture.Read(picData)

regData := &registry.RegistrationData{
Name: name,
Passport: passport,
Role: uint(role),
Picture: picData,
Hash: []byte(hash),
Registered: false,
}

err = userDb.Update(regId, regData)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(regId)
fmt.Println(regId)
}

// DeleteDocument translates the http request to delete a document in the database
func DeleteDocument(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
Expand Down

0 comments on commit 9373978

Please sign in to comment.