Advertisement
Guest User

Untitled

a guest
Feb 16th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.06 KB | None | 0 0
  1. import carPart._
  2. import com.fasterxml.jackson.databind.{ObjectMapper, DeserializationFeature}
  3. import com.twitter.finatra._
  4. import org.jboss.netty.handler.codec.http.{HttpVersion, HttpResponseStatus}
  5. import JsonUtil.JsonUtil
  6.  
  7. class AutoserviceServer extends Controller {
  8.  
  9.   var carsDatabase = collection.mutable.Map[String, List[Car]]()
  10.  
  11.   var audiCars = List(
  12.  
  13.     Car("1", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "A4", "", "A888C", "2013", "petrol", "3.3",
  14.       "210000", "automatic", "3650", "available", Bid("user1", "4000", "11")),
  15.     Car("2", "../images/audi/1.jpg", "A5", "", "A578B", "2013", "petrol", "3.3",
  16.       "1000", "automatic", "4650", "available", Bid("user2", "5000", "3")),
  17.     Car("3", "../../images/1.jpg", "A6", "", "A234D", "2013", "petrol", "3.3",
  18.       "210000", "automatic", "3330", "won", Bid("user1", "3400", "0"))
  19.  
  20.   );
  21.   var bmwCars = List(
  22.     Car("11", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "4", "", "B234N", "2013", "petrol", "3.3",
  23.       "210000", "automatic", "6936", "won", Bid("user1", "7000", "0")),
  24.     Car("12", "../images/audi/1.jpg", "1", "", "B422X", "2013", "petrol", "3.3",
  25.       "210000", "automatic", "8000", "available", Bid("user1", "9000", "8")),
  26.     Car("13", "../../images/1.jpg", "3", "", "B231T", "2013", "petrol", "3.3",
  27.       "210000", "automatic", "2333", "available", Bid("", "", ""))
  28.  
  29.   );
  30.  
  31.   carsDatabase.put("audi", audiCars)
  32.   carsDatabase.put("bmw", bmwCars)
  33.  
  34.   var carPartsDatabase = collection.mutable.Map[String, List[Any]]()
  35.  
  36.   var bodyParts = List(
  37.  
  38.     BodyPart("1", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4", "", "2005", "door", "67",
  39.       "available"),
  40.     BodyPart("2", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A5", "", "2000", "roof", "10",
  41.       "sold"),
  42.     BodyPart("3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4", "", "2013", "roof", "80",
  43.       "available")
  44.  
  45.   );
  46.  
  47.   var engines = List(
  48.  
  49.     Engine("1", "1111S3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4","", "2005", "petrol", "1.8",
  50.       "210000","AWT 026371","110","670","available"),
  51.     Engine("2", "1111R3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A5","", "2000", "petrol", "1.8",
  52.       "210000","AWT 023423","100","1000","sold"),
  53.     Engine("3", "1111K3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4","", "2013", "petrol", "2.0",
  54.       "","AWT 45646","180","900","available")
  55.   );
  56.  
  57.   var wheelsTires = List(
  58.  
  59.     WheelTire("1", "33323S3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4","", "wheel", "R16", "5",
  60.       "available"),
  61.     WheelTire("2", "3333S3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A5","", "wheel", "R16", "5",
  62.       "sold"),
  63.     WheelTire("3", "3333S3", "http://www.avtorinok.ru/news/pics/Audi_A4_pic_24667.jpg", "audi", "A4","", "wheel", "R17", "90",
  64.       "available")
  65.  
  66.  
  67.   );
  68.  
  69.   carPartsDatabase.put("body-part", bodyParts)
  70.   carPartsDatabase.put("engine", engines)
  71.   carPartsDatabase.put("wheels-tires", wheelsTires)
  72.  
  73.  
  74.   get("/api/catalog") { request =>
  75.     val json = JsonUtil.toJson(carsDatabase)
  76.     render.json(json).toFuture
  77.   }
  78.  
  79.  
  80.   get("/api/catalog/:carBrand") { request =>
  81.     val carBrand = request.routeParams.getOrElse("carBrand", "default")
  82.     val json = JsonUtil.toJson(carsDatabase.get(carBrand))
  83.     render.json(json).toFuture
  84.  
  85.   }
  86.  
  87.   get("/api/catalog/:carBrand") { request =>
  88.     val carBrand = request.routeParams.getOrElse("carBrand", "default")
  89.     val json = JsonUtil.toJson(carsDatabase.get(carBrand))
  90.     render.json(json).toFuture
  91.  
  92.   }
  93.  
  94.   get("/api/catalog/:carBrand/:model") { request =>
  95.     val carBrand = request.routeParams.getOrElse("carBrand", "default")
  96.     val modelCar = request.routeParams.getOrElse("model", "default")
  97.     var json: String = null
  98.     carsDatabase(carBrand).foreach { Car =>
  99.  
  100.       if (Car.model.equals(modelCar)) {
  101.         json = JsonUtil.toJson(Car)
  102.       }
  103.     }
  104.     render.json(json).toFuture
  105.  
  106.   }
  107.  
  108.  put("/api/catalog/:carBrand") { request =>
  109.     val c = request.getContentString()
  110.     val simple = AutosMap.parseToMap(c.substring(1, c.size - 1)) // вернёт Map
  111.     AutosMap.writeJson(c)
  112.     render.plain("okay").toFuture
  113.  
  114.   }
  115.  
  116.  notFound { request =>
  117.     render.status(404).plain("Not found").toFuture
  118.   }
  119.  
  120.  class Unauthorized extends Exception
  121.  
  122.   get("/unauthorized") { request =>
  123.     throw new Unauthorized
  124.   }
  125.   error { request =>
  126.     request.error match {
  127.       case Some(e: ArithmeticException) =>
  128.         render.status(500).plain("whoops, divide by zero!").toFuture
  129.       case Some(e: Unauthorized) =>
  130.         render.status(401).plain("Not Authorized!").toFuture
  131.       case Some(e: UnsupportedMediaType) =>
  132.         render.status(415).plain("Unsupported Media Type!").toFuture
  133.       case _ =>
  134.         render.status(500).plain("Something went wrong!").toFuture
  135.     }
  136.   }
  137.  
  138.  
  139. }
  140.  
  141.  
  142. object App extends FinatraServer {
  143.   register(new AutoserviceServer())
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement