Permalink
Browse files

added no-format sample

  • Loading branch information...
1 parent 4054460 commit 991ce30b0271923618d5bbb08542ca6e1097e17f @fehguy fehguy committed Sep 18, 2012
Showing with 1,153 additions and 0 deletions.
  1. +35 −0 samples/scala-play2-no-format/README.md
  2. +129 −0 samples/scala-play2-no-format/app/api/PetData.scala
  3. +62 −0 samples/scala-play2-no-format/app/api/StoreData.scala
  4. +74 −0 samples/scala-play2-no-format/app/api/UserData.scala
  5. +41 −0 samples/scala-play2-no-format/app/controllers/BaseApiController.scala
  6. +101 −0 samples/scala-play2-no-format/app/controllers/PetApiController.scala
  7. +67 −0 samples/scala-play2-no-format/app/controllers/StoreApiController.scala
  8. +126 −0 samples/scala-play2-no-format/app/controllers/UserApiController.scala
  9. +49 −0 samples/scala-play2-no-format/app/models/ApiResponse.scala
  10. +32 −0 samples/scala-play2-no-format/app/models/Category.scala
  11. +48 −0 samples/scala-play2-no-format/app/models/Order.scala
  12. +52 −0 samples/scala-play2-no-format/app/models/Pet.scala
  13. +32 −0 samples/scala-play2-no-format/app/models/Tag.scala
  14. +59 −0 samples/scala-play2-no-format/app/models/User.scala
  15. +96 −0 samples/scala-play2-no-format/app/security/AuthorizationFilter.scala
  16. +33 −0 samples/scala-play2-no-format/app/value/ApiResponse.scala
  17. +35 −0 samples/scala-play2-no-format/conf/application.conf
  18. +14 −0 samples/scala-play2-no-format/conf/log4j.properties
  19. +38 −0 samples/scala-play2-no-format/conf/routes
  20. +21 −0 samples/scala-play2-no-format/project/Build.scala
  21. +1 −0 samples/scala-play2-no-format/project/build.properties
  22. +7 −0 samples/scala-play2-no-format/project/plugins.sbt
  23. BIN samples/scala-play2-no-format/public/images/favicon.png
  24. +1 −0 samples/scala-play2-no-format/start
