From 94714dbe707f3db2cec0bf2bbcce5d492dcdb925 Mon Sep 17 00:00:00 2001 From: hongwei1 Date: Wed, 5 Sep 2018 16:17:48 +0200 Subject: [PATCH] get rid of book in api code --- .../code/api/APIBuilder/APIBuilder.scala | 14 +- .../code/api/APIBuilder/APIBuilderModel.scala | 26 +- .../code/api/APIBuilder/apisResource.json | 28 +- .../code/api/APIBuilder/modelSource.json | 4 +- .../api/builder/APIMethods_APIBuilder.scala | 263 +++--------------- .../api/builder/JsonFactory_APIBuilder.scala | 66 +---- .../api/APIBuilder/APIBuilderModelTest.scala | 40 +-- .../code/api/APIBuilder/modelSource.json | 8 - 8 files changed, 112 insertions(+), 337 deletions(-) delete mode 100644 src/test/scala/code/api/APIBuilder/modelSource.json diff --git a/src/main/scala/code/api/APIBuilder/APIBuilder.scala b/src/main/scala/code/api/APIBuilder/APIBuilder.scala index 1b2ee1805..5ba6755a2 100644 --- a/src/main/scala/code/api/APIBuilder/APIBuilder.scala +++ b/src/main/scala/code/api/APIBuilder/APIBuilder.scala @@ -45,18 +45,18 @@ object APIBuilder val deleteSingleApiJValue = resourceDocsJObject.filter(_.\("request_verb") == JString("DELETE")).head val getSingleApiResponseBody: JValue = getSingleApiJValue \ "success_response_body" - //"book" + //"template" val modelName = getModelName(getSingleApiResponseBody) - //All the fields in the book object. + //All the fields in the template object. val modelFieldsJValue: JValue = getSingleApiResponseBody \ modelName - //BOOK + //TEMPLATE val modelNameUpperCase = modelName.toUpperCase - //book + //template val modelNameLowerCase = modelName.toLowerCase - //Book + //Template val modelNameCapitalized = modelNameLowerCase.capitalize - //MappedBook_123123 + //MappedTemplate_123123 val modelMappedName = s"Mapped${modelNameCapitalized}_"+Math.abs(scala.util.Random.nextLong()) val modelTypeName = Type.Name(modelMappedName) val modelTermName = Term.Name(modelMappedName) @@ -292,7 +292,7 @@ object APIBuilder val modelCaseClassParams: List[Term.Param] = getModelCaseClassParams(modelFieldsNames, modelFieldsTypes, modelFieldsDefaultValues) //def createTemplate(createTemplateJson: CreateTemplateJson) = Full( - // MappedBook_6099750036365020434.create + // MappedTemplate_6099750036365020434.create // .mTemplateId(UUID.randomUUID().toString) // .mAuthor(createTemplateJson.author) // .mPages(createTemplateJson.pages) diff --git a/src/main/scala/code/api/APIBuilder/APIBuilderModel.scala b/src/main/scala/code/api/APIBuilder/APIBuilderModel.scala index 22ad26df2..36255d1ae 100644 --- a/src/main/scala/code/api/APIBuilder/APIBuilderModel.scala +++ b/src/main/scala/code/api/APIBuilder/APIBuilderModel.scala @@ -192,7 +192,7 @@ object APIBuilderModel } //def createTemplate(createTemplateJson: CreateTemplateJson) = - // Full(MappedBook_2145180497484573086.create + // Full(MappedTemplate_2145180497484573086.create // .mTemplateId(UUID.randomUUID().toString) // .mAuthor(createTemplateJson.author) // .mPages(createTemplateJson.pages) @@ -260,31 +260,31 @@ object APIBuilderModel val jsonJValueFromFile: JValue = APIUtil.getJValueFromFile("src/main/scala/code/api/APIBuilder/modelSource.json") - //"/books" + //"/templates" val apiUrl= getApiUrl(jsonJValueFromFile) - //"book" + //"template" val modelName = getModelName(jsonJValueFromFile) - //BOOK + //TEMPLATE val modelNameUpperCase = modelName.toUpperCase - //book + //template val modelNameLowerCase = modelName.toLowerCase - //Book + //Template val modelNameCapitalized = modelNameLowerCase.capitalize val modelFieldsJValue: JValue = jsonJValueFromFile \ modelName - //MappedBook_6285959801482269169 + //MappedTemplate_6285959801482269169 val modelMappedName = s"Mapped${modelNameCapitalized}_"+Math.abs(scala.util.Random.nextLong()) val modelTypeName: Type.Name = Type.Name(modelMappedName) val modelTermName = Term.Name(modelMappedName) val modelInit = Init.apply(Type.Name(modelMappedName), Term.Name(modelMappedName), Nil) - //getApiUrlVal: scala.meta.Lit.StrincreateModelJsonMethodField = "/books" + //getApiUrlVal: scala.meta.Lit.StrincreateModelJsonMethodField = "/templates" val getApiUrlVal: Lit.String = Lit.String(s"$apiUrl") - //getSingleApiUrlVal: scala.meta.Lit.String = "/books/BOOK_ID" + //getSingleApiUrlVal: scala.meta.Lit.String = "/templates/TEMPLATE_ID" val getSingleApiUrlVal = Lit.String(s"$apiUrl/${modelNameUpperCase}_ID") - //createSingleApiUrlVal: scala.meta.Lit.String = "/books" + //createSingleApiUrlVal: scala.meta.Lit.String = "/templates" val createSingleApiUrlVal = Lit.String(s"$apiUrl") - //deleteSingleApiUrlVal: scala.meta.Lit.String = "/books/BOOK_ID" + //deleteSingleApiUrlVal: scala.meta.Lit.String = "/templates/TEMPLATE_ID" val deleteSingleApiUrlVal = Lit.String(s"$apiUrl/${modelNameUpperCase}_ID") //TODO, escape issue:return the space, I added quotes in the end: allSourceCode.syntax.replaceAll(""" :: ""","""" :: """") //from "/my/template" --> "my :: template" @@ -315,9 +315,9 @@ object APIBuilderModel val deletePartialFuncTermName = Term.Name(s"delete${modelNameCapitalized}") val deletePartialFuncName = Pat.Var(deletePartialFuncTermName) - //implementedApiDefBody: scala.meta.Term.Name = `getBooks :: getBook :: createBook :: deleteBook :: Nil` + //implementedApiDefBody: scala.meta.Term.Name = `getTemplates :: getTemplate :: createTemplate :: deleteTemplate :: Nil` val implementedApiDefBody= Term.Name(s"${getPartialFuncTermName.value} :: ${getSinglePartialFuncTermName.value} :: ${createPartialFuncTermName.value} :: ${deletePartialFuncTermName.value} :: Nil") - //implementedApisDef: scala.meta.Defn.Def = def endpointsOfBuilderAPI = `getBooks :: getBook :: createBook :: deleteBook :: Nil` + //implementedApisDef: scala.meta.Defn.Def = def endpointsOfBuilderAPI = `getTemplates :: getTemplate :: createTemplate :: deleteTemplate :: Nil` val implementedApisDef: Defn.Def = q"""def endpointsOfBuilderAPI = $implementedApiDefBody""" val getTemplatesResourceCode: Term.ApplyInfix =q""" diff --git a/src/main/scala/code/api/APIBuilder/apisResource.json b/src/main/scala/code/api/APIBuilder/apisResource.json index ea8eb1a5a..640127c3a 100644 --- a/src/main/scala/code/api/APIBuilder/apisResource.json +++ b/src/main/scala/code/api/APIBuilder/apisResource.json @@ -1,13 +1,13 @@ { "resource_docs": [ { - "summary": "Get Books", + "summary": "Get Templates", "request_verb": "GET", - "description": "Return All my books. Authentication is Mandatory. ", - "request_url": "/my/books", + "description": "Return All my templates. Authentication is Mandatory. ", + "request_url": "/my/templates", "example_request_body": {}, "success_response_body": { - "books": [ + "templates": [ { "author": "Chinua Achebe", "pages": 209, @@ -22,13 +22,13 @@ } }, { - "summary": "Get Book", + "summary": "Get Template", "request_verb": "GET", - "description": "Return one book. Authentication is Mandatory. ", - "request_url": "/my/books/BOOK_ID", + "description": "Return one template. Authentication is Mandatory. ", + "request_url": "/my/templates/TEMPLATE_ID", "example_request_body": {}, "success_response_body": { - "book": { + "template": { "author": "Chinua Achebe", "pages": 209, "points": 1.3 @@ -36,10 +36,10 @@ } }, { - "summary": "Create Book", + "summary": "Create Template", "request_verb": "POST", - "description": "Create one book, Authentication is Mandatory ", - "request_url": "/my/books", + "description": "Create one template, Authentication is Mandatory ", + "request_url": "/my/templates", "example_request_body": { "author": "Chinua Achebe", "pages": 209, @@ -52,10 +52,10 @@ } }, { - "summary": "Delete Book", + "summary": "Delete Template", "request_verb": "DELETE", - "description": "Delete a book by id. Authentication is Mandatory. ", - "request_url": "/my/books/BOOK_ID", + "description": "Delete a template by id. Authentication is Mandatory. ", + "request_url": "/my/templates/TEMPLATE_ID", "example_request_body": {}, "success_response_body": true } diff --git a/src/main/scala/code/api/APIBuilder/modelSource.json b/src/main/scala/code/api/APIBuilder/modelSource.json index eb765391f..53be29517 100644 --- a/src/main/scala/code/api/APIBuilder/modelSource.json +++ b/src/main/scala/code/api/APIBuilder/modelSource.json @@ -1,6 +1,6 @@ { - "request_url": "/books", - "book": { + "request_url": "/templates", + "template": { "author": "Chinua Achebe", "pages": 209, "points": 1.3 diff --git a/src/main/scala/code/api/builder/APIMethods_APIBuilder.scala b/src/main/scala/code/api/builder/APIMethods_APIBuilder.scala index 1eb6fe317..5b872147a 100644 --- a/src/main/scala/code/api/builder/APIMethods_APIBuilder.scala +++ b/src/main/scala/code/api/builder/APIMethods_APIBuilder.scala @@ -1,31 +1,4 @@ -/** -Open Bank Project - API -Copyright (C) 2011-2018, TESOBE Ltd - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . - -Email: contact@tesobe.com -TESOBE Ltd -Osloerstrasse 16/17 -Berlin 13359, Germany - - This product includes software developed at - TESOBE (http://www.tesobe.com/) - */ - package code.api.builder - import java.util.UUID import code.api.builder.JsonFactory_APIBuilder._ import code.api.util.APIUtil._ @@ -40,226 +13,76 @@ import net.liftweb.mapper.By import net.liftweb.util.Helpers.tryo import scala.collection.immutable.Nil import scala.collection.mutable.ArrayBuffer - -trait APIMethods_APIBuilder -{ - self: RestHelper => - - val ImplementationsBuilderAPI = new Object() - { +trait APIMethods_APIBuilder { self: RestHelper => + val ImplementationsBuilderAPI = new Object() { val apiVersion: ApiVersion = ApiVersion.apiBuilder val resourceDocs = ArrayBuffer[ResourceDoc]() val apiRelations = ArrayBuffer[ApiRelation]() val codeContext = CodeContext(resourceDocs, apiRelations) - val TemplateNotFound = "OBP-31001: Template not found. Please specify a valid value for TEMPLATE_ID." - implicit val formats = net.liftweb.json.DefaultFormats - - def endpointsOfBuilderAPI = getTemplatesFromFile :: getTemplate :: createTemplate :: getTemplates :: deleteTemplate :: Nil - - resourceDocs += ResourceDoc( - getTemplatesFromFile, - apiVersion, - "getTemplatesFromFile", - "GET", - "/file/templates", - "Get Templates From File", - "Return all templates in file, Authentication is Mandatory", - emptyObjectJson, - templatesJson, - List(UserNotLoggedIn, UnknownError), - Catalogs(notCore, notPSD2, notOBWG), - apiTagApiBuilder :: Nil - ) - lazy val getTemplatesFromFile: OBPEndpoint ={ - case ("file" :: "templates" :: Nil) JsonGet req => - cc => - { - for{ - u <- cc.user ?~ UserNotLoggedIn; - jsonStringFromFile = scala.io.Source.fromFile("src/main/scala/code/api/APIBuilder/apisResource.json").mkString; - jsonJValueFromFile = json.parse(jsonStringFromFile); - resourceDocsJObject = jsonJValueFromFile.\("resource_docs").children.asInstanceOf[List[JObject]]; - getMethodJValue = resourceDocsJObject.filter(jObject => jObject.\("request_verb") == JString("GET") && !jObject.\("request_url").asInstanceOf[JString].values.contains("_ID")).head; - jsonObject = getMethodJValue \ "success_response_body" - }yield{ - successJsonResponse(jsonObject) - } - } - } - - resourceDocs += ResourceDoc( - getTemplates, - apiVersion, - "getTemplates", - "GET", - "/templates", - "Get All Templates.", - "Return all templates, Authentication is Mandatory", - emptyObjectJson, - templatesJson, - List(UserNotLoggedIn, UnknownError), - Catalogs(notCore, notPSD2, notOBWG), - apiTagApiBuilder :: Nil - ) - lazy val getTemplates: OBPEndpoint ={ + val TemplateNotFound = "OBP-31001: Template not found. Please specify a valid value for TEMPLATE_ID." + def endpointsOfBuilderAPI = getTemplates :: getTemplate :: createTemplate :: deleteTemplate :: Nil + resourceDocs += ResourceDoc(getTemplates, apiVersion, "getTemplates", "GET", "/templates", "Get Templates", "Return All Templates", emptyObjectJson, templatesJson, List(UserNotLoggedIn, UnknownError), Catalogs(notCore, notPSD2, notOBWG), apiTagApiBuilder :: Nil) + lazy val getTemplates: OBPEndpoint = { case ("templates" :: Nil) JsonGet req => - cc => - { - for{ - u <- cc.user ?~ UserNotLoggedIn - templates <- APIBuilder_Connector.getTemplates - templatesJson = JsonFactory_APIBuilder.createTemplates(templates) - jsonObject:JValue = decompose(templatesJson) - }yield{ + cc => { + for (u <- cc.user ?~ UserNotLoggedIn; templates <- APIBuilder_Connector.getTemplates; templatesJson = JsonFactory_APIBuilder.createTemplates(templates); jsonObject: JValue = decompose(templatesJson)) yield { successJsonResponse(jsonObject) } } } - - resourceDocs += ResourceDoc( - getTemplate, - apiVersion, - "getTemplate", - "GET", - "/templates/TEMPLATE_ID", - "Get Template ", - "Get a template by Id, Authentication is Mandatory", - emptyObjectJson, - templateJson, - List(UserNotLoggedIn, UnknownError), - Catalogs(notCore, notPSD2, notOBWG), - apiTagApiBuilder :: Nil - ) - lazy val getTemplate: OBPEndpoint ={ - case "templates" :: templateId :: Nil JsonGet _ => { - cc => - { - for{ - u <- cc.user ?~ UserNotLoggedIn - template <- APIBuilder_Connector.getTemplateById(templateId) ?~! TemplateNotFound - templateJson = JsonFactory_APIBuilder.createTemplate(template) - jsonObject:JValue = decompose(templateJson) - }yield{ + resourceDocs += ResourceDoc(getTemplate, apiVersion, "getTemplate", "GET", "/templates/TEMPLATE_ID", "Get Template", "Return One Template By Id", emptyObjectJson, templateJson, List(UserNotLoggedIn, UnknownError), Catalogs(notCore, notPSD2, notOBWG), apiTagApiBuilder :: Nil) + lazy val getTemplate: OBPEndpoint = { + case ("templates" :: templateId :: Nil) JsonGet _ => + cc => { + for (u <- cc.user ?~ UserNotLoggedIn; template <- APIBuilder_Connector.getTemplateById(templateId) ?~! TemplateNotFound; templateJson = JsonFactory_APIBuilder.createTemplate(template); jsonObject: JValue = decompose(templateJson)) yield { successJsonResponse(jsonObject) } } - } } - - resourceDocs += ResourceDoc( - createTemplate, - apiVersion, - "createTemplate", - "POST", - "/templates", - "Create Template ", - "Create one template, Authentication is Mandatory", - createTemplateJson, - templateJson, - List(InvalidJsonFormat,UserNotLoggedIn, UnknownError), - Catalogs(notCore, notPSD2, notOBWG), - apiTagApiBuilder :: Nil - ) - lazy val createTemplate: OBPEndpoint ={ - case "templates" :: Nil JsonPost json -> _ => { - cc => - { - for{ - createTemplateJson <- tryo(json.extract[CreateTemplateJson]) ?~! InvalidJsonFormat - u <- cc.user ?~ UserNotLoggedIn - template <- APIBuilder_Connector.createTemplate(createTemplateJson.author, createTemplateJson.pages, createTemplateJson.points) - templateJson = JsonFactory_APIBuilder.createTemplate(template) - jsonObject:JValue = decompose(templateJson) - }yield{ + resourceDocs += ResourceDoc(createTemplate, apiVersion, "createTemplate", "POST", "/templates", "Create Template", "Create One Template", createTemplateJson, templateJson, List(UnknownError), Catalogs(notCore, notPSD2, notOBWG), apiTagApiBuilder :: Nil) + lazy val createTemplate: OBPEndpoint = { + case ("templates" :: Nil) JsonPost json -> _ => + cc => { + for (createTemplateJson <- tryo(json.extract[CreateTemplateJson]) ?~! InvalidJsonFormat; u <- cc.user ?~ UserNotLoggedIn; template <- APIBuilder_Connector.createTemplate(createTemplateJson); templateJson = JsonFactory_APIBuilder.createTemplate(template); jsonObject: JValue = decompose(templateJson)) yield { successJsonResponse(jsonObject) } } - } } - - resourceDocs += ResourceDoc( - deleteTemplate, - apiVersion, - "deleteTemplate", - "DELETE", - "/templates/TEMPLATE_ID", - "Delete Template ", - "Delete a template, Authentication is Mandatory", - emptyObjectJson, - emptyObjectJson.copy("true"), - List(UserNotLoggedIn, UnknownError), - Catalogs(notCore, notPSD2, notOBWG), - apiTagApiBuilder :: Nil - ) - lazy val deleteTemplate: OBPEndpoint ={ - case "templates" :: templateId :: Nil JsonDelete _ => { - cc => - { - for{ - u <- cc.user ?~ UserNotLoggedIn - deleted <- APIBuilder_Connector.deleteTemplate(templateId) - }yield{ - if(deleted) - noContentJsonResponse - else - errorJsonResponse("Delete not completed") + resourceDocs += ResourceDoc(deleteTemplate, apiVersion, "deleteTemplate", "DELETE", "/templates/TEMPLATE_ID", "Delete Template", "Delete One Template", emptyObjectJson, emptyObjectJson.copy("true"), List(UserNotLoggedIn, UnknownError), Catalogs(notCore, notPSD2, notOBWG), apiTagApiBuilder :: Nil) + lazy val deleteTemplate: OBPEndpoint = { + case ("templates" :: templateId :: Nil) JsonDelete _ => + cc => { + for (u <- cc.user ?~ UserNotLoggedIn; deleted <- APIBuilder_Connector.deleteTemplate(templateId)) yield { + if (deleted) noContentJsonResponse else errorJsonResponse("Delete not completed") } } - } } } } - - -object APIBuilder_Connector -{ - val allAPIBuilderModels = List(MappedTemplate) - - def createTemplate( - author: String, - pages: Int, - points: Double - ) = - Full( - MappedTemplate.create - .mTemplateId(UUID.randomUUID().toString) - .mAuthor(author) - .mPages(pages) - .mPoints(points) - .saveMe() - ) - - def getTemplates()= Full(MappedTemplate.findAll()) - - def getTemplateById(templateId: String)= MappedTemplate.find(By(MappedTemplate.mTemplateId, templateId)) - - def deleteTemplate(templateId: String)= MappedTemplate.find(By(MappedTemplate.mTemplateId, templateId)).map(_.delete_!) - +object APIBuilder_Connector { + val allAPIBuilderModels = List(MappedTemplate_4824100653501473508) + def createTemplate(createTemplateJson: CreateTemplateJson) = Full(MappedTemplate_4824100653501473508.create.mTemplateId(UUID.randomUUID().toString).mAuthor(createTemplateJson.author).mPages(createTemplateJson.pages).mPoints(createTemplateJson.points).saveMe()) + def getTemplates() = Full(MappedTemplate_4824100653501473508.findAll()) + def getTemplateById(templateId: String) = MappedTemplate_4824100653501473508.find(By(MappedTemplate_4824100653501473508.mTemplateId, templateId)) + def deleteTemplate(templateId: String) = MappedTemplate_4824100653501473508.find(By(MappedTemplate_4824100653501473508.mTemplateId, templateId)).map(_.delete_!) } - import net.liftweb.mapper._ - -class MappedTemplate extends Template with LongKeyedMapper[MappedTemplate] with IdPK { - def getSingleton = MappedTemplate - - object mTemplateId extends MappedString(this,100) - object mAuthor extends MappedString(this,100) - object mPages extends MappedInt(this) - object mPoints extends MappedDouble(this) - - override def templateId: String = mTemplateId.get +class MappedTemplate_4824100653501473508 extends Template with LongKeyedMapper[MappedTemplate_4824100653501473508] with IdPK { + object mAuthor extends MappedString(this, 100) override def author: String = mAuthor.get + object mPages extends MappedInt(this) override def pages: Int = mPages.get + object mPoints extends MappedDouble(this) override def points: Double = mPoints.get + def getSingleton = MappedTemplate_4824100653501473508 + object mTemplateId extends MappedString(this, 100) + override def templateId: String = mTemplateId.get } - -object MappedTemplate extends MappedTemplate with LongKeyedMetaMapper[MappedTemplate] { -} - -trait Template { - def templateId : String - def author : String - def pages : Int - def points : Double +object MappedTemplate_4824100653501473508 extends MappedTemplate_4824100653501473508 with LongKeyedMetaMapper[MappedTemplate_4824100653501473508] +trait Template { `_` => + def author: String + def pages: Int + def points: Double + def templateId: String } \ No newline at end of file diff --git a/src/main/scala/code/api/builder/JsonFactory_APIBuilder.scala b/src/main/scala/code/api/builder/JsonFactory_APIBuilder.scala index 909ce5fd1..f854cd1cf 100644 --- a/src/main/scala/code/api/builder/JsonFactory_APIBuilder.scala +++ b/src/main/scala/code/api/builder/JsonFactory_APIBuilder.scala @@ -1,63 +1,15 @@ -/** -Open Bank Project - API -Copyright (C) 2011-2018, TESOBE Ltd - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . - -Email: contact@tesobe.com -TESOBE Ltd -Osloerstrasse 16/17 -Berlin 13359, Germany - - This product includes software developed at - TESOBE (http://www.tesobe.com/) - */ - package code.api.builder - import code.api.util.APIUtil - -case class TemplateJson( - template_id: String = "123 abcd", - author: String = """Chinua Achebe""", - pages: Int = 209, - points: Double = 1.3 -) - -case class CreateTemplateJson( - author: String = """Chinua Achebe""", - pages: Int = 209, - points: Double = 1.3 -) - -object JsonFactory_APIBuilder -{ - +case class CreateTemplateJson(author: String = """Chinua Achebe""", pages: Int = 209, points: Double = 1.3) +case class TemplateJson(template_id: String = """11231231312""", author: String = """Chinua Achebe""", pages: Int = 209, points: Double = 1.3) +object JsonFactory_APIBuilder { val templateJson = TemplateJson() val templatesJson = List(templateJson) val createTemplateJson = CreateTemplateJson() - - def createTemplate(template: Template) = TemplateJson(template.templateId,template.author,template.pages,template.points) - def createTemplates(templates: List[Template])= - templates.map(template => TemplateJson(template.templateId,template.author,template.pages,template.points)) - - val allFields = for ( - v <- this.getClass.getDeclaredFields; - if APIUtil.notExstingBaseClass(v.getName()) - ) yield - { - v.setAccessible(true) - v.get(this) - } + def createTemplate(template: Template) = TemplateJson(template.templateId, template.author, template.pages, template.points) + def createTemplates(templates: List[Template]) = templates.map(template => TemplateJson(template.templateId, template.author, template.pages, template.points)) + val allFields = for (v <- this.getClass.getDeclaredFields; if APIUtil.notExstingBaseClass(v.getName())) yield { + v.setAccessible(true) + v.get(this) + } } \ No newline at end of file diff --git a/src/test/scala/code/api/APIBuilder/APIBuilderModelTest.scala b/src/test/scala/code/api/APIBuilder/APIBuilderModelTest.scala index 4d1c020c4..a0577b08a 100644 --- a/src/test/scala/code/api/APIBuilder/APIBuilderModelTest.scala +++ b/src/test/scala/code/api/APIBuilder/APIBuilderModelTest.scala @@ -36,19 +36,27 @@ import net.liftweb.json.JValue class APIBuilderModelTest extends FlatSpec with Matchers with MdcLoggable { - val jsonStringFromFile: String = scala.io.Source.fromFile("src/test/scala/code/api/APIBuilder/modelSource.json").mkString + val jsonStringFromFile: String = + """{ + "request_url": "/templates", + "template": { + "author": "Chinua Achebe", + "pages": 209, + "points": 1.3 + } + }""" val jsonJValueFromFile: JValue = json.parse(jsonStringFromFile) - //"book" + //"template" val modelName = getModelName(jsonJValueFromFile) - //BOOK + //TEMPLATE val modelNameUpperCase = modelName.toUpperCase - //book + //template val modelNameLowerCase = modelName.toLowerCase - //Book + //Template val modelNameCapitalized = modelNameLowerCase.capitalize - //MappedBook_1 + //MappedTemplate_1 val modelMappedName = s"Mapped${modelNameCapitalized}_1" val modelTypeName: Type.Name = Type.Name(modelMappedName) val modelTermName = Term.Name(modelMappedName) @@ -63,16 +71,16 @@ class APIBuilderModelTest extends FlatSpec with Matchers with MdcLoggable { "getApiUrl" should "work as expected" in { val apiUrl: String = APIBuilderModel.getApiUrl(jsonJValueFromFile) - apiUrl should be ("/books") + apiUrl should be ("/templates") - val jvalueMissingSlash: JValue = json.parse("""{"request_url":"books/my"}""") + val jvalueMissingSlash: JValue = json.parse("""{"request_url":"templates/my"}""") val apiUrl2: String = APIBuilderModel.getApiUrl(jvalueMissingSlash) - apiUrl2 should be ("/books/my") + apiUrl2 should be ("/templates/my") } "getModelName" should "work as expected" in { val apiUrl: String = APIBuilderModel.getModelName(jsonJValueFromFile) - apiUrl should be ("book") + apiUrl should be ("template") } "getModelFieldsNames" should "work as expected" in { @@ -152,14 +160,14 @@ class APIBuilderModelTest extends FlatSpec with Matchers with MdcLoggable { "getModelClass" should "work as expected" in { val modelClass= APIBuilderModel.getModelClass(modelTypeName, modelTermName, modelFieldsNames, modelFieldTypes) modelClass.toString() should be ( - "class MappedBook_1 extends Template with LongKeyedMapper[MappedBook_1] with IdPK {" + + "class MappedTemplate_1 extends Template with LongKeyedMapper[MappedTemplate_1] with IdPK {" + "\n object mAuthor extends MappedString(this, 100)" + "\n override def author: String = mAuthor.get" + "\n object mPages extends MappedInt(this)" + "\n override def pages: Int = mPages.get" + "\n object mPoints extends MappedDouble(this)" + "\n override def points: Double = mPoints.get" + - "\n def getSingleton = MappedBook_1" + + "\n def getSingleton = MappedTemplate_1" + "\n object mTemplateId extends MappedString(this, 100)" + "\n override def templateId: String = mTemplateId.get" + "\n" + @@ -170,7 +178,7 @@ class APIBuilderModelTest extends FlatSpec with Matchers with MdcLoggable { val createModelJsonMethod= APIBuilderModel.generateCreateModelJsonMethod(modelFieldsNames, modelMappedName) createModelJsonMethod.toString() should be ( "def createTemplate(createTemplateJson: CreateTemplateJson) = " + - "Full(MappedBook_1.create" + + "Full(MappedTemplate_1.create" + ".mTemplateId(UUID.randomUUID().toString)" + ".mAuthor(createTemplateJson.author)" + ".mPages(createTemplateJson.pages)" + @@ -191,11 +199,11 @@ class APIBuilderModelTest extends FlatSpec with Matchers with MdcLoggable { } "createTemplateJsonClass" should "work as expected" in { - val className ="Book" - val templateIdField: Term.Param = Term.Param(Nil, Term.Name(s"book_id"), Some(Type.Name("String")), Some(Term.Name("`11231231312`"))) + val className ="Template" + val templateIdField: Term.Param = Term.Param(Nil, Term.Name(s"template_id"), Some(Type.Name("String")), Some(Term.Name("`11231231312`"))) val templateJsonClassParams = List(templateIdField) val templateJsonClass: Defn.Class = APIBuilderModel.createTemplateJsonClass(className, templateJsonClassParams) - templateJsonClass.toString() should be ("case class Book(book_id: String = `11231231312`)") + templateJsonClass.toString() should be ("case class Template(template_id: String = `11231231312`)") } } \ No newline at end of file diff --git a/src/test/scala/code/api/APIBuilder/modelSource.json b/src/test/scala/code/api/APIBuilder/modelSource.json deleted file mode 100644 index eb765391f..000000000 --- a/src/test/scala/code/api/APIBuilder/modelSource.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "request_url": "/books", - "book": { - "author": "Chinua Achebe", - "pages": 209, - "points": 1.3 - } -} \ No newline at end of file