-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: first try to refactor DAO Pattern
- Loading branch information
Showing
52 changed files
with
448 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 25 additions & 17 deletions
42
src/main/scala/dev/mongocamp/driver/mongodb/operation/Search.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,53 @@ | ||
package dev.mongocamp.driver.mongodb.operation | ||
|
||
import dev.mongocamp.driver.mongodb._ | ||
import dev.mongocamp.driver.mongodb.bson.BsonConverter | ||
import dev.mongocamp.driver.mongodb.bson.BsonConverter._ | ||
import dev.mongocamp.driver.mongodb.database.DatabaseProvider | ||
import org.bson.BsonValue | ||
import org.mongodb.scala.bson.ObjectId | ||
import org.mongodb.scala.bson.conversions.Bson | ||
import org.mongodb.scala.model.Filters._ | ||
import org.mongodb.scala.{ AggregateObservable, DistinctObservable, Document, FindObservable, MongoCollection } | ||
import org.mongodb.scala.{ AggregateObservable, DistinctObservable, Document, FindObservable, MongoCollection, Observable } | ||
|
||
import scala.reflect.ClassTag | ||
|
||
abstract class Search[A]()(implicit ct: ClassTag[A]) extends Base[A] { | ||
|
||
protected def coll: MongoCollection[A] | ||
protected def coll: MongoCollection[Document] | ||
|
||
def find( | ||
filter: Bson = Document(), | ||
sort: Bson = Document(), | ||
projection: Bson = Document(), | ||
limit: Int = 0 | ||
): FindObservable[A] = | ||
if (limit > 0) { | ||
coll.find(filter).sort(sort).projection(projection).limit(limit) | ||
} | ||
else { | ||
coll.find(filter).sort(sort).projection(projection) | ||
} | ||
|
||
def findById(oid: ObjectId): FindObservable[A] = find(equal(DatabaseProvider.ObjectIdKey, oid)) | ||
|
||
def find(name: String, value: Any): FindObservable[A] = | ||
): Observable[A] = { | ||
{ | ||
if (limit > 0) { | ||
coll.find(filter).sort(sort).projection(projection).limit(limit) | ||
} | ||
else { | ||
coll.find(filter).sort(sort).projection(projection) | ||
} | ||
}.map(doc => documentToObject[A](doc)) | ||
} | ||
|
||
def findById(oid: ObjectId): Observable[A] = find(equal(DatabaseProvider.ObjectIdKey, oid)) | ||
|
||
def find(name: String, value: Any): Observable[A] = { | ||
find(equal(name, value)) | ||
} | ||
|
||
def distinct[S <: Any](fieldName: String, filter: Bson = Document()): DistinctObservable[BsonValue] = | ||
def distinct[S <: Any](fieldName: String, filter: Bson = Document()): DistinctObservable[BsonValue] = { | ||
coll.distinct[BsonValue](fieldName, filter) | ||
} | ||
|
||
def distinctResult[S <: Any](fieldName: String, filter: Bson = Document()): Seq[S] = | ||
def distinctResult[S <: Any](fieldName: String, filter: Bson = Document()): Seq[S] = { | ||
distinct(fieldName, filter).resultList().map(v => fromBson(v).asInstanceOf[S]) | ||
} | ||
|
||
def findAggregated(pipeline: Seq[Bson], allowDiskUse: Boolean = false): AggregateObservable[A] = | ||
coll.aggregate(pipeline).allowDiskUse(allowDiskUse) | ||
def findAggregated(pipeline: Seq[Bson], allowDiskUse: Boolean = false): Observable[A] = { | ||
coll.aggregate(pipeline).allowDiskUse(allowDiskUse).map(doc => documentToObject[A](doc)).asInstanceOf[AggregateObservable[A]] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.