@@ -0,0 +1,35 @@
+# Swagger Playframework Sample App
+
+## Overview
+This is a scala project to build a stand-alone server which implements the Swagger spec. You can find out
+more about both the spec and the framework at http://swagger.wordnik.com. For more information
+about Wordnik's APIs, please visit http://developer.wordnik.com. There is an online version of this
+server at http://petstore.swagger.wordnik.com/api/resources.json
+
+### To build from source
+Please follow instructions to build the top-level [swagger-core project](https://github.com/wordnik/swagger-core)
+
+### To run
+The swagger-play2 module lives in a github repository, currently not in maven central. You'll have to add
+a dependency and a resolver to your Build.scala like this:
+
+```scala
+ val appDependencies: Seq[sbt.ModuleID] = Seq(
+ /* your other dependencies */
+ "com.wordnik" %% "swagger-play2" % "1.1.1")
+
+ val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
+ Resolver.url("swagger-core-github-repo", url("http://wordnik.github.com/repository"))(Resolver.ivyStylePatterns),
+ /* your other resolvers */
+ )
+}
+```
+
+then you can build the sample app:
+
+````
+play run
+````
+
+The application will listen on port 9000 and respond to `http://localhost:9000/resources`
+
@@ -0,0 +1,129 @@
+/**
+ * Copyright 2012 Wordnik, Inc.
+
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+import models._
+
+import java.util.ArrayList
+
+import scala.collection.mutable.ListBuffer
+import scala.collection.JavaConversions._
+
+class PetData {
+ val pets: ListBuffer[Pet] = new ListBuffer[Pet]()
+ val categories: ListBuffer[Category] = new ListBuffer[Category]()
+
+ {
+ categories += createCategory(1, "Dogs")
+ categories += createCategory(2, "Cats")
+ categories += createCategory(3, "Rabbits")
+ categories += createCategory(4, "Lions")
+
+ pets += createPet(1, categories(1), "Cat 1", List("url1", "url2"), List("tag1", "tag2"), "available")
+ pets += createPet(2, categories(1), "Cat 2", List("url1", "url2"), List("tag2", "tag3"), "available")
+ pets += createPet(3, categories(1), "Cat 3", List("url1", "url2"), List("tag3", "tag4"), "pending")
+
+ pets += createPet(4, categories(0), "Dog 1", List("url1", "url2"), List("tag1", "tag2"), "available")
+ pets += createPet(5, categories(0), "Dog 2", List("url1", "url2"), List("tag2", "tag3"), "sold")
+ pets += createPet(6, categories(0), "Dog 3", List("url1", "url2"), List("tag3", "tag4"), "pending")
+
+ pets += createPet(7, categories(3), "Lion 1", List("url1", "url2"), List("tag1", "tag2"), "available")
+ pets += createPet(8, categories(3), "Lion 2", List("url1", "url2"), List("tag2", "tag3"), "available")
+ pets += createPet(9, categories(3), "Lion 3", List("url1", "url2"), List("tag3", "tag4"), "available")
+
+ pets += createPet(10, categories(2), "Rabbit 1", List("url1", "url2"), List("tag3", "tag4"), "available")
+
+ }
+
+ def getPetbyId(petId: Long): Option[Pet] = {
+ pets.filter(pet => pet.getId == petId) match {
+ case pets if(pets.size) > 0 => Some(pets.head)
+ case _ => None
+ }
+ }
+
+ def findPetByStatus(status: String): java.util.List[Pet] = {
+ var statues = status.split(",")
+ var result = new java.util.ArrayList[Pet]()
+ for (pet <- pets) {
+ if (statues.contains(pet.getStatus())) {
+ result.add(pet)
+ }
+ }
+ result
+ }
+
+ def findPetByTags(tags: String): java.util.List[Pet] = {
+ var tagList = tags.split(",")
+ var result = new java.util.ArrayList[Pet]()
+ for (pet <- pets) {
+ if (null != pet.getTags()) {
+ for (tag <- pet.getTags()) {
+ if (tagList.contains(tag.getName)) {
+ result.add(pet)
+ }
+ }
+ }
+ }
+ result
+ }
+
+ def addPet(pet: Pet): Unit = {
+ pets --= pets.filter(existing => existing.getId == pet.getId)
+ pets += pet
+ }
+
+ private def createPet(id: Long, cat: Category, name: String, urls: List[String], tags: List[String], status: String): Pet = {
+ val pet = new Pet
+ pet.setId(id)
+ pet.setCategory(cat)
+ pet.setName(name)
+ if (null != urls) {
+ var urlObjs: java.util.List[String] = new java.util.ArrayList[String]()
+ for (urlString <- urls) {
+ urlObjs.add(urlString)
+ }
+ pet.setPhotoUrls(urlObjs)
+ }
+ var tagObjs: java.util.List[Tag] = new java.util.ArrayList[Tag]()
+ var i = 0
+ if (null != tags) {
+ for (tagString <- tags) {
+ i = i + 1
+ val tag = new Tag()
+ tag.setId(i)
+ tag.setName(tagString)
+ tagObjs.add(tag)
+ }
+ }
+ pet.setTags(tagObjs)
+ pet.setStatus(status)
+ pet
+ }
+
+ private def createCategory(id: Long, name: String): Category = {
+ val category = new Category
+ category.setId(id)
+ category.setName(name)
+ category
+ }
+}
+
+class PetStoreApi {
+
+}
@@ -0,0 +1,62 @@
+/**
+ * Copyright 2012 Wordnik, Inc.
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+import collection.mutable.ListBuffer
+
+import models.Order
+import java.util.Date
+
+class StoreData {
+ val orders: ListBuffer[Order] = new ListBuffer[Order]()
+
+ {
+ orders += createOrder(1, 1, 2, new Date(), "placed")
+ orders += createOrder(2, 1, 2, new Date(), "delivered")
+ orders += createOrder(3, 2, 2, new Date(), "placed")
+ orders += createOrder(4, 2, 2, new Date(), "delivered")
+ orders += createOrder(5, 3, 2, new Date(), "placed")
+ }
+
+ def findOrderById(orderId: Long): Option[Order] = {
+ orders.filter(order => order.getId == orderId) match {
+ case orders if (orders.size) > 0 => Some(orders.head)
+ case _ => None
+ }
+ }
+
+ def placeOrder(order: Order) = {
+ // remove any pets with same id
+ orders --= orders.filter(o => o.getId == order.getId)
+ orders += order
+ }
+
+ def deleteOrder(orderId: Long): Unit = {
+ orders --= orders.filter(o => o.getId == orderId)
+ }
+
+ private def createOrder(id: Long, petId: Long, quantity: Int, shipDate: Date, status: String): Order = {
+ val order = new Order()
+ order.setId(id)
+ order.setPetId(petId)
+ order.setQuantity(quantity)
+ order.setShipDate(shipDate)
+ order.setStatus(status)
+ order
+ }
+}
@@ -0,0 +1,74 @@
+/**
+ * Copyright 2012 Wordnik, Inc.
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+import models.{ Category, User, Pet }
+
+import collection.mutable.ListBuffer
+
+class UserData {
+ val users: ListBuffer[User] = new ListBuffer[User]()
+
+ {
+ users += createUser(1, "user1", "first name 1", "last name 1", "email1@test.com", "123-456-7890", 1)
+ users += createUser(2, "user2", "first name 2", "last name 2", "email2@test.com", "123-456-7890", 2)
+ users += createUser(3, "user3", "first name 3", "last name 3", "email3@test.com", "123-456-7890", 3)
+ users += createUser(4, "user4", "first name 4", "last name 4", "email4@test.com", "123-456-7890", 1)
+ users += createUser(5, "user5", "first name 5", "last name 5", "email5@test.com", "123-456-7890", 2)
+ users += createUser(6, "user6", "first name 6", "last name 6", "email6@test.com", "123-456-7890", 3)
+ users += createUser(7, "user7", "first name 7", "last name 7", "email7@test.com", "123-456-7890", 1)
+ users += createUser(8, "user8", "first name 8", "last name 8", "email8@test.com", "123-456-7890", 2)
+ users += createUser(9, "user9", "first name 9", "last name 9", "email9@test.com", "123-456-7890", 3)
+ users += createUser(10, "user10", "first name 10", "last name 10", "email10@test.com", "123-456-7890", 1)
+ users += createUser(11, "user?10", "first name ?10", "last name ?10", "email101@test.com", "123-456-7890", 1)
+
+ }
+
+ def findUserByName(username: String): Option[User] = {
+ users.filter(user => user.getUsername == username) match {
+ case user if (user.size) > 0 => Some(user.head)
+ case _ => None
+ }
+ }
+
+ def addUser(user: User): Unit = {
+ users --= users.filter(u => u.getId == user.getId)
+ users += user
+ }
+
+ def removeUser(username: String): Unit = {
+ for (user <- users) {
+ if (user.getUsername() == username) {
+ users -= user
+ }
+ }
+ }
+
+ private def createUser(id: Long, username: String, firstName: String, lastName: String, email: String, phone: String, userStatus: Int): User = {
+ var user = new User
+ user.setId(id)
+ user.setUsername(username)
+ user.setFirstName(firstName)
+ user.setLastName(lastName)
+ user.setEmail(email)
+ user.setPassword("XXXXXXXXXXX")
+ user.setPhone(phone)
+ user.setUserStatus(userStatus)
+ user
+ }
+}
@@ -0,0 +1,41 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data._
+import play.api.data.Forms._
+import play.api.data.format.Formats._
+import play.api.Play.current
+
+import value._
+import api._
+import com.wordnik.swagger.core.util.{ JsonUtil, RestResourceUtil }
+
+import java.io.StringWriter
+
+object BaseApiController {
+ val mapper = JsonUtil.getJsonMapper
+}
+
+class BaseApiController extends Controller with RestResourceUtil {
+ // APIs
+ protected def JsonResponse(data: Object) = {
+ val w = new StringWriter()
+
+ BaseApiController.mapper.writeValue(w, data)
+
+ val jsonValue: String = w.toString()
+ new SimpleResult[String](header = ResponseHeader(200), body = play.api.libs.iteratee.Enumerator(jsonValue)).as("application/json")
+ .withHeaders(("Access-Control-Allow-Origin", "*"))
+ }
+
+ protected def JsonResponse(data: Object, code: Int) = {
+ val w = new StringWriter()
+
+ BaseApiController.mapper.writeValue(w, data)
+
+ val jsonValue: String = w.toString()
+ new SimpleResult[String](header = ResponseHeader(code), body = play.api.libs.iteratee.Enumerator(jsonValue)).as("application/json")
+ .withHeaders(("Access-Control-Allow-Origin", "*"))
+ }
+}
Oops, something went wrong.

0 comments on commit 991ce30

Please sign in to comment.