Created
March 22, 2021 09:51
-
-
Save KangDroid/701b6529bf402c9458c040019e623847 to your computer and use it in GitHub Desktop.
Template Repository for User[for now, only file-related function implemented]
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
package com.example.demo.domain | |
import com.mongodb.client.result.UpdateResult | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.data.mongodb.core.MongoTemplate | |
import org.springframework.data.mongodb.core.aggregation.* | |
import org.springframework.data.mongodb.core.query.Criteria | |
import org.springframework.data.mongodb.core.query.Query | |
import org.springframework.data.mongodb.core.query.Update | |
import org.springframework.stereotype.Repository | |
@Repository | |
class UserTemplateRepository { | |
@Autowired | |
private lateinit var mongoTemplate: MongoTemplate | |
fun clearAll() { | |
mongoTemplate.remove(Query(), User::class.java) | |
} | |
fun save(user: User): User { | |
return mongoTemplate.save(user) | |
} | |
fun innerFileListSearch( | |
inputUserName: String, | |
inputSearchKey: String, | |
inputSearchValue: String | |
): AggregationResults<User> { | |
// Match user name [Filter username first] | |
val userNameMatchCriteria: Criteria = Criteria.where("userName").`is`(inputUserName) | |
val matchOperation: MatchOperation = Aggregation.match(userNameMatchCriteria) | |
// Unwind | |
val unwindOperation: UnwindOperation = Aggregation.unwind("fileList") | |
// Match Token [Filter token] | |
val fileTokenMatchCriteria: Criteria = Criteria.where(inputSearchKey).`is`(inputSearchValue) | |
val fileTokenMatchOperation: MatchOperation = Aggregation.match(fileTokenMatchCriteria) | |
// Group | |
val groupOperation: GroupOperation = Aggregation.group("id") | |
.push( | |
"fileList" | |
).`as`("fileList") | |
return mongoTemplate.aggregate( | |
Aggregation.newAggregation( | |
matchOperation, | |
unwindOperation, | |
fileTokenMatchOperation, | |
groupOperation | |
), | |
User::class.java, | |
User::class.java | |
) | |
} | |
fun findByPrevToken(inputUserName: String, inputPrevToken: String): AggregationResults<User>? { | |
return innerFileListSearch(inputUserName, "fileList.prevToken", inputPrevToken) | |
} | |
fun findByToken(inputUserName: String, inputToken: String): AggregationResults<User>? { | |
return innerFileListSearch(inputUserName, "fileList.token", inputToken) | |
} | |
fun findByUserName(inputUserName: String): User? { | |
val findNameQuery: Query = Query() | |
findNameQuery.addCriteria( | |
Criteria.where("userName").`is`(inputUserName) | |
) | |
return mongoTemplate.findOne(findNameQuery, User::class.java) | |
} | |
fun deleteByToken(inputUserName: String, inputToken: String): UpdateResult { | |
val updateQuery: Query = Query().apply { | |
addCriteria( | |
Criteria.where("userName").`is`(inputUserName) | |
) | |
} | |
val update: Update = Update().pull( | |
"fileList", | |
Query.query(Criteria.where("token").`is`(inputToken)) | |
) | |
return mongoTemplate.updateMulti(updateQuery, update, User::class.java) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment