Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Daily Thought App
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COM3014-Coursework
Daily Thought App
Commits
cebb00cc
Commit
cebb00cc
authored
2 years ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Add delete method to base repository
parent
a4f11947
No related branches found
Branches containing commit
No related tags found
1 merge request
!25
Add a question management page
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend-services/feed-service/app/repositories/Repository.scala
+9
-0
9 additions, 0 deletions
...d-services/feed-service/app/repositories/Repository.scala
backend-services/feed-service/app/utils/MongoConnection.scala
+23
-1
23 additions, 1 deletion
...end-services/feed-service/app/utils/MongoConnection.scala
with
32 additions
and
1 deletion
backend-services/feed-service/app/repositories/Repository.scala
+
9
−
0
View file @
cebb00cc
...
...
@@ -126,4 +126,13 @@ class Repository[T: ClassTag](databaseName: String, collectionName: String) {
def
updateOne
(
documentId
:
ObjectId
,
updates
:
Seq
[
Bson
])
:
Future
[
Unit
]
=
{
MongoConnection
.
updateOne
[
T
](
collection
,
documentId
,
updates
)
}
/**
* Delete one document from the collection that matches the given ID.
*
* @param documentId The ID of the document to delete.
*/
def
deleteOne
(
documentId
:
ObjectId
)
:
Future
[
Unit
]
=
{
MongoConnection
.
deleteOne
(
collection
,
documentId
)
}
}
This diff is collapsed.
Click to expand it.
backend-services/feed-service/app/utils/MongoConnection.scala
+
23
−
1
View file @
cebb00cc
...
...
@@ -2,7 +2,7 @@ package utils
import
org.mongodb.scala.
{
MongoClient
,
MongoDatabase
,
MongoCollection
}
import
org.mongodb.scala.model.
{
Filters
}
import
com.mongodb.client.result.
{
InsertOneResult
,
UpdateResult
}
import
com.mongodb.client.result.
{
InsertOneResult
,
UpdateResult
,
DeleteResult
}
import
org.bson.conversions.Bson
import
org.bson.types.ObjectId
...
...
@@ -118,4 +118,26 @@ object MongoConnection {
}
})
}
/**
* Delete one document from the collection that matches the given ID.
*
* @param collection The MongoCollection instance the document is in.
* @param documentId The ID of the document to delete.
* @throws RuntimeException if the delete was not acknowledged by the database.
*/
def
deleteOne
[
T
](
collection
:
MongoCollection
[
T
],
documentId
:
ObjectId
)
:
Future
[
Unit
]
=
{
val
filter
:
Bson
=
Filters
.
equal
[
ObjectId
](
"_id"
,
documentId
)
val
futureResult
:
Future
[
DeleteResult
]
=
collection
.
deleteOne
(
filter
).
toFuture
()
futureResult
.
map
[
Unit
]((
result
:
DeleteResult
)
=>
{
if
(!
result
.
wasAcknowledged
())
{
throw
new
RuntimeException
(
"Delete was not acknowledged"
)
}
if
(
result
.
getDeletedCount
()
==
0
)
{
throw
new
RuntimeException
(
"Delete was not acknowledged"
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment