Skip to content

Commit

Permalink
v0.1: final clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tsbonev committed Sep 27, 2018
1 parent da85f41 commit 69dff7e
Show file tree
Hide file tree
Showing 29 changed files with 290 additions and 246 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repositories {
}

buildscript {
ext.kotlinVersion = "1.2.70"
ext.kotlinVersion = "1.2.71"
ext.nitriteVersion = "3.1.0"

repositories {
Expand All @@ -19,7 +19,6 @@ buildscript {
}
}


allprojects {

apply plugin: "jacoco"
Expand Down
2 changes: 0 additions & 2 deletions nharker-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
group 'com.tsbonev.nharker'

repositories {
mavenCentral()
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite,
private val collectionName: String = "Article_synonyms",
private val globalMapId: String = "Global_synonym_map")
: ArticleSynonymProvider {
/**
* Retrieve the repository on every request.
*/

private val coll: NitriteCollection
get() = nitriteDb.getCollection(collectionName)

Expand All @@ -33,7 +31,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite,
override fun addSynonym(articleSynonym: String, article: Article): String {
val map = getSynonymMap().toMutableMap()

if(map[articleSynonym] != null) throw SynonymAlreadyTakenException()
if (map[articleSynonym] != null) throw SynonymAlreadyTakenException()

map[articleSynonym] = article.linkTitle

Expand All @@ -44,7 +42,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite,
override fun removeSynonym(articleSynonym: String): String {
val map = getSynonymMap().toMutableMap()

if(map[articleSynonym] == null) throw SynonymNotFoundException()
if (map[articleSynonym] == null) throw SynonymNotFoundException()

map.remove(articleSynonym)

Expand All @@ -59,7 +57,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite,
* @param synonymMap The map whose values to save.
* @return A Document of the map.
*/
private fun updateOrCreateMap(synonymMap: Map<String, String>): Document{
private fun updateOrCreateMap(synonymMap: Map<String, String>): Document {
val mapDocument = coll.find("globalId" eq globalMapId).firstOrNull()
?: Document.createDocument("globalId", globalMapId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ import java.util.Optional
class NitriteArticles(private val nitriteDb: Nitrite,
private val collectionName: String = "Articles",
private val entryLinker: EntryLinker,
private val clock: Clock = Clock.systemUTC()) : Articles {
private val clock: Clock = Clock.systemUTC())
: Articles {

/**
* Retrieve the repository on every request.
*/
private val coll: ObjectRepository<Article>
get() = nitriteDb.getRepository(collectionName, Article::class.java)

@Throws(ArticleTitleTakenException::class)
override fun create(articleRequest: ArticleRequest): Article {
val article = Article(
NitriteId.newId().toString(),
articleRequest.title.toLinkTitle(),
articleRequest.title,
articleRequest.fullTitle.toLinkTitle(),
articleRequest.fullTitle,
LocalDateTime.now(clock)
)

if(coll.find(Article::linkTitle eq article.linkTitle).firstOrNull() != null)
if (coll.find(Article::linkTitle eq article.linkTitle).firstOrNull() != null)
throw ArticleTitleTakenException()

coll.insert(article)
Expand All @@ -46,46 +43,60 @@ class NitriteArticles(private val nitriteDb: Nitrite,
}

override fun getById(articleId: String): Optional<Article> {
val article = coll.find(Article::id eq articleId).firstOrNull() ?: return Optional.empty()
val article = coll.find(Article::id eq articleId).firstOrNull()
?: return Optional.empty()

return Optional.of(article)
}

override fun delete(articleId: String): Article {
val article = findByIdOrThrow(articleId)

coll.remove(article)
return article
}

override fun appendEntry(articleId: String, entry: Entry): Entry {
val article = findByIdOrThrow(articleId)

if(article.entries.containsKey(entry.id)) throw EntryAlreadyInArticleException()
if (article.entries.containsKey(entry.id)) throw EntryAlreadyInArticleException()

handleArticleLinks(article, entry, true)

val updatedArticle = article
.copy(entries = article.entries.append(entry.id))
.copy(entries = article.entries
.append(entry.id))

coll.update(updatedArticle)

return entry
}

override fun removeEntry(articleId: String, entry: Entry): Entry {
val article = findByIdOrThrow(articleId)

if(!article.entries.contains(entry.id)) throw EntryNotInArticleException()
if (!article.entries.contains(entry.id)) throw EntryNotInArticleException()

handleArticleLinks(article, entry, false)

val updatedArticle = article.copy(entries = article.entries.subtract(entry.id))
coll.update(updatedArticle)
val updatedArticle = article
.copy(entries = article.entries
.subtract(entry.id))

coll.update(updatedArticle)
return entry
}

override fun switchEntries(articleId: String, first: Entry, second: Entry): Article {
val article = findByIdOrThrow(articleId)

return try {
val updatedArticle = article.copy(entries = article.entries.switch(first.id, second.id))
val updatedArticle = article
.copy(entries = article.entries
.switch(first.id, second.id))

coll.update(updatedArticle)
updatedArticle
}catch (ex: ElementNotInMapException){
} catch (ex: ElementNotInMapException) {
throw EntryNotInArticleException()
}
}
Expand All @@ -111,7 +122,7 @@ class NitriteArticles(private val nitriteDb: Nitrite,
override fun getArticleTitles(linkTitleList: Set<String>): List<String> {
val fullTitleList = mutableListOf<String>()

linkTitleList.forEach{
linkTitleList.forEach {
val article = coll.find(Article::linkTitle eq it)
.project(ArticleFullTitle::class.java).firstOrNull() ?: return@forEach
fullTitleList.add(article.fullTitle)
Expand All @@ -123,7 +134,7 @@ class NitriteArticles(private val nitriteDb: Nitrite,
/**
* Finds an article by id or throws an exception.
*/
private fun findByIdOrThrow(articleId: String): Article{
private fun findByIdOrThrow(articleId: String): Article {
return coll.find(Article::id eq articleId).firstOrNull() ?: throw ArticleNotFoundException()
}

Expand All @@ -132,7 +143,7 @@ class NitriteArticles(private val nitriteDb: Nitrite,
*
* @return List of article link titles.
*/
private fun getArticleLinkTitles(): List<String>{
private fun getArticleLinkTitles(): List<String> {
val projectedArticleTitles = coll
.find()
.project(ArticleLinkTitle::class.java)
Expand All @@ -153,17 +164,17 @@ class NitriteArticles(private val nitriteDb: Nitrite,
* @param entry The entry whose links are up for modification.
* @param adding Whether or not the links should be added or removed.
*/
private fun handleArticleLinks(article: Article, entry: Entry, adding: Boolean){
private fun handleArticleLinks(article: Article, entry: Entry, adding: Boolean) {
val entryLinks = entryLinker.findArticleLinks(
entry,
getArticleLinkTitles()
)

if(adding){
if (adding) {
entryLinks.forEach {
article.links.addLink(it)
}
}else{
} else {
entryLinks.forEach {
article.links.removeLink(it)
}
Expand Down
Loading

0 comments on commit 69dff7e

Please sign in to comment.