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
4
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
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
31c0c0c8
Commit
31c0c0c8
authored
1 year ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Add a base repository
parent
ce0ba034
No related branches found
Branches containing commit
No related tags found
1 merge request
!14
Add endpoints to manage Dailies
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend-services/feed-service/app/repositories/Repository.scala
+102
-0
102 additions, 0 deletions
...d-services/feed-service/app/repositories/Repository.scala
with
102 additions
and
0 deletions
backend-services/feed-service/app/repositories/Repository.scala
0 → 100644
+
102
−
0
View file @
31c0c0c8
package
repositories
import
utils.ConfigHelper
import
utils.MongoConnection
import
org.mongodb.scala.
{
MongoClient
,
MongoDatabase
,
MongoCollection
}
import
org.mongodb.scala.model.Filters
import
org.bson.types.ObjectId
import
org.bson.conversions.Bson
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.concurrent.
{
Future
,
Await
}
import
scala.concurrent.duration._
import
scala.reflect.ClassTag
/**
* The base repository class that provides basic CRUD operations for a MongoDB collection.
*
* @tparam T The type of objects stored in the collection.
* @param databaseName The name of the MongoDB database to use.
* @param collectionName The name of the MongoDB collection to use.
*/
class
Repository
[
T:
ClassTag
](
databaseName
:
String
,
collectionName
:
String
)
{
/**
* Returns a reference to a MongoDB collection within a database.
* Once Future completes, collection reference is returned.
*
* @return a MongoCollection[T] object representing the collection.
* @throws TimeoutException if the Future doesn't complete within the 3 second timeout.
*/
protected
val
collection
:
MongoCollection
[
T
]
=
{
val
mongoUri
:
String
=
ConfigHelper
.
getString
(
"mongodb.uri"
)
val
futureCollection
:
Future
[
MongoCollection
[
T
]]
=
for
{
client
:
MongoClient
<-
MongoConnection.getClient
(
mongoUri
)
database:
MongoDatabase
<-
MongoConnection.getDatabase
(
client
,
databaseName
)
collection
:
MongoCollection
[
T
]
<-
MongoConnection
.
getCollection
[
T
](
database
,
collectionName
)
}
yield
collection
:
MongoCollection
[
T
]
Await
.
result
[
MongoCollection
[
T
]](
futureCollection
,
3.
seconds
)
}
/**
* Gets all the records in the collection.
*
* @return A Future containing a sequence of the collection's records.
*/
def
getAll
()
:
Future
[
Seq
[
T
]]
=
{
MongoConnection
.
find
[
T
](
collection
)
}
/**
* Gets a record with the given ID in the collection.
*
* @return A Future containing a sequence of matching documents.
*/
def
getById
(
id
:
ObjectId
)
:
Future
[
T
]
=
{
val
filter
:
Bson
=
Filters
.
equal
[
ObjectId
](
"_id"
,
id
)
MongoConnection
.
find
[
T
](
collection
,
filter
).
map
(
_
.
head
)
}
/**
* Retrieves records from the collection that have the specified value for the given field name.
*
* @tparam V The type of the query value.
* @param field_name The name of the field to search for.
* @param value The value to search for.
*
* @return A Future containing a sequence of matching documents.
*/
def
getByValue
[
V
](
field_name
:
String
,
value
:
V
)
:
Future
[
Seq
[
T
]]
=
{
val
filter
:
Bson
=
Filters
.
equal
[
V
](
field_name
,
value
)
MongoConnection
.
find
[
T
](
collection
,
filter
)
}
/**
* Retrieves records from the collection that contain any of the specified values for the given field name.
*
* @tparam V The type of the query values.
* @param field_name The name of the field to search for.
* @param value A sequence of values to search for.
*
* @return A Future containing a sequence of matching documents.
*/
def
getByValues
[
V
](
field_name
:
String
,
values
:
Seq
[
V
])
:
Future
[
Seq
[
T
]]
=
{
val
filter
:
Bson
=
Filters
.
in
[
V
](
field_name
,
values
:
_
*
)
MongoConnection
.
find
[
T
](
collection
,
filter
)
}
/**
* Updates a document in the collection.
*
* @param documentId The ID of the document to update.
* @param updates A sequence of Bson documents defining the updates.
*/
def
updateOne
(
documentId
:
ObjectId
,
updates
:
Seq
[
Bson
])
:
Future
[
Unit
]
=
{
MongoConnection
.
updateOne
[
T
](
collection
,
documentId
,
updates
)
}
}
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