Skip to content

Commit

Permalink
feat: delete thread from meeting #298
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlourenco committed Nov 8, 2022
1 parent 7307dd0 commit 46c1435
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 4 deletions.
34 changes: 34 additions & 0 deletions backend/src/mongodb/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,37 @@ func (m *MeetingsType) FindThread(threadID primitive.ObjectID) (*models.Meeting,

return nil, nil
}

func (m *MeetingsType) DeleteMeetingThread(id, threadID primitive.ObjectID) (*models.Meeting, error) {
meeting, err := m.GetMeeting(id)
if err != nil {
return nil, err
} else if meet, err := m.FindThread(threadID); err != nil || meet == nil {
return meet, err
}

var updatedMeeting models.Meeting
var communications []primitive.ObjectID

for i, s := range meeting.Communications {
if s == threadID {
communications = append(meeting.Communications[:i], meeting.Communications[i+1:]...)
break
}
}

var updateQuery = bson.M{
"$set": bson.M{
"communications": communications,
},
}

var optionsQuery = options.FindOneAndUpdate()
optionsQuery.SetReturnDocument(options.After)

if err := m.Collection.FindOneAndUpdate(ctx, bson.M{"_id": id}, updateQuery, optionsQuery).Decode(&updatedMeeting); err != nil {
return nil, err
}

return &updatedMeeting, nil
}
2 changes: 1 addition & 1 deletion backend/src/mongodb/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

// ThreadsType contains important database information on Meetings
// ThreadsType contains important database information on Threads
type ThreadsType struct {
Collection *mongo.Collection
}
Expand Down
1 change: 1 addition & 0 deletions backend/src/router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func InitializeRouter() {
meetingRouter.HandleFunc("/{id}", authCoordinator(deleteMeeting)).Methods("DELETE")
meetingRouter.HandleFunc("/{id}", authCoordinator(updateMeeting)).Methods("PUT")
meetingRouter.HandleFunc("/{id}/thread", authMember(addMeetingThread)).Methods("POST")
meetingRouter.HandleFunc("/{id}/thread/{threadID}", authMember(deleteMeetingThread)).Methods("DELETE")
meetingRouter.HandleFunc("/{id}/minute", authMember(uploadMeetingMinute)).Methods("POST")
meetingRouter.HandleFunc("/{id}/minute", authMember(deleteMeetingMinute)).Methods("DELETE")
meetingRouter.HandleFunc("/{id}/participants", authMember(addMeetingParticipant)).Methods("POST")
Expand Down
31 changes: 29 additions & 2 deletions backend/src/router/meetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ func addMeetingThread(w http.ResponseWriter, r *http.Request) {
}
}

func deleteMeetingThread(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

params := mux.Vars(r)
id, _ := primitive.ObjectIDFromHex(params["id"])
threadID, _ := primitive.ObjectIDFromHex(params["threadID"])

_, ok := r.Context().Value(credentialsKey).(models.AuthorizationCredentials)

if !ok {
http.Error(w, "Could not parse credentials", http.StatusBadRequest)
return
}

// Delete thread and posts (comments) associated to it
if _, err := mongodb.Threads.DeleteThread(threadID); err != nil {
http.Error(w, "Thread not found", http.StatusNotFound)
return
}

meeting, err := mongodb.Meetings.DeleteMeetingThread(id, threadID)
if err != nil {
http.Error(w, "Meeting or thread not found", http.StatusNotFound)
return
}

json.NewEncoder(w).Encode(meeting)
}

func getMeetings(w http.ResponseWriter, r *http.Request) {

urlQuery := r.URL.Query()
Expand Down Expand Up @@ -228,8 +257,6 @@ func getMeetings(w http.ResponseWriter, r *http.Request) {

func updateMeeting(w http.ResponseWriter, r *http.Request) {

//TODO: Update Participants as well

defer r.Body.Close()

params := mux.Vars(r)
Expand Down
2 changes: 1 addition & 1 deletion backend/static/swagger.json

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions backend/swagger/meetings-id-thread-threadid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"delete": {
"tags": [
"meetings",
"threads"
],
"summary": "Deletes thread from a meeting",
"operationId": "deleteMeetingThread",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"security": [
{
"Bearer": []
}
],
"parameters": [
{
"in": "path",
"name": "id",
"description": "ID of the meeting",
"required": true,
"type": "string"
},
{
"in": "path",
"name": "threadID",
"description": "ID of the thread",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Updated meeting",
"schema": {
"$ref": "./models/meeting.json"
}
},
"400": {
"description": "Invalid payload, or invalid credentials"
},
"404": {
"description": "Meeting not found"
},
"401": {
"description": "Unauthorized"
}
}
}
}
3 changes: 3 additions & 0 deletions backend/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@
"/meetings/{id}/thread": {
"$ref": "./meetings-id-thread.json"
},
"/meetings/{id}/thread/{threadID}": {
"$ref": "./meetings-id-thread-threadid.json"
},
"/meetings/{id}/participants": {
"$ref": "./meetings-id-participants.json"
}
Expand Down

0 comments on commit 46c1435

Please sign in to comment.