Skip to content

Commit

Permalink
feat: endpoint to delete threads from companies and speakers #298
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlourenco committed Nov 19, 2022
1 parent b5b40ff commit 85c7eba
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
25 changes: 25 additions & 0 deletions backend/src/mongodb/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,31 @@ func (c *CompaniesType) UpdateCompanyParticipation(companyID primitive.ObjectID,
return &updatedCompany, nil
}

// DeleteCompanyThread deletes a thread from a company participation
func (c *CompaniesType) DeleteCompanyThread(id, threadID primitive.ObjectID) (*models.Company, error) {
_, err := c.GetCompany(id)
if err != nil {
return nil, err
}

var updatedCompany models.Company

var updateQuery = bson.M{
"$pull": bson.M{
"participations.$.communications": threadID,
},
}

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

if err := c.Collection.FindOneAndUpdate(ctx, bson.M{"participations.communications": threadID}, updateQuery, optionsQuery).Decode(&updatedCompany); err != nil {
return nil, err
}

return &updatedCompany, nil
}

// UpdateCompanyParticipationStatus updates a company's participation status
// related to the current event. This is the method used when one does not want necessarily to follow
// the state machine described on models.ParticipationStatus.
Expand Down
29 changes: 27 additions & 2 deletions backend/src/mongodb/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (uspd *UpdateSpeakerParticipationData) ParseBody(body io.Reader) error {
return nil
}

// UpdateSpeakerParticipation updates a company's participation data
// UpdateSpeakerParticipation updates a speaker's participation data
// related to the current event.
func (s *SpeakersType) UpdateSpeakerParticipation(speakerID primitive.ObjectID, data UpdateSpeakerParticipationData) (*models.Speaker, error) {
ctx := context.Background()
Expand Down Expand Up @@ -851,6 +851,31 @@ func (s *SpeakersType) UpdateSpeakerPublicImage(speakerID primitive.ObjectID, ur
return &updatedSpeaker, nil
}

// DeleteSpeakerThread deletes a thread from a speaker participation
func (s *SpeakersType) DeleteSpeakerThread(id, threadID primitive.ObjectID) (*models.Speaker, error) {
_, err := s.GetSpeaker(id)
if err != nil {
return nil, err
}

var updatedSpeaker models.Speaker

var updateQuery = bson.M{
"$pull": bson.M{
"participations.$.communications": threadID,
},
}

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

if err := s.Collection.FindOneAndUpdate(ctx, bson.M{"participations.communications": threadID}, updateQuery, optionsQuery).Decode(&updatedSpeaker); err != nil {
return nil, err
}

return &updatedSpeaker, nil
}

// AddSpeakerFlightInfo stores a flightInfo on the speaker's participation.
func (s *SpeakersType) AddSpeakerFlightInfo(speakerID primitive.ObjectID, flightInfo primitive.ObjectID) (*models.Speaker, error) {
ctx := context.Background()
Expand Down Expand Up @@ -911,7 +936,7 @@ func (s *SpeakersType) RemoveSpeakerFlightInfo(speakerID primitive.ObjectID, fli
return &updatedSpeaker, nil
}

// AddThread adds a models.Thread to a company's participation's list of communications (related to the current event).
// AddThread adds a models.Thread to a speaker's participation's list of communications (related to the current event).
func (s *SpeakersType) AddThread(speakerID primitive.ObjectID, threadID primitive.ObjectID) (*models.Speaker, error) {
ctx := context.Background()

Expand Down
30 changes: 30 additions & 0 deletions backend/src/router/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,36 @@ func updateCompanyParticipation(w http.ResponseWriter, r *http.Request) {
}
}

func deleteCompanyThread(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
}

company, err := mongodb.Companies.DeleteCompanyThread(id, threadID)
if err != nil {
http.Error(w, "Company or thread not found", http.StatusNotFound)
return
}

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

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

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

defer r.Body.Close()
Expand Down
2 changes: 2 additions & 0 deletions backend/src/router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func InitializeRouter() {
companyRouter.HandleFunc("/{id}/image/public", authCoordinator(setCompanyPublicImage)).Methods("POST")
companyRouter.HandleFunc("/{id}/participation", authMember(addCompanyParticipation)).Methods("POST")
companyRouter.HandleFunc("/{id}/participation", authMember(updateCompanyParticipation)).Methods("PUT")
companyRouter.HandleFunc("/{id}/participation/thread/{threadID}", authMember(deleteCompanyThread)).Methods("DELETE")
companyRouter.HandleFunc("/{id}/participation/status/next", authMember(getCompanyValidSteps)).Methods("GET")
companyRouter.HandleFunc("/{id}/participation/status/{status}", authAdmin(setCompanyStatus)).Methods("PUT")
companyRouter.HandleFunc("/{id}/participation/status/{step}", authMember(stepCompanyStatus)).Methods("POST")
Expand All @@ -183,6 +184,7 @@ func InitializeRouter() {
speakerRouter.HandleFunc("/{id}/unsubscribe", authMember(unsubscribeToSpeaker)).Methods("PUT")
speakerRouter.HandleFunc("/{id}/participation", authMember(addSpeakerParticipation)).Methods("POST")
speakerRouter.HandleFunc("/{id}/participation", authMember(updateSpeakerParticipation)).Methods("PUT")
speakerRouter.HandleFunc("/{id}/participation/thread/{threadID}", authMember(deleteSpeakerThread)).Methods("DELETE")
speakerRouter.HandleFunc("/{id}/participation/status/next", authMember(getSpeakerValidSteps)).Methods("GET")
speakerRouter.HandleFunc("/{id}/participation/status/{step}", authMember(stepSpeakerStatus)).Methods("POST")
speakerRouter.HandleFunc("/{id}/participation/status/{status}", authAdmin(setSpeakerStatus)).Methods("PUT")
Expand Down
30 changes: 30 additions & 0 deletions backend/src/router/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,36 @@ func updateSpeakerParticipation(w http.ResponseWriter, r *http.Request) {
}
}

func deleteSpeakerThread(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
}

speaker, err := mongodb.Speakers.DeleteSpeakerThread(id, threadID)
if err != nil {
http.Error(w, "Speaker or thread not found", http.StatusNotFound)
return
}

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

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

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

params := mux.Vars(r)
Expand Down

0 comments on commit 85c7eba

Please sign in to comment.