Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyford committed Jul 23, 2024
1 parent 3ac5799 commit 828d0a1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
22 changes: 22 additions & 0 deletions apps/rule-manager/app/controllers/RulesController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,26 @@ class RulesController(
case Left(error) => BadRequest(s"Invalid request: $error")
}
}

def csvImport() = Action { implicit request =>
request.body.asMultipartFormData match {
case Some(formData) =>
formData.file("file") match {
case Some(file) =>
val rules = RuleManager.csvImport(
file.ref.path.toFile,
formData.dataParts.get("tag") match {
case Some(tag) => tag(0)
case None => ""
},
bucketRuleResource
)
Ok(Json.toJson(rules))
case None => BadRequest("No file found in request")
}
case None => BadRequest("No form data found in request")
}
}
}


2 changes: 1 addition & 1 deletion apps/rule-manager/app/db/DbRuleDraft.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class DbRuleDraft(
id match {
case None =>
throw new Exception(
s"Attempted to make live rule for rule with externalId $externalId, but the rule did not have an id"
s"Attempted to make live rule for rule with externalId ${externalId}, but the rule did not have an id"
)
case Some(_) =>
DbRuleLive(
Expand Down
60 changes: 50 additions & 10 deletions apps/rule-manager/app/service/RuleManager.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package service

import com.gu.typerighter.lib.Loggable
import com.gu.typerighter.model.{
CheckerRule,
CheckerRuleResource,
DictionaryRule,
LTRule,
LTRuleCore,
LTRuleXML,
RegexRule,
WordTag
}
import com.gu.typerighter.model.{CheckerRule, CheckerRuleResource, DictionaryRule, LTRule, LTRuleCore, LTRuleXML, RegexRule, WordTag}
import com.gu.typerighter.rules.BucketRuleResource
import db.{DbRuleDraft, DbRuleLive, RuleTagDraft, RuleTagLive, Tag, Tags}
import db.DbRuleDraft.autoSession
import model.{DictionaryForm, LTRuleCoreForm, LTRuleXMLForm, PaginatedResponse, RegexRuleForm}
import play.api.data.FormError
import play.api.libs.json.{Json, OWrites}
import scalikejdbc.DBSession
import com.github.tototoshi.csv.CSVReader
import java.io.File

object AllRuleData {
implicit val writes: OWrites[AllRuleData] = Json.writes[AllRuleData]
Expand All @@ -32,6 +25,53 @@ case class AllRuleData(
)

object RuleManager extends Loggable {
def csvImport(toFile: File, tagName: String, bucketRuleResource: BucketRuleResource) = {
val reader = CSVReader.open(toFile)
val rules = reader.all()

val initialRuleOrder = DbRuleDraft.getLatestRuleOrder() + 1

val draftRules = rules.zipWithIndex.map { case (rule, index) =>
val pattern = rule(0)
val replacement = rule(1)
val description = rule(2)

DbRuleDraft.withUser(
id = None,
ruleType = RuleType.regex,
pattern = Some(pattern),
category = Some("Imported from CSV"),
description = Some(description),
ignore = false,
replacement = Some(replacement),
user = "CSV Import",
ruleOrder = initialRuleOrder + index,
)
}
val ruleIds = DbRuleDraft.batchInsert(draftRules, true)

// Apply tag
val allTags = Tags.findAllWithRuleCounts()
allTags.find(tag => tag.name == tagName).map(_.id) match {
case Some(tagId) => DbRuleDraft.batchUpdate(ruleIds, None, Option(List(tagId.getOrElse(-1))), "CSV Import")
case None => log.error(s"Tag $tagName not found")
}

val rulesWithIds = DbRuleDraft.findRules(ruleIds)
val liveRulesWithIds = rulesWithIds.map(_.toLive("Imported from CSV", true))

// log liveRulesWithIds to the console
println("TESTING")
println(liveRulesWithIds)

// TODO: FAILING HERE
DbRuleLive.batchInsert(liveRulesWithIds)

publishLiveRules(bucketRuleResource)

liveRulesWithIds.size
}

object RuleType {
val regex = "regex"
val languageToolXML = "languageToolXML"
Expand Down
2 changes: 2 additions & 0 deletions apps/rule-manager/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ GET /api/rules/batch/:ids controllers.RulesController.getRules(ids
+nocsrf
POST /api/rules/batch controllers.RulesController.batchUpdate()
+nocsrf
POST /api/rules/csv-import controllers.RulesController.csvImport()
+nocsrf
GET /api/rules/:id controllers.RulesController.get(id: Int)
+nocsrf
POST /api/rules/:id controllers.RulesController.update(id: Int)
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ val ruleManager = playProject(
"org.scalikejdbc" %% "scalikejdbc-test" % scalikejdbcVersion % Test,
"org.scalikejdbc" %% "scalikejdbc-syntax-support-macro" % scalikejdbcVersion,
"com.gu" %% "editorial-permissions-client" % "2.14",
"com.github.tototoshi" %% "scala-csv" % "2.0.0",
),
libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always
)
Expand Down

0 comments on commit 828d0a1

Please sign in to comment.